我不知道该怎么办,我的索引页不起作用...我的意思是在我添加已经连接到数据库的功能滑块之前它完美运行......我该怎么办?
遇到未捕获的异常
类型:解析错误
消息:语法错误,意外的"endforeach"(T_ENDFOREACH(, 预期文件结束
文件名: C:\xampp\htdocs\eat\application\views\front\index.php
行号:96
回溯:
文件: C:\xampp\htdocs\eat\application\controllers\index.php 行: 15 功能:查看
文件: C:\xampp\htdocs\eat\index.php 行: 315 功能: require_once
查看:索引.php
<!--- Carousel -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php foreach ($slider as $slider) ?>
<div class="item-active">
<img src="<?php echo base_url('upload/banner/'.$banner->image)?>">
<div class="carousel-caption">
<h3><?=$slider->name?></h3>
<p><?=$slider->description?></p>
</div>
</div>
<div class="item">
<img src="<?php echo base_url('upload/banner/'.$banner->image)?>">
<div class="carousel-caption">
<h3><?=$slider->name?></h3>
<p><?=$slider->dekripsi?></p>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only"> previous</span>
</a>
<a class="right carousel-control" href="=#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only"> next </span>
</a>
</div>
<?php endforeach; ?>
控制器:索引.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Index extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("Indexweb_model");
$this->load->library('form_validation');
}
public function index()
{
$data["slider"] = $this->Indexweb_model->getAll();
$this->load->view("front/index", $data);
}
}
型号: Indexweb_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Indexweb_model extends CI_Model
{
private $_table = "slider";
public $id_slider;
public $name;
public $deskripsi;
public $image;
public function rules()
{
return [
['field' => 'name',
'label' => 'Name',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_slider" => $id])->row();
}
public function save()
{
$post = $this->input->post();
// $this->id_slider = uniqid();
$this->name = $post["name"];
$this->deskripsi = $post["deskripsi"];
$this->image = $this->_uploadImage();
$this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->id_slider = $post["id"];
$this->name = $post["name"];
$this->deskripsi = $post["deskripsi"];
if (!empty($_FILES["image"]["name"])) {
$this->image = $this->_uploadImage();
} else {
$this->image = $post["old_image"];
}
$this->db->update($this->_table, $this, array('id_slider' => $post['id']));
}
public function delete($id)
{
$this->_deleteImage($id);
return $this->db->delete($this->_table, array("id_slider" => $id));
}
private function _uploadImage()
{
$config['upload_path'] = './upload/slider/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_max_filesize'] = '100000M';
$config['post_max_size'] = '100000M';
$config['file_name'] = basename($_FILES["image"]["name"]);
$config['overwrite'] = true;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
}
private function _deleteImage($id)
{
$slider = $this->getById($id);
if ($slider->image != "default.jpg") {
$filename = explode(".", $slider->image)[0];
return array_map('unlink', glob(FCPATH."upload/slider/$filename.*"));
}
}
}
你的语法是错误的。 您缺少冒号,并且您的endforeach
在错误的位置。
试试这个:
<!--- Carousel -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php foreach ($slider as $slider): ?>
<div class="item-active">
<img src="<?php echo base_url('upload/banner/'.$banner->image)?>">
<div class="carousel-caption">
<h3><?=$slider->name?></h3>
<p><?=$slider->description?></p>
</div>
</div>
<div class="item">
<img src="<?php echo base_url('upload/banner/'.$banner->image)?>">
<div class="carousel-caption">
<h3><?=$slider->name?></h3>
<p><?=$slider->dekripsi?></p>
</div>
</div>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only"> previous</span>
</a>
<a class="right carousel-control" href="=#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only"> next </span>
</a>
</div>
这是正确的 foreach 结构,在您的代码中缺少 ":">
<?php
foreach($array as $element):
#do something
endforeach;
?>