我在数据库中有两个表::: 1. tbl_category 2. tbl_product。 显示主页中的所有类别列表,当用户点击signle类别时,它会按类别ID(包含在tbl_product中的类别ID)转到产品页面(来自tbl_product)
问题:::我想在此处显示类别名称和图像。这样用户就可以看到他们点击了哪个类别(来自tbl_category):::我怎样才能得到它们?请帮帮我
class Home extends CI_Controller{
//put your code here
public function __construct() {
parent::__construct();
$this->load->model('home_model');
}
public function index(){
$data=array();
$data['result']=$this->home_model->selectCategory($config['per_page'], $this->uri->segment(3));
$data['maincontent']=$this->load->view('home_message',$data,TRUE);
$this->load->view('home', $data);
}
public function category($category_id){
$data=array();
$data['result']=$this->home_model->selectProductByCategoryId($category_id);
$data['maincontent']=$this->load->view('category_detail',$data,TRUE);
$this->load->view('home', $data);
}
class Home_Model extends CI_Model{
//put your code here
public function selectCategory($per_page, $offset)
{
$this->db->select('*');
$this->db->from('tbl_category');
$this->db->order_by("category_id", "desc");
$query = $this->db->get('', $per_page, $offset);
foreach ($query->result() as $row)
$data[] = $row;
return $data;
}
public function selectProductByCategoryId($category_id)
{
$this->db->select('*');
$this->db->from('tbl_product');
$this->db->where('category_id',$category_id);
//$this->db->order_by("product_id", "desc");
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}
我想在此处显示名称和图像类别。这样用户就可以看到他们点击了哪个类别::::
<?php
foreach ($result as $values)
{
?>
<div>
<div>
<a href="<?php echo base_url(); ?>product_category/product_detail/<?php echo $values->product_id ?>"><img src="<?php echo base_url();?><?php echo $values->product_image ?>" width="90%" height="220" /></a>
</div>
<div>
<b><a href="<?php echo base_url(); ?>product_category/product_detail/<?php echo $values->product_id ?>"><?php echo $values->product_name ?></a></b> <br>
<b>Price: <?php echo $values->product_price ?></b> <br>
<a href="<?php echo base_url(); ?>product_category/product_detail/<?php echo $values->product_id ?>" class="a-btn">Order Now</a>
</div>
</div>
<?php } ?>
答案 0 :(得分:1)
控制器
public function category($category_id){
$data=array();
$data['result'] = $this->home_model->selectProductByCategoryId($category_id);
// Added this line
$data['category'] = $this->home_model->selectCategoryByd($category_id);
$data['maincontent'] = $this->load->view('category_detail',$data,TRUE);
$this->load->view('home', $data);
}
Model_Home
public function selectCategoryById($category_id)
{
$result = $this->db->select('*')
->from('tbl_category')
->where('id',$category_id)
->get()
->result();
return $result;
}
然后在你看来回应一些。
您使用Model_Home
进行其他模型功能?
模型用于数据以及与数据有关的所有内容。您应该Model_Category
具有selectById
功能,Model_Product
具有selectByCategoryId
和selectById
功能。模型不用于描述页面。
请阅读MVC和CodeIgniters implementation of it。然后重构您的代码并正确使用codeigniters功能,如果正确实现,您甚至不必自己编写这些功能!注意:我不太了解CI,所以我不知道例如具体的实现。 MVC,ORM等