0

I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes

My View

<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
     <div class="col-sm-3">
         <h5><?php echo $row->categoriesName; ?></h5>
         <ul><li><a href="<?php echo base_url();?>member/detail">
         <?php echo $row->productName; ?></a> </li></ul>
     </div>
<?php endforeach; ?>
</div>
     <!-- product detail -->
     <?php foreach ($detail as $details_new) : ?>
     <div class="box">
         <h1 class="text-center"><?php echo $details_new->productName;?></h1>
         <p class="price"><?php echo $details_new->price;?></p>
     </div>
     <?php endforeach; ?>

My Controller

public function home() {
    $data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
          'username'=> $this->session->userdata('username'),
          'product' => $this->product_model->all(),
          'categories' => $this->categories_model->get_main(),
          'isi'  =>'member/index');    
    $this->load->view('template_home/wrapper',$data);
}
public function detail() {
    $data['username'] = $this->session->userdata('username');
    $data['categories'] = $this->categories_model->get_main();
    $data['detail'] = $this->categories_model->get_details();
    $this->load->view('member/coba',$data);
}

My Model

public function get_main(){
    $this->db->select('product.*,categories.*');
    $this->db->from('product');
    $this->db->join('categories','product.categoriesID=categories.categoriesID');
    $this->db->limit(5);
    $query = $this->db->get();
    if($query->num_rows() > 0) {
        $results = $query->result();
    } return $results;
}       
public function get_details() {
    $query = $this->db->query("SELECT * FROM product WHERE productID");
    $row = $query->row();
}

enter image description here

still can not display a single product

enter image description here

I do not know where my mistake... please help me, thanks

Anonymous
  • 25
  • 1
  • 9

3 Answers3

1

you are iterating details array in wrong format.

public function get_details() {
    $query = $this->db->query("SELECT * FROM product WHERE productID");
    $row = $query->row();
    return $row;
}

This will return only single row without any indexing like - 0,1,2...

and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --

<div class="box">
         <h1 class="text-center"><?php echo $detail->productName; ?></h1>
         <p class="price"><?php echo $detail->price; ?></p>
     </div>

please use this way hope it will work..

Sorav Garg
  • 1,116
  • 1
  • 9
  • 26
0

it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping

<?php if(!empty($detail)){    
     foreach ($detail as $details_new) : ?>
 <div class="box">
     <h1 class="text-center"><?php echo $details_new->productName;?></h1>
     <p class="price"><?php echo $details_new->price;?></p>
 </div>
 <?php endforeach; } ?>
mshoaibdev
  • 123
  • 2
  • 12
0

In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...

     <div class="box">
         <h1 class="text-center"><?php echo $detail->productName;?></h1>
         <p class="price"><?php echo $detail->price;?></p>
     </div>

And In model

public function get_details() {
    $query = $this->db->query("SELECT * FROM product WHERE productID");
    $row = $query->row();
    return $row;
}
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19