title: jQuery looping through ajax json response
body: <p>I have a JSON response from PHP like this:</p>
  
  <pre><code>[[{"name":"J1","id":"0088","order":"2928868"}],[{"name":"J2","id":"0171","order":"2928868"}],[{"name":"J2","id":"0249","order":"2928868"}]]
  </code></pre>
  
  <p>What I want to do with this is out output the order number which is always the same in an element with an id of #ordernum.</p>
  
  <p>Then for the other parts, i want to create a couple of divs within a container div for the name and id.</p>
  
  <p>I'm pretty sure I can get that bit working but what i'm unsure of is how to loop through each of the JSON rows to get to the part where i can create my divs</p>
  
  <p>This is what I have so far but this appends to a div where i want to create new divs.</p>
  
  <p>Would i need to do another $.each() after $('#ordernum').text(row[0].order);?</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "check.php",
      data: { order: order },
      dataType: "json",
      cache: false,
      success: function(response) {
  
          if(response.length != 0) {
              console.log(response.order);
              $.each(response, function(index, row) {
  
                  console.log(row);
                  $('#ordernum').text(row[0].order);
  
                  $('.name').append(row[0].name+"&lt;br /&gt;");
                  $('.id').append(row[0].id+"&lt;br /&gt;");
              });
          }
          else {
              $('#foo').text('Order number not found!!');
          }
  
          // set the focus to the order input
          $('#order').focus().val('');
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
          console.log('An Ajax error was thrown.');
          console.log(XMLHttpRequest);
          console.log(textStatus);
          console.log(errorThrown);
      }
  });
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is how the json is created.  $rows is from a query from a SQLSERVER database and I loop through that to get all the rows from the MySQL database</p>
  
  <pre><code>foreach($rows as $row) {
  
      $sql = 'SELECT bin, id FROM totebin WHERE id= :id';
  
      $stmt = $db-&gt;prepare($sql);
      $stmt-&gt;bindValue(':tote', trim($row['id']));
      $stmt-&gt;execute();
  
      $array = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
      $array[0]['id'] = str_pad($array[0]['id'], 4, '0', STR_PAD_LEFT);
      $array[0]['order'] = trim($row['order']);
  
      $output[] = $array;
  }
  
  echo json_encode($output);
  </code></pre>