title: Warning: Invalid argument supplied for foreach()
body: <p>i have this <code>MySQL</code> table for news categories: </p>
  
  <p><code>id - name - parent</code></p>
  
  <p>1:
  now I  need to list parent/child categories into <code>&lt;optgroup&gt;&lt;optgroup&gt;</code>.</p>
  
  <p>PHP CODE:</p>
  
  <pre><code>$options    = array();
  $categories = mysql_query("
    SELECT 
      id, name, p.name AS parent 
    FROM 
      " . CATS . " AS child
    INNER JOIN
      " . CATS . " AS p ON p.id = child.parent
  ");
  foreach($categories as $category) { // LINE 734
    $parent = $category['parent'];
    if (! isset($options[$parent])) $options[$parent] = array();
    $options[$parent][$category['id']] = $category['name'];
  }
  echo "&lt;select&gt;";
  foreach($options as $group =&gt; $option) {
    printf('&lt;optgroup label="%s"&gt;', $group);
    foreach ($option as $id =&gt; $label) {
      printf('&lt;option value="%s"&gt;%s&lt;/option&gt;', $id, $label);
    }
    printf('&lt;/optgroup&gt;');
  }
  echo "&lt;/select&gt;";
  </code></pre>
  
  <p>PROBLEM: </p>
  
  <p><code>Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\cms\news.php on line 734</code></p>
  
  <p>UPDATE: I test with <code>$categories</code> <code>array</code> and see my code worked. if $categories MySQL result Like this:</p>
  
  <pre><code>$categories = array(
    array('id' =&gt; 1, 'name' =&gt; 'Label 1', 'parent' =&gt; 'Parent 1'),
    array('id' =&gt; 2, 'name' =&gt; 'Label 2', 'parent' =&gt; 'Parent 2'),
    array('id' =&gt; 3, 'name' =&gt; 'Label 3', 'parent' =&gt; 'Parent 1'),
    array('id' =&gt; 4, 'name' =&gt; 'Label 4', 'parent' =&gt; 'Parent 1'),
    array('id' =&gt; 5, 'name' =&gt; 'Label 5', 'parent' =&gt; 'Parent 1'),
    array('id' =&gt; 6, 'name' =&gt; 'Label 6', 'parent' =&gt; 'Parent 3'),
    array('id' =&gt; 7, 'name' =&gt; 'Label 7', 'parent' =&gt; 'Parent 8'),
    array('id' =&gt; 8, 'name' =&gt; 'Label 8', 'parent' =&gt; 'Parent 4'),
  );
  </code></pre>
  
  <p>How To Fix My Problem!?</p>
  
  <p>2: in update news page,I need To select News cats in this menu. My mean is: if <code>$newscatid = $catid</code> selected Option.(<code>option value = $newscatid</code>) How To update my code for this?</p>