1

Hello guys I just want to ask if how can I get the value of JSON using a loop? I already did some research but my code wont work. I didn't know what's my error. What i want is get all the json value and put it in a table. By the way i am using codeigniter

My model:

public function getAllPurchaseOrder(){

        $array = array();
        $sqlSelectAll = "select * from po_order
                         inner join po_supplier on po_order.poid = po_supplier.poid
                         inner join po_account_details on po_supplier.poid = po_account_details.poid";
        $resultSelectAll = $this->db->query($sqlSelectAll);
        foreach($resultSelectAll->result_array() as $details){
            $array[] = $details;
        }
        return "{\"data\":" .json_encode($array). "}";

}

My controller:

  public function index(){

        $data['title'] = "Welcome";
        $data['copyright'] = date('Y');
        $data['purchased_comment'] = $this->po_model->fetch_purchased_refid(); 
        $data['link'] = "PO";
        $data['itemlist'] = $this->po_model->getAllPurchaseOrder();            
        $this->load->view('common/header_common',$data);
        $this->load->view('common/navigation');
        $this->load->view('Purchase Order/contents/index',$data);
        $this->load->view('common/footer_common',$data);

    }

Here's my code:

echo "<pre>";
var_dump(json_decode($itemlist,true));

My json output using var_dump(json_encode)

  array(1) {
  ["data"]=>
  array(1) {
    [0]=>
    array(25) {
      ["poid"]=>
      string(1) "1"
      ["itemid"]=>
      string(1) "3"
      ["item_desc"]=>
      string(6) "Drinks"
      ["item_qty"]=>
      string(1) "4"
      ["item_price"]=>
      string(4) "5.00"
      ["total_amount"]=>
      string(5) "20.00"
      ["cash_on_delivery"]=>
      string(1) "Y"
      ["is_check"]=>
      string(1) "Y"
      ["bank_transfer"]=>
      string(1) "N"
      ["transaction_date"]=>
      string(19) "2013-08-15 15:44:30"
      ["posupid"]=>
      string(1) "1"
      ["spid"]=>
      string(1) "2"
      ["spname"]=>
      string(12) "MILO COMPANY"
      ["spaddress"]=>
      string(6) "Manila"
      ["contact_person"]=>
      string(4) "None"
      ["tin"]=>
      string(12) "1232-456-789"
      ["contact_number"]=>
      string(7) "0912151"
      ["poaccid"]=>
      string(1) "1"
      ["payee_name"]=>
      string(6) "Larren"
      ["bank_details"]=>
      string(11) "BDO Account"
      ["account_name"]=>
      string(7) "Premium"
      ["account_number"]=>
      string(5) "54564"
      ["prepared"]=>
      string(5) "Admin"
      ["checked"]=>
      string(5) "Admin"
      ["approved"]=>
      string(5) "Admin"
    }
  }

My problem is I can't get the value of the json. Let's say for example i want the value of poid but i dont know how. I tried some array but wont work

here's my code:

$items = json_decode($itemlist,true);
echo $items['poid'];
3
  • can you print your json string here? Commented Aug 16, 2013 at 6:40
  • ok wait i am using codeigniter Commented Aug 16, 2013 at 6:41
  • print value of $itemlist only. Commented Aug 16, 2013 at 6:44

4 Answers 4

1

Use this code:

$arr = json_decode($itemlist,true);
echo $arr['data'][0]['poid'];
Sign up to request clarification or add additional context in comments.

Comments

1

Previous answers will only get you the value of the first element, I assume you want to loop through all the elements that result from your query.

First off you can simplify your model function to:

public function getAllPurchaseOrder(){
        $sqlSelectAll = "select * from po_order
                         inner join po_supplier on po_order.poid = po_supplier.poid
                         inner join po_account_details on po_supplier.poid = po_account_details.poid";
        $resultSelectAll = $this->db->query($sqlSelectAll);
        return json_encode($resultSelectAll->result_array());
}

Now to loop through the data in your view you should be familiar with foreach to loop through all elements in an array.

$items = json_decode($itemlist,true); //passing true decodes into associative array

foreach($items as $item){
    echo 'poid: '.$item['poid'];
}

Passing true to json_decode() returns all objects converted to arrays, if you wanted to work with objects you would just need to use -> to access the object properties, ex: $item->poid

Comments

1

How about?

echo $items['data'][0]['poid']

EDIT: loop items

$data = $items['data'];    
foreach($data as $item){
    echo 'poid: '.$item['poid'];
}

3 Comments

should be $items['data'][0]['poid']
how can i do this in array?
@rochellecanale you'd need to change the 0 to the index value you're after or alternatively loop using a foreach.
0

A JSON string when decoded it will be converted to a Javascript object and you can transverse its properties with the dot notation or like a dictionary.

So for example if your JSON string is something like:

var myJSONString = "{'data':['a', 'b', 'c'] }";
var obj = JSON.parse(myJSONString);

you can retrieve the value of 'a', 'b' or 'c' by either using

var a  = obj.data[0];

or

var a = obj['data'][0];

if obj.data were not an array of strings but other objects you would follow the above notation to retrieve nested values.

e.g.

var myJSONString = "{'data':['a':[2,4,6], 'b':[1,3,5], 'c':[1,2,3,5,8]] }";
var obj = JSON.parse(myJSONString);

var firstOfA = obj['data']['a'][0];

or

var firstOfA= obj.data.a[0];

2 Comments

Sorry but I have to downvote because the question is not related to javascript, I already removed the tags that were erroneously added by the OP
I do not mind and I see it is not relevant after the edits, although must be said that the original question was tagged with Javascript, JQuery and Json only. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.