Why is my php loop not working?
<p>Ok I have a PHP variable $question that looks like this:</p>

<pre><code>$question['3']['type'] = 'checkbox';
$question['3']['desc'] = 'checkbox question';
$question['3']['deft'] = '1';
$question['3']['opts'] = 'Check Test 1';
$question['3']['opts'] = 'Check Test 2';
</code></pre>

<p>What I want to do is have this be a question on a survey where the desc is the question, and opts are the multiple answers. </p>

<p>I currently have this statement:</p>

<pre><code> if ($type == "checkbox") {
              if (strtolower($deft) == 'y') { $box_value = "1"; $CHECKED = ' CHECKED'; }
                 else { $box_value = "0"; $CHECKED = ''; }
              print "&lt;input type=\"checkbox\" name=\"$key\" value=\"$box_value\"$CHECKED&gt;\n&lt;br&gt;";
         }
</code></pre>

<p>It works but it doesn't allow me to include a question (only answers).</p>

<p>I want to do something like this:</p>

<pre><code> if ($type == "checkbox") {
            print "&lt;br&gt;\n";
            $i = 1;
            foreach ($opts as $opt_key =&gt; $opt_value) {
                if ($deft == $i) {$CHECKED = "checked=\"checked\""; } 
                   else { $CHECKED = ''; }

                print "&lt;input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED&gt; - $opt_value &lt;br&gt;\n";
                $i++;
            }
         }
</code></pre>

<p>I'm getting an error because my code is crappy. Can anyone tell help me fix this code? </p>

<p>THanks</p>

<p>Here is the rest of the code</p>

<pre><code>    &lt;?php
$thisfile_URL = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$thisdir_URL  = substr($thisfile_URL, 0, strrpos($thisfile_URL, '/')).'/';
$thisfile_ABS = $_SERVER['SCRIPT_FILENAME'];
$thisdir_ABS  =  substr($thisfile_ABS, 0, strrpos($thisfile_ABS, '/')).'/';
# -------------------------------------------------------------------
# ***** CONFIGURATION SETTINGS *****

# The Email address you want results mailed to.
$email_to_send_to = 'nursesurveyresults@aplusprofessionalresumes.com';

# The name of the survey. What do you want to call it?
$name_of_survey = 'Nurse Resume Toolkit Sales Page Exit Survey';

# Template files to load. The opening template, the close template.
# The template displayed before the survey form is printed and
# The template displayed after the email is sent. Or leave empty.
$open_template         = $thisdir_ABS."templates/sss.open.php";
$close_template        = $thisdir_ABS."templates/sss.close.php";
$survey_heading_page   = $thisdir_ABS."templates/sss.head.php";
$survey_complete_page  = $thisdir_ABS."templates/sss.done.php";

# The charset being used for all messages sent out. ISO-8859-1 by default,
# but you can use anything you want (Unicode, etc).
$charset = 'ISO-8859-1';

# -------------------------------------------------------------------

# ----------------------- Survey Form Information -------------------
# Type: checkbox
# Deft: Y or N for default check.
#
# Type: radio
# Deft: Default selected option
# Opts: Array of option values
#
# Type: select
# Deft: Default selected option
# Opts: Array of option values
#
# Type: text
# Deft: Default field text
# Opts: maxl, size - Maxlength and field size
#
# Type: textarea
# Deft: Default textarea field text
# Opts: rows, cols - row size, column size
#
# $hidden is an array of hidden fields.
# key being the name of the field, value as the value.
#
# $button_text is the submit button text
# -------------------------------------------------------------------

# ***** SAMPLE DATA *****

$question['1']['type'] = 'select';
$question['1']['desc'] = 'Whats Your Current Employment Status';
$question['1']['deft'] = '1';
$question['1']['opts'][] = 'Employed';
$question['1']['opts'][] = 'Unemployed';
$question['1']['opts'][] = 'Not Sure';

$question['2']['type'] = 'radio';
$question['2']['desc'] = 'Are You Currently Looking for a Job';
$question['2']['deft'] = '1';
$question['2']['opts'][] = 'Yes';
$question['2']['opts'][] = 'No';

