foreach重复上一次迭代

时间:2017-06-05 12:50:09

标签: php foreach

我很抱歉重复我关于我的另一篇文章的问题,但它没有解决,因此我在开发过程中完全被阻止了。我是体育俱乐部的志愿者,允许会员租用滑雪设备。我有3个文件:成员做出选择的第一个“choice.php”,第二个“summary.php”,它允许他添加材料或确认他的选择,第三个“confirm.php”,他添加个人数据和我通过电子邮件确认订单的地方。我的问题是,当所有选择都已完成并且用户想要确认时,最后一行加倍。我试着把

UNSET();

指示循环内外的不同位置,但没有任何改变。怎么避免这个?这是我的文件(请注意这些是干净的文件,我删除了所有控件,以尽可能缩短文件)。非常感谢您的帮助。 第一个文件“choice.php”:

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Choice</title>
</head>
<body id="top">     
          <?php process_si_contact_form();
                if (isset($_SESSION['ctform']['error']) &&     $_SESSION['ctform']['error'] == true): 
                elseif (isset($_SESSION['ctform']['success']) && $_SESSION['ctform']['success'] == true): endif; ?>                     
          <form method="post" action="#top" id="contact_form" />
            <input type="hidden" name="do" value="contact" enctype="text/plain" />
            <table class="table" align="center" border="0" cellspacing="0" cellpadding="0">
              <tr valign="middle">
                <td align="left"><label>&nbsp;Type</label></td>
                <td><select name="ct_ski" readonly="readonly" value=""/>
                  <?php $ski = isset($_REQUEST['ct_ski']) ? $_REQUEST['ct_ski'] : NULL; ?>
                  <?php $attr = 'selected="selected"'; ?>
                  <option value="1" <?php echo $ski == 1 ? $attr : ''; ?> >Skis for adults</option>
                  <option value="2" <?php echo $ski == 2 ? $attr : ''; ?> >Skis for juniors</option>
                  <option value="3" <?php echo $ski == 3 ? $attr : ''; ?> >Skis for kids</option>
                  </select>
                </td>    
                <td align="left"><label>Boots</label></td>
                <td><select name="ct_boots" readonly="readonly" value=""/>
                  <?php $boots = isset($_REQUEST['ct_boots']) ? $_REQUEST['ct_boots'] : NULL; ?>
                  <?php $attr = 'selected="selected"'; ?>
                  <option value="1" <?php echo $boots == 1 ? $attr : ''; ?> >Adult</option>
                  <option value="2" <?php echo $boots == 2 ? $attr : ''; ?> >Junior</option>
                  <option value="3" <?php echo $boots == 3 ? $attr : ''; ?> >Kids</option>
                  </select>
                  </td>    
                </tr>
              <tr valign="middle">
                <td align="left"><label>&nbsp;Length</label></td>
                <td><select name="ct_length" readonly="readonly" value=""/>
                  <?php $length = isset($_REQUEST['ct_length']) ? $_REQUEST['ct_length'] : NULL; ?>
                  <?php $attr = 'selected="selected"'; ?>
                  <option value="1" <?php echo $length == 1 ? $attr : ''; ?> >75 cm</option>
                  <option value="2" <?php echo $length == 2 ? $attr : ''; ?> >120 cm</option>
                  <option value="3" <?php echo $length == 3 ? $attr : ''; ?> >165 cm</option>
                  </select>
                </td>
                <td align="left"><label>Size</label></td>
                <td><select name="ct_size" readonly="readonly" value=""/>
                  <?php $size = isset($_REQUEST['ct_size']) ? $_REQUEST['ct_size'] : NULL; ?>
                  <?php $attr = 'selected="selected"'; ?>
                  <option value="1" <?php echo $size == 1 ? $attr : ''; ?> >20,0</option>
                  <option value="2" <?php echo $size == 2 ? $attr : ''; ?> >25,0</option>
                  <option value="3" <?php echo $size == 3 ? $attr : ''; ?> >30,0</option>
                  </select>
                  </td>
                </tr>
</table>
            <br /><center><input type="submit" value="Summary" /></center></form>
</body>
</html>
<?php
function process_si_contact_form()
{
$_SESSION['ctform'] = array(); 
  if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
    foreach($_POST as $key => $value) {
      if (!is_array($key)) {
        if ($key != 'ct_message') $value = strip_tags($value);
        $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
      }
    }
    $ski    = @$_POST['ct_ski'];
    $boots  = @$_POST['ct_boots'];
    $length = @$_POST['ct_length'];
    $size   = @$_POST['ct_size'];
    $errors = array();
    if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {  
    }     
$skilist = array("Skis for adults" => 1, "Skis for juniors" => 2, "Skis for kids" => 3);
$skimail = array_search($ski, $skilist);  
$bootslist = array("Adult" => 1, "Junior" => 2, "Kids" => 3);
$bootsmail = array_search($boots, $bootslist);  
$lengthlist = array("75 cm" => 1, "120 cm" => 2, "165 cm" => 3);
$lengthmail = array_search($length, $lengthlist);
$sizelist = array("20,0" => 1, "25,0" => 2, "30,0" => 3);
$sizemail = array_search($size, $sizelist); 
if (sizeof($errors) != 0) {
    goto err;
      }     
$_SESSION['ct_ski'] = $skimail;
$_SESSION['ct_boots'] = $bootsmail;
$_SESSION['ct_length'] = $lengthmail;
$_SESSION['ct_size'] = $sizemail;
/* $_SESSION['DEBUG_MODE'] = $GLOBALS['DEBUG_MODE']; */
?>      
<a href="summary.php"></a>
<script type="text/javascript">;
window.location.replace("summary.php");
</script>
<?php     err:
    if (sizeof($errors) == 0) {
      $_SESSION['ctform']['error'] = false;
      $_SESSION['ctform']['success'] = true;
    } else {
      $_SESSION['ctform']['ct_ski'] = $ski;
      $_SESSION['ctform']['ct_boots'] = $boots;
      $_SESSION['ctform']['ct_length'] = $length;
      $_SESSION['ctform']['ct_size'] = $size;         
      foreach($errors as $key => $error) {
        $_SESSION['ctform'][$key] = "<span style=\"font-weight: bold; color: #f00\">$error</span>";
      }
        $_SESSION['ctform']['error'] = true;
    }
  } 
}
$_SESSION['ctform']['success'] = false;
?>

