10/10/2018, 10:54

Cần giúp đỡ codeigniter

Mình có:
+ Table: categories ++>> cat_id, cat_name
+ Table: products ++>> cat_id, prod_id, prod_name

Mình muốn dùng codeigniter để làm được dạng:

Chuyên mục 1 (cat_id = 1)
+ Sản phẩm 1 (prod_id = 1 & cat_id = 1)
+ Sản phẩm 2 (prod_id = 2 & cat_id = 1)

Chuyên mục 2 (cat_id = 2)
+ Sản phẩm 3 (prod_id = 3 & cat_id = 2)
+ Sản phẩm 5 (prod_id = 5 & cat_id = 2)

Chuyên mục 3 (cat_id = 3)
+ Sản phẩm 6 (prod_id = 6 & cat_id = 3)
+ Sản phẩm 7 (prod_id = 7 & cat_id = 3)

........... n

Tức là mình muốn có 1 index và hiển thị hết chuyên mục & trong chuyên mục sẽ có sản phẩm của chuyên mục đó.

Rất mong bạn nào biết codeigniter viết hộ mình đoạn mã này với !

Thân...
Cảm ơn nhiều nhiều
1024KB viết 12:59 ngày 10/10/2018
PHP Code:
//home_view.php code
foreach($cats_info->result() as $cat_info){
   echo 
anchor('link/'.$cat_info->cat_id$cat_info->cat_name);
   
//call function show_product($cat_id) from product_helper.php
   
show_product($cat_info->cat_id);
}

//product_helper.php code
function show_product($cat_id){
   
$CI = & get_instance();
   
$prods_info $CI->db->get_where('product', array('cat_id' => $cat_id);
   foreach(
$prods_info->result() as $prod_info){
      echo 
anchor('view/'.$prod_info->prod_id.'/'.$cat_id'+ '.$prod_info->prod_name.'<br/>');
   }

Chắc là controller + model thì tự viết được. Cái này gợi ý thôi. Thử nghĩ cách khác hay hơn xem.
minhtuan242 viết 13:05 ngày 10/10/2018
Model

<?php
class Site_model extends Model{
function Site_model()
{
parent::Model();
}

function get_records(){
$this->db->order_by('id', 'desc');
$query = $this->db->get('catagories');
return $query->result();
}
}
?>

Controllers

<?php
class Home extends Controller
{
function index()
{
$this->load->model('site_model');

if($query = $this->site_model->get_records())
{
$data['records'] = $query;
}

$this->load->view('include/contents', $data);
}
}
?>

View

<?php foreach($records as $rows): ?>
<h1>Catagories: <?=$rows->name?></h1>
<?php
$entries = $this->db->get_where('entries', array('cat_id'=>$rows->id));
foreach($entries->result() as $entries_row):
?>
<h2><?=$entries_row->Title?></h2>
<p><?=$entries_row->body?></p>
<?
endforeach;
?>
<?php endforeach; ?>

Em cũng mới kiếm được 1 cách như thế này chia sẻ luôn cho ae nào cần xài ..

Thak 1024KB nhiều nhiều
haicop viết 13:05 ngày 10/10/2018
codeigniter là cái gì thế bác ?
minhtuan242 viết 12:56 ngày 10/10/2018
Được gửi bởi haicop
codeigniter là cái gì thế bác ?
http://codeigniter.com/
minhtuan242 viết 12:59 ngày 10/10/2018
Được gửi bởi haicop
codeigniter là cái gì thế bác ?
ngon rồi chứ .. hehehêhhee

[=========> Bổ sung bài viết <=========]

[QUOTE=1024KB;2397121]
PHP Code:
//home_view.php code
foreach($cats_info->result() as $cat_info){
   echo 
anchor('link/'.$cat_info->cat_id$cat_info->cat_name);
   
//call function show_product($cat_id) from product_helper.php
   
show_product($cat_info->cat_id);
}

//product_helper.php code
function show_product($cat_id){
   
$CI = & get_instance();
   
$prods_info $CI->db->get_where('product', array('cat_id' => $cat_id);
   foreach(
$prods_info->result() as $prod_info){
      echo 
anchor('view/'.$prod_info->prod_id.'/'.$cat_id'+ '.$prod_info->prod_name.'<br/>');
   }

Chắc là controller + model thì tự viết được. Cái này gợi ý thôi. Thử nghĩ cách khác hay hơn xem.
Bài liên quan
0