/*
$question['1']['type'] = 'text';
$question['1']['desc'] = 'Test Text';
$question['1']['deft'] = 'Blah';
$question['1']['opts']['maxl'] = '30';
$question['1']['opts']['size'] = '10';

$question['2']['type'] = 'checkbox';
$question['2']['desc'] = 'Check Test 1';
$question['2']['deft'] = 'y';
*/

$question['3']['type'] = 'checkbox';
$question['3']['desc'] = 'checkbox question';
$question['3']['deft'] = '1';
$question['3']['opts'] = 'Check Test 1';
$question['3']['opts'] = 'Check Test 2';


$question['4']['type'] = 'radio';
$question['4']['desc'] = 'Radio Test';
$question['4']['deft'] = '2';
$question['4']['opts'][] = 'Option 1';
$question['4']['opts'][] = 'Option 2';
$question['4']['opts'][] = 'Option 3';

$question['5']['type'] = 'select';
$question['5']['desc'] = 'Select Test';
$question['5']['deft'] = '2';
$question['5']['opts'][] = 'Option 1';
$question['5']['opts'][] = 'Option 2';
$question['5']['opts'][] = 'Option 3';

$question['6']['type'] = 'textarea';
$question['6']['desc'] = 'Textarea Test';
$question['6']['deft'] = 'Default Junk';
$question['6']['opts']['rows'] = '10';
$question['6']['opts']['cols'] = '30';

$hidden['test1'] = "TEST!";
$hidden['test2'] = "TESTO!";
$button_text     = 'Send it!';

# -------------------------------------------------------------------
function my_remove_slashes($st) {
  if (get_magic_quotes_gpc()) { return stripslashes($st); }
  else { return $st; }
}

function security_filter($st) {
   $attribs = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
              'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';

   $st = my_remove_slashes($st);
   $st = stripslashes(preg_replace("/$attribs/i", 'forbidden', $st));
   $st = strip_tags($st);
   $st = htmlentities($st);
   return $st;
}
# -------------------------------------------------------------------

if (!(empty($open_template))) { include($open_template); }

if (isset($_POST['button'])) {
   foreach ($_POST as $key =&gt; $value) {
     if ($key != 'button') {
        if (preg_match('/^hidden_(.*)/i',$key)) {
           $value = security_filter($value);
           $key = trim(strstr($key,'_'),'_');

           if (isset($hidden[$key])) { 
               $hidden_data[$key] = $value; 
           }
        }
        else {
           if (isset($question[$key])) {
               $value = security_filter($value);
               if ($question[$key]['type'] == 'checkbox') { $value = "YES"; }
               $results[$key] = $value;
           }
        }
     }
   }

   # Now that the responses are processed, prepare the email.

   $msg  = "----------------- User Info -----------------\n\n";
   $msg .= "Sent from: ".$_SERVER['REMOTE_HOST']." [".$_SERVER['REMOTE_ADDR']."] \n";
   $msg .= "Coming from (referer): ".$_SERVER['HTTP_REFERER']."\n";
   $msg .= "Using (user agent): ".$_SERVER['HTTP_USER_AGENT']."\n\n";
   $msg .= "---------------------------------------------\n\n";

   if (isset($question)) {
      $lineflag = FALSE;
      foreach ($results as $key =&gt; $value) {
         if ($lineflag == FALSE) { 
              $msg .= "----------------- Questions -----------------\n\n";
              $lineflag = TRUE;
         }
         $msg .= "Question: ". $key ." - ". $question[$key]['desc'] ."\n";
         $msg .= "Response: ". $value . "\n\n";
      }
      $msg .= "---------------------------------------------\n";
   }

   $msg .= "\n";

   if (isset($hidden_data)) {
      $lineflag = FALSE;
      foreach ($hidden_data as $key =&gt; $value) {
         if ($lineflag == FALSE) { 
              $msg .= "---------------- Hidden vars ----------------\n\n";
              $lineflag = TRUE;
         }
         $msg .= $key ." - ". $value ."\n";
      }
      $msg .= "\n---------------------------------------------\n";
   }

   # Prep and send email.
   $headers  = "Return-Path: $email_to_send_to\r\n";
   $headers .= "From: $name_of_survey processor &lt; $email_to_send_to &gt;\r\n";
   $headers .= "Content-type: text/plain; charset=$charset\r\n";
   mail($email_to_send_to, $name_of_survey . " mailer", $msg, $headers);

   # Include template file.
   if (!(empty($survey_complete_page))) { include($survey_complete_page); }
}

