cant loop through array with ajax_command_html im beginner in drupal and i would like to ask why is this not looping , it just gives me first result 

$string = $form_state['values']['search_block_form'];

  $query=db_select('node','n');
$query-&gt;join('field_data_body','b' , 'n.nid = b.entity_id');

$query
-&gt;fields('n',array('nid', 'title'))
-&gt;fields('b', array('entity_id', 'body_value'));



  $db_or = db_or();
  $db_or-&gt;condition('n.title', '%' . db_like($string) . '%', 'LIKE');
  $db_or-&gt;condition('b.body_value', '%' . db_like($string) . '%', 'LIKE');
  $query-&gt;condition($db_or);

  $result = $query-&gt;execute();

while ($row = $result-&gt;fetchAssoc()) {
      $node_title = $row['title'];
      $node_id = $row['nid'];
      $entity_id = $row['entity_id'];
      $body_text = $row['body_value']; 


  $commands[] = ajax_command_html("#block-system-main", "&lt;h1&gt; $node_title &lt;/h1&gt; &lt;p&gt; $body_text &lt;/p&gt;"  );
  return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands); 

}


I want replace div with id block-system-main with found content
Try with this: 

  $string = $form_state['values']['search_block_form'];

  $query = db_select('node', 'n');
  $query-&gt;join('field_data_body', 'b', 'n.nid = b.entity_id');

  $query
    -&gt;fields('n', array('nid', 'title'))
    -&gt;fields('b', array('entity_id', 'body_value'));

  $db_or = db_or();
  $db_or-&gt;condition('n.title', '%' . db_like($string) . '%', 'LIKE');
  $db_or-&gt;condition('b.body_value', '%' . db_like($string) . '%', 'LIKE');
  $query-&gt;condition($db_or);

  $results = $query
    -&gt;execute()
    -&gt;fetchAll(PDO::FETCH_ASSOC);

  $commands = [];

  foreach ($results as $result) {
    $node_title = $result['title'];
    $node_id = $result['nid'];
    $entity_id = $result['entity_id'];
    $body_text = $result['body_value'];
    $commands[] = ajax_command_html("#block-system-main", "&lt;h1&gt;" . $node_title . "&lt;/h1&gt; &lt;p&gt;" . $body_text . "&lt;/p&gt;");
  }
  return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);