Warning: Invalid argument supplied for foreach()

407
January 18, 2017, at 00:37 AM

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();
}

still can not display a single product

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

Answer 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..

Answer 2

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; } ?>
Answer 3

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;
}
Rent Charter Buses Company
READ ALSO
PHP - Not showing all rows from database

PHP - Not showing all rows from database

I have the following code which is supposed to get several records from the databaseWhen the query is ran in phpmyadmin it displays the rows fine, but on the page it only displays the first item

306
How to build a mySQL query with three different tables with timespan, summarizing and grouping of values

How to build a mySQL query with three different tables with timespan, summarizing and grouping of values

My first question here and with very little knowledge of programming so please bear with me

240
LIMT query to 1 if selcted two tables

LIMT query to 1 if selcted two tables

How to limit query to 100 if i have selected data from two tables

235
in qt how can i run two query sucessfully?

in qt how can i run two query sucessfully?

How can I run two query if I run one query is executed successfully then the second query must also be executed else the value changed by first query should be reverted?

423