else {

     if (!(empty($survey_heading_page))) { include($survey_heading_page); }

     print "&lt;form action=\"$thisfile_URL\" method=\"POST\"&gt; \n";

     foreach ($question as $key =&gt; $value) {
         $type = strtolower($question[$key]['type']);
         $desc = $question[$key]['desc'];
         $deft = $question[$key]['deft'];
         $opts = $question[$key]['opts'];

         print $desc."\n";

         /*
         if ($type == "checkbox") {
              if (strtolower($deft) == 'y') { $box_value = "1"; $CHECKED = ' CHECKED'; }
                 else { $box_value = "0"; $CHECKED = ''; }
              print "&lt;input type=\"checkbox\" name=\"$key\" value=\"$box_value\"$CHECKED&gt;\n&lt;br&gt;";
         }
         */

            if ($type == "checkbox") {
            print "&lt;br&gt;\n";
            $i = 1;
            foreach ($opts as $opt_key =&gt; $opt_value) {
                if ($deft == $i) {$CHECKED = "checked=\"checked\""; } 
                   else { $CHECKED = ''; }

                print "&lt;input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED&gt; - $opt_value &lt;br&gt;\n";
                $i++;
            }
         }


         if ($type == "radio") {
            print "&lt;br&gt;\n";
            $i = 1;
            foreach ($opts as $opt_key =&gt; $opt_value) {
                if ($deft == $i) { $CHECKED = " checked=\"checked\""; } 
                   else { $CHECKED = ''; }

                print "&lt;input type=\"RADIO\" name=\"$key\" value=\"$opt_value\"$CHECKED&gt; - $opt_value &lt;br&gt;\n";
                $i++;
            }
         }

         if ($type == "select") {
             print "&lt;br&gt;\n";
             print "&lt;select name=\"$key\"&gt;\n";

             $i = 1;
             foreach ($opts as $opt_key =&gt; $opt_value) {
                 if ($deft == $i) { $CHECKED = ' SELECTED'; } 
                     else { $CHECKED = ''; }
                     print "&lt;option value=\"$opt_value\"$CHECKED&gt;$opt_value&lt;/option&gt;\n";
                 $i++;
             }
             print "&lt;/select&gt;&lt;br&gt;\n";
         }

         if ($type == "text") {
             print "&lt;br&gt;\n";
             $size = $opts['size'];
             $maxl = $opts['maxl'];
             print "&lt;input maxlength=\"$maxl\" size=\"$size\" name=\"$key\" value=\"$deft\"&gt;&lt;br&gt;\n";
         }

         if ($type == "textarea") {
             print "&lt;br&gt;\n";
             $colz = $opts['cols'];
             $rowz = $opts['rows'];
             print "&lt;textarea name=\"$key\" rows=\"$rowz\" cols=\"$colz\"&gt;$deft&lt;/textarea&gt;&lt;br&gt;\n";
         }

       # Spacing between survey questions -----------

       print "&lt;br&gt;\n";

       # --------------------------------------------

     }

     foreach ($hidden as $key =&gt; $value) {
        print "&lt;input type=\"hidden\" name=\"hidden_$key\" value=\"$value\"&gt;\n";
     }

     print "&lt;input type=\"submit\" name=\"button\" value=\"$button_text\"&gt;\n";
     print " &amp;nbsp; -- &amp;nbsp; \n";
     print "&lt;INPUT TYPE=\"reset\" VALUE=\"Clear\"&gt;\n";
     print "&lt;/form&gt;";
     print "&lt;center&gt;&lt;font size=\"2\"&gt;Powered by &lt;A HREF=\"http://www.violetonresumes.com\"&gt;A Plus Professional Resumes&lt;/A&gt;&lt;/font&gt;&lt;/center&gt;&lt;br&gt;\n";
}

if (!(empty($close_template))) { include($close_template); }
?&gt;
</code></pre>
