为什么我的php循环不工作?

为什么我的php循环不工作?,php,Php,好的,我有一个PHP变量$question,如下所示: $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'; if ($type == "checkbox")

好的,我有一个PHP变量$question,如下所示:

$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';
 if ($type == "checkbox") {
            print "<br>\n";
            $i = 1;
            foreach ($opts as $opt_key => $opt_value) {
                if ($deft == $i) {$CHECKED = "checked=\"checked\""; } 
                   else { $CHECKED = ''; }

                print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
                $i++;
            }
         }
foreach ($opts as $opt_key => $opt_value) { if ($deft == $i) {$CHECKED = "checked=\"checked\""; } else { $CHECKED = ''; } print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n"; $i++; } 我想做的是让这成为一个调查中的问题,其中desc是一个问题,opts是多个答案

我目前有以下声明:

 if ($type == "checkbox") {
              if (strtolower($deft) == 'y') { $box_value = "1"; $CHECKED = ' CHECKED'; }
                 else { $box_value = "0"; $CHECKED = ''; }
              print "<input type=\"checkbox\" name=\"$key\" value=\"$box_value\"$CHECKED>\n<br>";
         }
if($type==“复选框”){
如果(strtolower($deft)='y'){$box_value=“1”;$CHECKED='CHECKED';}
else{$box_value=“0”;$CHECKED='';}
打印“\n
”; }
它工作,但它不允许我包括一个问题(只有答案)

我想这样做:

$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';
 if ($type == "checkbox") {
            print "<br>\n";
            $i = 1;
            foreach ($opts as $opt_key => $opt_value) {
                if ($deft == $i) {$CHECKED = "checked=\"checked\""; } 
                   else { $CHECKED = ''; }

                print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
                $i++;
            }
         }
foreach ($opts as $opt_key => $opt_value) { if ($deft == $i) {$CHECKED = "checked=\"checked\""; } else { $CHECKED = ''; } print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n"; $i++; }
if($type==“复选框”){
打印“
\n”; $i=1; foreach($opts as$opt_key=>$opt_value){ 如果($deft==i){$CHECKED=“CHECKED=\”CHECKED\”;} else{$CHECKED='';} 打印“-$opt\u值
\n”; $i++; } }
我收到一个错误,因为我的代码很糟糕。谁能告诉我帮我修复这个代码吗

谢谢

下面是代码的其余部分

    <?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 => $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 => $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 => $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 < $email_to_send_to >\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 "<form action=\"$thisfile_URL\" method=\"POST\"> \n";

     foreach ($question as $key => $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 "<input type=\"checkbox\" name=\"$key\" value=\"$box_value\"$CHECKED>\n<br>";
         }
         */

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

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


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

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

         if ($type == "select") {
             print "<br>\n";
             print "<select name=\"$key\">\n";

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

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

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

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

       print "<br>\n";

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

     }

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

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

if (!(empty($close_template))) { include($close_template); }
?>

在打印行中的
$CHECKED
之前是否需要一个空格

我假设您在某个地方突破$question以获取
$key
$deft
$opts
,因为您没有显示它们的定义位置。
$key
应该是
$opt\u key

我知道:

$question['3']['opts'] = 'Check Test 1';
$question['3']['opts'] = 'Check Test 2';

这些是变量:第二个赋值覆盖了第一个。

这有点困难,因为我似乎无法找到您的问题所在,但您的代码如下所示:

$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';
 if ($type == "checkbox") {
            print "<br>\n";
            $i = 1;
            foreach ($opts as $opt_key => $opt_value) {
                if ($deft == $i) {$CHECKED = "checked=\"checked\""; } 
                   else { $CHECKED = ''; }

                print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
                $i++;
            }
         }
foreach ($opts as $opt_key => $opt_value) { if ($deft == $i) {$CHECKED = "checked=\"checked\""; } else { $CHECKED = ''; } print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n"; $i++; } foreach($opts as$opt_key=>$opt_value){ 如果($deft==i){$CHECKED=“CHECKED=\”CHECKED\”;} else{$CHECKED='';} 打印“-$opt\u值
\n”; $i++; } 肯定会导致html出现问题

我可以建议重写一下吗(如果它影响到你的错误,还可以写一份报告)

foreach($opts as$opt_key=>$opt_value){ $CHECKED=$deft=$i?'CHECKED=“CHECKED”:“”; 打印“-$opt\u值
\n”; ++$i; }
代码的问题是生成的标记看起来像
name=“key”checked=“checked”
。应该有一个分隔的空格,如
name=“key”checked=“checked”

您需要认真重新格式化您的邮件。您需要解释您收到的错误。请同时包括您的输出和预期输出。我可以发现一些问题,但我需要知道您的确切错误。例如,您可能希望在打印对账单中签入美元之前插入空格。我还建议您学习如何使用=?:;如$CHECKED=$deft==$i?“checked=“checked”:“”;在for语句的最后一行中,使用+++$i而不是$i++(并仔细阅读差异,因为存在差异)。