title: Looping with while and foreach in PHP
body: <p>I cannot seem to get foreach to work. Maybe I do not understand it correctly.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>$statement = "SELECT * FROM categories ORDER BY name ASC";
  $query = mysql_query($statement)
  
  ...
  ...
  
  $cals = array("sports","general","other","clubs");
  
  foreach ($cals as $value)
  {
  /*  echo "&lt;h3&gt;".$value."&lt;/h3&gt;"; 
  */  echo "&lt;table width='100%'&gt;";
  while ($array = mysql_fetch_array($query))
      {
          if ($array['calendar'] == $value)
          {?&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;?php echo $array['name']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;a onclick="update_form('&lt;?php echo $array['name']; ?&gt;', '&lt;?php echo $array['calendar']; ?&gt;')" href="#"&gt;Edit&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php }
      }
      echo "&lt;/table&gt;&lt;br /&gt;Value: $value";
  }
  </code></pre>
  
  <p>The goal of this is to have the foreach change the if statement. I had planned for the if statement to say: <code>if ($array['calendar'] == "sports")</code> the first time, <code>if ($array['calendar'] == "general")</code> the second time, and so on. However, it shows all of the tables (in the source code), but no table rows are created after the first for each array value. For example, I correctly see the sports table, but I do not see any table rows for general, other, or clubs. There are records in that database that should appear in each of those. Could it be a problem with the while and if statements? If I manually set the $value in the if statement to one of the values in the array, it shows the correct records.</p>
  
  <p>What am I missing?</p>
  
  <p>Sample Data:</p>
  
  <p>in the MySQL database - </p>
  
  <p>categories table.
  fields:</p>
  
  <ul>
  <li>id</li>
  <li>name</li>
  <li>num_events</li>
  <li>calendar</li>
  <li>calendar_url</li>
  </ul>
  
  <p>All of these fields except the calendar field has dummy data in it.</p>
  
  <p>Currently, I have 5 records in there. Each one has a different calendar value. One is sports, one is clubs, and one is general. Depending on what value I place first in the array, it only shows that one table, of all of the values with whatever the first value in the array is.</p>
  
  <p>Here is the source code from the resulting page:</p>
  
  <pre><code>&lt;table width='100%'&gt;&lt;tr&gt;
    &lt;td&gt;test4&lt;/td&gt;
    &lt;td&gt;&lt;a onclick="update_form('test4', 'sports')" href="#"&gt;Edit&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;test5&lt;/td&gt;
  
    &lt;td&gt;&lt;a onclick="update_form('test5', 'sports')" href="#"&gt;Edit&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;&lt;br /&gt;Value: sports&lt;table width='100%'&gt;&lt;/table&gt;&lt;br /&gt;Value: general&lt;table width='100%'&gt;&lt;/table&gt;&lt;br /&gt;Value: other&lt;table width='100%'&gt;&lt;/table&gt;&lt;br /&gt;Value: clubs
  </code></pre>