我有一个包含多个条目的数据库。当我加载/计划时,它将正确加载所有条目。但是,当我尝试使用/ schedule / state时,在视图上显示“为foreach()提供了无效参数”的错误提示。我已经使它以某种方式工作(尽管我显然已经将其破坏了),但是当我将实际状态添加到末尾时,它总是会抛出此错误...即。 / schedule / state /犹他州。
有人可以发现我在做什么错吗?预先谢谢你!
视图(views / schedule / state.php):
<h2><?php echo $title; ?></h2>
<div class='container'>
<div class='row'>
<?php
function printDateRange($start_date, $end_date) {
$string = "";
if($start_date !== $end_date) {
$string = "".date_format(date_create($start_date),"F d, Y")." - ".date_format(date_create($end_date),"F d, Y")."";
} else {
$string = date_format(date_create($start_date),"F d, Y");
}
return $string;
}
$counter = 0;
foreach ($events as $event) {
$id = $event['id'];
$slug = $event['slug'];
$title = $event['title'];
$start_date = $event['start_date'];
$end_date = $event['end_date'];
$address = $event['address'];
$city = $event['city'];
$state = $event['state'];
$country = $event['country'];
$postal_code = $event['postal_code'];
$image = $event['image'];
$website = $event['website'];
$facebook = $event['facebook'];
$instagram = $event['instagram'];
$twitter = $event['twitter'];
$first_year_held = $event['first_year_held'];
$attendance = $event['attendance'];
$ticket_price = $event['ticket_price'];
$description = $event['description'];
echo "
<div class='col-12 col-md-3'>
<div class='schedule_box' style='width: 100%; max-width: 600px; background-color: #200080; color: #A0A0FF; padding: 10px; border: dash 1px #000; margin-bottom: 40px;'>
<div style='width: 100%; text-align: center; padding: 4px; color: #CCCCFF;'>
<h2>".$title."</h2>
</div>
<div style='width: 100%;'>
<img src='".$image."' style='width: 100%;'>
</div>
<div style='width: 100%; padding-top: 10px'>
Date: ".printDateRange($start_date, $end_date)."
</div>
<div style='width: 100%; padding-top: 10px;'>
".$address."<br/>
".$city.", ".$state."<br/>
".$country."<br/>
".$postal_code."
</div>
<div style='width: 100%; padding: 4px; text-align: center;'>
<a href='".$website."' style='color: #DDD;'>Visit Official Website</a>
</div>
</div>
</div>
";
$counter++;
if($counter == 4) {
echo "</div><div class='row'>";
$counter = 0;
}
}
?>
</div>
</div>
Controller(Schedule.php):
<?php
class Schedule extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('schedule_model');
$this->load->helper('url_helper');
}
public function index() {
$data['events'] = $this->schedule_model->get_events();
$data['title'] = 'Schedule';
$this->load->view('templates/header', $data);
$this->load->view('schedule/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL) {
$data['event'] = $this->schedule_model->get_events();
if (empty($data['events']))
{
show_404();
}
$data['title'] = $data['event']['id'];
$this->load->view('templates/header', $data);
$this->load->view('schedule/index', $data);
$this->load->view('templates/footer');
}
public function state($slug = NULL) {
$data['events'] = $this->schedule_model->get_state_events($slug);
$data['title'] = 'Schedule';
$this->load->view('templates/header', $data);
$this->load->view('schedule/state', $data);
$this->load->view('templates/footer');
}
}
?>
模型(Schedule_model.php):
<?php
class Schedule_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_events($slug = FALSE) {
if ($slug === FALSE) {
$this->db->order_by("start_date", "asc");
$query = $this->db->get('events');
return $query->result_array();
}
$query = $this->db->get_where('events', array('slug' => $slug));
return $query->row_array();
}
public function get_state_events($slug = FALSE) {
if ($slug === FALSE) {
$this->db->order_by("start_date", "asc");
$query = $this->db->get('events');
return $query->result_array();
}
$query = $this->db->get_where('events', array('slug' => $slug));
return $query->row_array();
}
}
?>
路线:
$route['schedule/state/(:any)'] = 'schedule/state/$1';
$route['schedule/state'] = 'schedule/state';
$route['schedule/(:any)'] = 'schedule/view/$1';
$route['schedule'] = 'schedule';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;