title: foreach loop to populate multiple radio buttons from a database in a php string
body: <p>UPDATE: 
  Thanks to the answers provided by SeanWM and Styphon, I offer the full, working code to those who are looking for a similar answer:</p>
  
  <pre><code>&lt;? 
  if (isset($_GET['id'])) {
  include "connect_to_mysql.php";
  $id= preg_replace('#[^0-9]#i','',$_GET['id']);
  $sql=mysql_query("SELECT * FROM angler3 WHERE id='$id' LIMIT 1");
  $clientCount=mysql_num_rows($sql);
  
      if($clientCount&gt;0){ 
  
   while($row=mysql_fetch_array($sql)){
   $anglerAvidity=$row["anglerAvidity"];
   $anglerFishMode=$row["anglerFishMode"];
  
   $vals = array('novice', 'occasional', 'avid');
   $radio_opts = "";
  foreach ($vals as $val)
  {
  $checked = ($val==$anglerAvidity) ? ' checked="checked"' : '';
  $radio_opts .= "&lt;input type=\"radio\" name=\"anglerAvidity\" value=\"{$val}\" {$checked}&gt; {$val}\n";
  
  }
  $vals2 = array('boat', 'kayak or canoe', 'float tube', 'on ice', 'rod and reel', 'trap nets', 'other');
  $radio_opts2 = "";
  foreach ($vals2 as $val2)
  {
  $checked2 = ($val2==$anglerFishMode) ? ' checked="checked"' : '';
  $radio_opts2 .= "&lt;input type=\"radio\" name=\"anglerFishMode\" value=\"{$val2}\"{$checked2}&gt; {$val2}\n";
  }
  $content='
  &lt;h3&gt;Please click on any buttons that are not answered. Your responses are shown below, and new or updated responses will be saved. &lt;/h3&gt;
  &lt;hr&gt;
  &lt;form enctype="multipart/form-data" action="thankyouAnglerYouRevise.php?id='.$id.' " method="POST"&gt; 
  &lt;table&gt;
  &lt;tr &gt;&lt;td&gt;I consider myself a/an:&lt;/td&gt;
  &lt;td colspan="2" align=left&gt;'.$radio_opts .'
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td colspan="3" style="border-bottom: 1px solid #BBB; height: 10px;"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr &gt;&lt;td colspan="3" &gt;My normal mode of fishing is:&lt;/td&gt;&lt;/tr&gt;
  &lt;tr &gt;&lt;td colspan="3"&gt;'.$radio_opts2.'
  &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;br&gt;
  &lt;input class="button" value="Submit" type="submit"&gt;
  &lt;/form&gt;
  ';
  require_once ('index2.html.php');
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>ORIGINAL QUESTION: </p>
  
  <p>I am trying to write an edit page using php/mysql, and have this foreach code that works: </p>
  
  <pre><code>$vals = array('novice', 'occasional', 'avid');
  $radio_opts = "";
  foreach ($vals as $val) {
      $checked = ($val == $anglerAvidity) ? ' checked="checked"' : '';
      $radio_opts .= "&lt;input type=\"radio\" name=\"groupname\" value=\"{$val}\"{$checked}&gt;   {$val}&lt;br /&gt;\n";
  
  }
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;tr &gt;&lt;td&gt;I consider myself a/an:&lt;/td&gt;
              &lt;td colspan="2" align=left&gt;'. $radio_opts .'
              &lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>The radio buttons are printed and the correct value is selected.</p>
  
  <p>However, I have many questions on the page, and all are called out in a string using this type of thing: </p>
  
  <pre><code>$title='Survey&lt;br&gt;';
  $content='
  &lt;tr &gt;&lt;td&gt;I consider myself a/an:&lt;/td&gt;
  &lt;td colspan="2" align=left&gt;'. $radio_opts .'
  &lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>This is all working fine, but when I try this: </p>
  
  <pre><code>$vals = array('novice', 'occasional', 'avid');
  $radio_opts = "";
  foreach ($vals as $val) {
      $checked = ($val == $anglerAvidity) ? ' checked="checked"' : '';
      $radio_opts .= "&lt;input type=\"radio\" name=\"groupname\" value=\"{$val}\"{$checked}&gt;   {$val}&lt;br /&gt;\n";
  
  }
  $vals2 = array('kayak or canoe', 'float tube', 'on ice', 'rod and reel', 'trap nets', 'other');
  $radio_opts2 = "";
  foreach ($vals2 as $val2) {
      $checked2 = ($val2 == $anglerFishMode) ? ' checked="checked"' : '';
      $radio_opts2 .= "&lt;input type=\"radio\" name=\"groupname\" value=\"{$val2}\"{$checked2}&gt; {$val2}&lt;br /&gt;\n";
  }
  </code></pre>
  
  <p>with HTML </p>
  
  <pre><code>    &lt;tr &gt;&lt;td&gt;I consider myself a/an:&lt;/td&gt;
      &lt;td colspan="2" align=left&gt;'.$radio_opts .'
      &lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>    &lt;tr &gt;&lt;td colspan="3" &gt;My normal mode of fishing is:&lt;/td&gt;&lt;/tr&gt;
      &lt;tr &gt;&lt;td colspan="3"&gt;'.$radio_opts2.'
      &lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>both radio button groups are displayed, but only the second group's radio button is populated from the db. If I switch the order around and call radio_opts2 first, then radio_opts is populated and radio_opts2 displays as an unselected radio group. That's why I think it's <em>almost</em> working if I could get the foreach to end somehow. If I end the first foreach group or call the second  foreach group within the content variable I get an unexpected T_STRING error. I knew this call was bad code, but for the sake of completeness, tried it anyway. </p>
  
  <p>It's got to be some dumb thing I'm doing, not ending the first foreach loop, but when I try to use endif or break I will either get nothing echoed at all for the first radio group or throw an unexpected else error message. I have reviewed all of the manuals and spent hours searching this forum for help, but I still can't figure out how to write this code. </p>
  
  <p>Thanks much for any help!</p>