user876200
user876200

Reputation:

Displays only the last row in foreach statement

          if($query->num_rows()>0)
    {
        foreach($query->result() as $row)
        {
            $slNo   =   $row->sl_no;
            //echo "</br>";
            $sql1 = "SELECT * FROM main_stock_outward WHERE product_id='$slNo' AND request_id='$requestId'";
            //echo "</br>";
            $query1     = $this->db->query($sql1);
            if (!($query1->num_rows() > 0)) 
            {
                echo "hi";
                echo $sql2  =   "SELECT a.sl_no,a.product_name,a.barcode,b.request_qty,b.deliver FROM stock_product a, stock_request b WHERE b.pos_id='$posNum' AND b.ordered_date like '$requestIssueDate%' AND b.request_id='$requestId' AND a.sl_no='$slNo' AND b.deliver='0' AND a.barcode=b.product_barcode ORDER BY request_qty DESC";
                $query2     = $this->db->query($sql2);
                //return $query2;
                $arr_data['query3']=array('sales'   => $query2);
                //$query3   = array(
                //  'sales' => $query2);
            }
        }
        print_r($arr_data['query3']);
        return $query2;
    }

as shown in the code again.I am able to display only the last row it is not able to display the rows ?

Upvotes: 0

Views: 625

Answers (2)

Darius Matulionis
Darius Matulionis

Reputation: 1

Because you are doing it wrong. You are assigning every result to the same variable. Try this instead:

$arr_data[]=array('sales'   => $query2);

and the:

print_r($arr_data);

Upvotes: 0

mahadeb
mahadeb

Reputation: 676

 if($query->num_rows()>0)
{
    foreach($query->result() as $row)
    {
        $slNo   =   $row->sl_no;
        //echo "</br>";
        $sql1 = "SELECT * FROM main_stock_outward WHERE product_id='$slNo' AND request_id='$requestId'";
        //echo "</br>";
        $query1     = $this->db->query($sql1);
        if (!($query1->num_rows() > 0)) 
        {
            echo "hi";
            echo $sql2  =   "SELECT a.sl_no,a.product_name,a.barcode,b.request_qty,b.deliver FROM stock_product a, stock_request b WHERE b.pos_id='$posNum' AND b.ordered_date like '$requestIssueDate%' AND b.request_id='$requestId' AND a.sl_no='$slNo' AND b.deliver='0' AND a.barcode=b.product_barcode ORDER BY request_qty DESC";
            $query2     = $this->db->query($sql2);
            //return $query2;
            $arr_data['query3']**[]** =array('sales'   => $query2);
            //$query3   = array(
            //  'sales' => $query2);
        }
    }
    print_r($arr_data['query3']);
    return $query2;
}

you have missed to make the array, I have modified the line - $arr_data['query3'] =array('sales' => $query2); as $arr_data['query3'][] =array('sales' => $query2);

I think your problem will solve by this.

Upvotes: 1

Related Questions