使用$ i的php“while”语句问题

时间:2014-07-01 10:51:55

标签: php while-loop

真的难倒,我的代码不断落在下面的部分。我真的不明白为什么,任何线索都会非常感激!

    $message = "Order Number : ".$order_number."\n";
    $message .= "Coupon : ".$coupon."\n"."\n";
    $i = 0;
while (++$i) {
  if (isset($_POST['course'.$i.''])) {
    # we have a course defined, lets add it to the message.
    $message .= "Race ".$i."      : ".$_POST['course'.$i.'']."\n";
    $message .= "Date        : ".$_POST['day'.$i.'']."\n";
    $message .= "Time        : ".$_POST['hr'.$i.''].":".$_POST['min'.$i.'']."\n";
    $message .= "Race Number : ".$_POST['numb'.$i.'']."\n";
    $message .= "Race Name   : ".$_POST['name'.$i.'']."\n";
    $message .= "Horse       : ".$_POST['horse'.$i.'']."\n"."\n";

  } else {   
   break;
  }
}  
    $message .= "Copies      : ".$_POST['copies']."\n";
    $message .= "Phone       : ".$_POST['custphone']."\n";
    $message .= "Total Cost  : ".$order_amount."\n";

var_dump $ message:

    Order Number : 50638785
    Coupon : 

    Copies      : 1
    Phone       : 0123456789
    Total Cost  : 80.00

var_dump $ post:

array(13){[" course1"] => string(4)" BRIG" [" DAY1"] => string(10)" 16-07-2014" [" HR1"] =>字符串(2)" 15" [" MIN1"] =>字符串(2)" 30" [" numb1"] =>字符串(2)" 06" [" NAME1"] => string(14)" Grand National" [" horse1"] =>字符串(9)"毛茛" [" custphone"] => string(11)" 01234567898" ["优惠券"] => string(0)"" ["国家"] => string(14)"英国" ["拷贝"] =>字符串(1)" 1" ["量"] => string(5)" 80.00" [" B1"] =>字符串(3)"购买" }

1 个答案:

答案 0 :(得分:0)

这可能更好:

 $i = 0 ;    
 while(isset($_POST['course'.$i.''])) {
        # we have a course defined, lets add it to the message.
        $message .= "Race ".$i."      : ".$_POST['course'.$i.'']."\n";
        $message .= "Date        : ".$_POST['day'.$i.'']."\n";
        $message .= "Time        : ".$_POST['hr'.$i.''].":".$_POST['min'.$i.'']."\n";
        $message .= "Race Number : ".$_POST['numb'.$i.'']."\n";
        $message .= "Race Name   : ".$_POST['name'.$i.'']."\n";
        $message .= "Horse       : ".$_POST['horse'.$i.'']."\n"."\n";
        $i++;
 }