我在 Apache xampp 设置上使用 Codeigniter 并首次尝试在视图中使用 foreach,但我无法让它工作。
我的控制器代码:
class Test extends CI_Controller {
public function index($page = 'testv')
{
if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
{
show_404();
}
$this->load->model('testm');
$data['news'] = $this->testm->get_news();
$this->load->view('headder');
$this->load->view($page, $data);
$this->load->view('footer');
}
}
Run Code Online (Sandbox Code Playgroud)
我的型号:
class Testm extends CI_Model {
public function __construct()
{
parent::__construct();
}
Public function get_news()
{
$query = $this->db->query('SELECT id, date, text FROM news');
foreach ($query->result_array() as $row)
{
echo $row['id'];
echo $row['date'];
echo $row['text'];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的看法:
<main id='central_section'>
<section id='logo_section'>
<img …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个表单验证回调函数,但我遇到了一些麻烦.
我想要做的是创建一个联系表单,其中加入邮件列表选项.如果选中加入邮件列表的选项,我希望将该人员的姓名和电子邮件添加到邮件列表数据库中.这部分工作正常,但我也希望功能检查数据库,以确保添加的电子邮件地址是唯一的,这是我无法理解的一点.
控制器:
public function contact()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
$this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
if($this->form_validation->run() == FALSE)
{
$this->load->view('templates/headder');
$this->load->view('contact');
$this->load->view('templates/footer');
}
else
{
$this->load->library('email');
$name = $this->input->post('name');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$message = $this->input->post('message');
$list = $this->input->post('mailing_list');
$email_message = "Name: $name<br>Email: $email<br>Phone: $phone<br>Message:<br>$message";
$this->email->initialize();
$this->email->from($email, $name);
$this->email->to('myaddress@mydomain.co.uk');
$this->email->subject('New Query');
$this->email->message($email_message);
$this->email->send();
if($this->email->send()){
$this->load->view('send_error');
}
else
{
if($list == 'no')
{
$this->load->view('sent');
}
else
{
$this->form_validation->set_rules('email', …Run Code Online (Sandbox Code Playgroud)