1

This is my code to get product details from database..

$i=0;
        foreach($res->result() as $row ){



            $products=json_decode($row->product_name,1);
            //var_dump($products);
            /*$sess_products[$i]['product_id'] = $row->product_id;
            $sess_products[$i]['product_name'] = $row->product_name;
            $sess_products[$i]['quantity'] = $row->quantity;
            $sess_products[$i]['unit'] = $row->unit;
            $sess_products[$i]['unit_rate'] = $row->unit_rate;

            $this->session->set_userdata('sess_products',$sess_products);*/
            //$post_array['cart']=$this->session->userdata('sess_products');
            echo "<tr>";
            echo "<td><input type='hidden'  style='width:80%;'  value='".$products[$i]['product_id']."' name='product_id[]'/></td>";
            echo "<td><input type='hidden'  style='width:80%;'  value='".$products[$i]['product_name']."' name='product_name[]'/></td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td style='width:40%;'>".$products[$i]['product_name']."</td>";

            echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['quantity']."' name='quantity[]'/></td>";
            echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['unit']."' name='unit[]'/></td>";
            echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['unit_rate']."' name='unit_rate[]'/></td>";
            echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
            echo "</tr>";
            $i++;

        }

Now I am able to display first item in json string by decoding it. but I want to display whole records in foreach loop.? So what will be the error??

Above code display only first record from that array.

7
  • 2
    Why you doing $products=json_decode($product); inside the foreach loop? Did you try it outside the loop? Commented Feb 2, 2015 at 5:44
  • if you want array, why dont use $res->result_array(); Documentation here Commented Feb 2, 2015 at 5:49
  • Does $row represent a JSON or $row->product_name? $row->product_name looks more like a string column of name, not the whole product details. Commented Feb 2, 2015 at 5:58
  • give us the EXACT var_dump of $product. Commented Feb 2, 2015 at 6:01
  • The issue seems to be with code ignitor, which version are you using? Commented Feb 2, 2015 at 6:14

4 Answers 4

2

I guess this is working,

<?php

$product = '[{"product_id":"1","product_name":"Apple iMac","quantity":"32","unit":"23","unit_rate":"32"},{"product_id":"5","product_name":"Nokia E5","quantity":"543","unit":"543","unit_rate":"543"},{"product_id":"8","product_name":"Zinc Sulphate 500 ml","quantity":"5443","unit":"434","unit_rate":"5333"}]';

$products = json_decode($product, true);

print_r($products);


Output:

Array
(
    [0] => Array
        (
            [product_id] => 1
            [product_name] => Apple iMac
            [quantity] => 32
            [unit] => 23
            [unit_rate] => 32
        )

    [1] => Array
        (
            [product_id] => 5
            [product_name] => Nokia E5
            [quantity] => 543
            [unit] => 543
            [unit_rate] => 543
        )

    [2] => Array
        (
            [product_id] => 8
            [product_name] => Zinc Sulphate 500 ml
            [quantity] => 5443
            [unit] => 434
            [unit_rate] => 5333
        )

)


Demo:
http://3v4l.org/5D3qe


EDIT:

foreach($res->result() as $row)
{
    $products = json_decode($row->product_name, true);

    foreach($products as $prod)
    {
        echo "<tr>";
        echo "<td><input type='hidden'  style='width:80%;'  value='".$prod['product_id']."' name='product_id[]'/></td>";
        echo "<td><input type='hidden'  style='width:80%;'  value='".$prod['product_name']."' name='product_name[]'/></td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td style='width:40%;'>".$prod['product_name']."</td>";

        echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['quantity']."' name='quantity[]'/></td>";
        echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['unit']."' name='unit[]'/></td>";
        echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['unit_rate']."' name='unit_rate[]'/></td>";
        echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
        echo "</tr>";
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

As per your updated question, check the edits in the answer. You'll need another foreach loop.
Cool. Mind closing the answer as accepted for future use to others. :D
Okay you need a flag value as well. I didn't know that. You can accept your answer as well :)
0

Use

json_decode($product, true);

To get as an array

1 Comment

What is your PHP version?
0

Try trimming:

foreach($res->result() as $row ){
                $product=$row->product_name;
                $products=json_decode(trim($product), 1);
                print_r($products);
}

Tried:

var_dump(json_decode('[{"product_id":"1","product_name":"Apple iMac","quantity":"32","unit":"23","unit_rate":"32"},{"product_id":"5","product_name":"Nokia E5","quantity":"543","unit":"543","unit_rate":"543"},{"product_id":"8","product_name":"Zinc Sulphate 500 ml","quantity":"5443","unit":"434","unit_rate":"5333"}]'));

UPDATE:

Not sure but, it can be this bug. I can see the white space between Apple iMac. One option left is to try updating PHP, this might be too much.

Try test cases from the bug link:

Test script:

<?

function json_cmp($x, $y)
{
  print var_dump(json_decode($x) === $y);
}

// works
json_cmp("true", true);

// fails - is actually true
json_cmp("tRue", NULL);

// fails - is actually NULL
json_cmp("true ", true);

// works
json_cmp("[true ] ", array(true));

// works, even though the non-array version fails
json_cmp("[tRue]", NULL);

?>

Expected result:

true * 5

Actual result:

bool(true) bool(false) bool(false) bool(true) bool(true)

4 Comments

I manually tried decoding the string you mentioned in your question, that is working. So just a guess.
and try var_dump instead, print_r is specific to arrays.
Sorry I tried your manual code but It again display json string instead of array or object..
strange, confirm if extension is enabled in phpinfo();, just run this code in php tag. and look for json. are you on ubuntu ?
0

This is my working code..

$i=0;

        foreach($res->result() as $row ){


            $j=0;
            $products=json_decode($row->product_name,1);
            foreach($products as $row2){


            $sess_products[$j]['product_id'] = $row2['product_id'];
            $sess_products[$j]['product_name'] = $row2['product_name'];
            $sess_products[$j]['quantity'] = $row2['quantity'];
            $sess_products[$j]['unit'] = $row2['unit'];
            $sess_products[$j]['unit_rate'] = $row2['unit_rate'];

            $this->session->set_userdata('sess_products',$sess_products);
            //print_r($sess_products);
            //$post_array['cart']=$this->session->userdata('sess_products');
            echo "<tr>";
            echo "<td><input type='hidden'  style='width:80%;'  value='".$row2['product_id']."' name='product_id[]'/></td>";
            echo "<td><input type='hidden'  style='width:80%;'  value='".$row2['product_name']."' name='product_name[]'/></td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td style='width:40%;'>".$row2['product_name']."</td>";


            echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['quantity']."' name='quantity[]'/></td>";
            echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['unit']."' name='unit[]'/></td>";
            echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['unit_rate']."' name='unit_rate[]'/></td>";
            echo "<td><a href='javascript:void(0)' rownum='".$j."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
            echo "</tr>";
            $j++;

        }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.