这是我的第二个文件“summary.php”的代码:

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Summary</title>
<style>
pre {
    display: block;
    font-family: "Courier New", monospace;
    font-stretch: normal;
    font-size: 12px;
    white-space: pre;
} 
</style>
</head>
<body>     
<?php
$select = array();
$select['type'] = $_SESSION['ct_ski'];
$select['length'] = $_SESSION['ct_length'];
$select['boots'] = $_SESSION['ct_boots'];
$select['size'] = $_SESSION['ct_size'];
if(!isset($_SESSION['panier']))
{
$_SESSION['panier'] = array();
$_SESSION['panier']['type'] = array();
$_SESSION['panier']['length'] = array();
$_SESSION['panier']['boots'] = array();
$_SESSION['panier']['size'] = array();
}   
array_push($_SESSION['panier']['type'],$select['type']);
array_push($_SESSION['panier']['length'],$select['length']);
array_push($_SESSION['panier']['boots'],$select['boots']);
array_push($_SESSION['panier']['size'],$select['size']);
$type = $_SESSION['panier']['type'];
$length = $_SESSION['panier']['length'];
$boots = $_SESSION['panier']['boots'];
$size = $_SESSION['panier']['size'];     
  echo "Your summary :<br><br>
  <pre>
  Type                 | Length              | Boots                  |       Size</pre>";        
  foreach ($type as $key => $value) {
   unset($value);
   $type[$key] = str_pad($type[$key], 20);
   $length[$key] = str_pad($length[$key], 19);
   $boots[$key] = str_pad($boots[$key], 22);
   $size[$key] = str_pad($size[$key], 15);   
   echo "<pre>  " .$type[$key] . " | " . $length[$key] . " | " .     $boots[$key] . " | " . $size[$key] .  "</pre>";    
  }   
   /* $_SESSION['type'] = $type[$key]; 
   $_SESSION['length'] = $length[$key]; 
   $_SESSION['boots'] = $boots[$key]; 
   $_SESSION['size'] = $size[$key]; */
?>
          <hr>
          <a href="choice.php">Clic here to add skis</a>
          <hr>
          <a href="confirm.php">Clic here to confirm</a>
          <hr>
          <a href="delete.php">Clic here to delete all</a>
</body>
</html>

以下是我的最终文件“confirm.php”的代码,其中您看到最后一个选项的错误加倍:

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Confirmation</title>
<style>
pre {
    display: block;
    font-family: "Courier New", monospace;
    font-stretch: normal;
    font-size: 12px;
    white-space: pre;
} 
</style>
</head>
<body>     
<?php       
$select = array();
$select['type'] = $_SESSION['ct_ski'];
$select['length'] = $_SESSION['ct_length'];
$select['boots'] = $_SESSION['ct_boots'];
$select['size'] = $_SESSION['ct_size'];

if(!isset($_SESSION['panier']))
{
    $_SESSION['panier'] = array();
    $_SESSION['panier']['type'] = array();
    $_SESSION['panier']['length'] = array();
    $_SESSION['panier']['boots'] = array();
    $_SESSION['panier']['size'] = array();
}   
array_push($_SESSION['panier']['type'],$select['type']);
array_push($_SESSION['panier']['length'],$select['length']);
array_push($_SESSION['panier']['boots'],$select['boots']);
array_push($_SESSION['panier']['size'],$select['size']); 
$type = $_SESSION['panier']['type'];
$length = $_SESSION['panier']['length'];
$boots = $_SESSION['panier']['boots'];
$size = $_SESSION['panier']['size'];     
  echo "Your summary :<br><br>
  <pre>
  Type                 | Length              | Boots                  | Size</pre>";          
  foreach ($type as $key => $value) {
   unset($value);
   $type[$key] = str_pad($type[$key], 20);
   $length[$key] = str_pad($length[$key], 19);
   $boots[$key] = str_pad($boots[$key], 22);
   $size[$key] = str_pad($size[$key], 15);  
   echo "<pre>  " .$type[$key] . " | " . $length[$key] . " | " . $boots[$key] . " | " . $size[$key] .  "</pre>";    
  }          
?>
          <hr>
          <a href="delete.php">Clic here to delete all</a>
          <hr><br>Personal information here... (Name, Firstname, email, etc.) 
</body>
</html>

最后是一个简短的文件,只需点击一下即可轻松删除所有选项

<?php session_start();
$vide = false;
    if(!isset($_SESSION['panier']['verrouille']) || $_SESSION['panier']['verrouille'] == false)
    {
        if(isset($_SESSION['panier']))
        {
            unset($_SESSION['panier']);
            if(!isset($_SESSION['panier']))
            {
                $vide = true;
            }
        }
        else
        {
             $vide = "inexistant";
        }
    } 
?>
<html>
<head>
<script type="text/javascript">
     window.location.replace("choice.php");
</script>
</head>
</html>

0 个答案:

没有答案