0

I've this column in database table:

value=[
       {"srno":1,
        "name":"Gaspari Aminolast ",
        "quantity":"2",
        "price":"2920.0000",
        "total_bill":5840
       },
       {"srno":2,
        "name":"Gaspari Amino Max ",
         "quantity":"2",
         "price":"2640.0000",
         "total_bill":5280
       },
       {"srno":3,
        "name":"Myofusion 10Lbs",
        "quantity":"2",
        "price":"8400.0000",
        "total_bill":16800}
]

And my php code is:

<?php
    $getbill="select value from $tbl where bill_id='$_GET[id]' ";
    $result=mysql_query($getbill)or die(mysql_error());
    $results = array();
    while($row=mysql_fetch_array($result))
    {
         $results[] = json_decode($row['value']);
    }
    print_r($results);
    foreach($results as $key=>$value){
?>
<tbody>
    <tr>
        <td><?php echo $value['srno'];?></td>
        <td><?php echo $value['name'];?></td>
        <td><?php echo $value['quantity'];?></td>
        <td><?php echo $value['price'];?></td>
        <td ><?php echo $value['total_bill'];?></td>
    </tr>
</tbody>
<?PHP   
    }
?>

I'm confused with how I loop through this and print all it contains.

print_r() :

Array ( 
      [0] => Array ( 
         [0] => stdClass Object ( 
                       [srno] => 1 
                       [name] => Gaspari Aminolast 
                       [quantity] => 2 
                       [price] => 2920.0000 
                       [total_bill] => 5840 
                  ) 
          [1] => stdClass Object ( 
                       [srno] => 2 
                       [name] => Gaspari Amino Max 
                       [quantity] => 2 
                       [price] => 2640.0000 
                       [total_bill] => 5280 
                  ) 
          [2] => stdClass Object ( 
                       [srno] => 3 
                       [name] => Myofusion 10Lbs 
                       [quantity] => 2 
                       [price] => 8400.0000 
                       [total_bill] => 16800 
                   ) 
       )
)
3
  • show the result of print_r(). Wrap it in <pre> tags. Commented Aug 26, 2015 at 13:02
  • Do you print these records in a HTML <table> object? Also, normally a table has one body and will be filled with multiple rows. Commented Aug 26, 2015 at 13:04
  • print_r() : Array ( [0] => Array ( [0] => stdClass Object ( [srno] => 1 [name] => Gaspari Aminolast [quantity] => 2 [price] => 2920.0000 [total_bill] => 5840 ) [1] => stdClass Object ( [srno] => 2 [name] => Gaspari Amino Max [quantity] => 2 [price] => 2640.0000 [total_bill] => 5280 ) [2] => stdClass Object ( [srno] => 3 [name] => Myofusion 10Lbs [quantity] => 2 [price] => 8400.0000 [total_bill] => 16800 ) ) ) Commented Aug 26, 2015 at 13:05

3 Answers 3

1

use second argument in json_decode function,set it to TRUE to get as array

$json='[{"srno":1,"name":"Gaspari Aminolast ","quantity":"2","price":"2920.0000","total_bill":5840},{"srno":2,"name":"Gaspari Amino Max ","quantity":"2","price":"2640.0000","total_bill":5280},{"srno":3,"name":"Myofusion 10Lbs","quantity":"2","price":"8400.0000","total_bill":16800}]';


echo '<pre>';print_r(json_decode($json,TRUE));

output:

Array
(
    [0] => Array
        (
            [srno] => 1
            [name] => Gaspari Aminolast 
            [quantity] => 2
            [price] => 2920.0000
            [total_bill] => 5840
        )

    [1] => Array
        (
            [srno] => 2
            [name] => Gaspari Amino Max 
            [quantity] => 2
            [price] => 2640.0000
            [total_bill] => 5280
        )

    [2] => Array
        (
            [srno] => 3
            [name] => Myofusion 10Lbs
            [quantity] => 2
            [price] => 8400.0000
            [total_bill] => 16800
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Why, whats wrong with Objects. You have to assume that if they were put on the database as Objects there was a good reason.
@RiggsFolly Nothing wrong with object,its good to use objects ,just answered how a json string decoded to array only.
0

The array your trying to loop over seems to be in another array. Try this or @RiggsFolly's answer

<table>
<?php
    $getbill="select value from $tbl where bill_id='$_GET[id]' ";
    $result=mysql_query($getbill)or die(mysql_error());
    $results = array();
    while($row=mysql_fetch_array($result))
    {
         $results = json_decode($row['value']);
    }
    print_r($results);
    foreach($results as $key=>$value){
?>

    <tr>
        <td><?php echo $value['srno'];?></td>
        <td><?php echo $value['name'];?></td>
        <td><?php echo $value['quantity'];?></td>
        <td><?php echo $value['price'];?></td>
        <td ><?php echo $value['total_bill'];?></td>
    </tr>

<?PHP   
    }
?>
</table>

8 Comments

I think you missed the inner array of objects.
The inner array is brought outside by losing the [] in the while loop. The mysql result once json_decoded is the whole array he is trying to output and by using the [] he put them in another array
Yea, it also looses all but the last json_decode($row['value']); in the result set loop
Yes but purely based on his printr, there is only one record. This is why I said to try either of our answers because mine assumes his mistake was to treat a single result as an array and yours assumes his mistake was to not dig into the array enough.
So remove the while loop. Although the OP may have just reduced the actual data for the sake of brevity. Remember the old adage, assumption is the mother of all &^%$£""!
|
0

If you notice, now I reformatted the value column, each value column contains a JSON string denoting an array of objects.

So for each value you store in the $results array you neeed to do another loop to examine the inner array of objects. Like so:

<?php
    $getbill="select value from $tbl where bill_id='{$_GET[id]}' ";
    $result=mysql_query($getbill)or die(mysql_error());
    $results = array();
    while($row=mysql_fetch_array($result))
    {
         $results[] = json_decode($row['value']);
    }
    print_r($results);

    echo '<tbody>';

    foreach($results as $value) :
        foreach( $value as $object) :
?>
    <tr>
        <td><?php echo $object->srno;?></td>
        <td><?php echo $object->name;?></td>
        <td><?php echo $object->quantity;?></td>
        <td><?php echo $object->price;?></td>
        <td><?php echo $object->total_bill;?></td>
    </tr>

<?php   
        endforeach;
    endforeach;
?>
</tbody>

I also moved the <tbody> and </tbody> outside the loop as that would also have caused a problem with your layout.

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.