在 else if 中结束 PHP 块以输出本身包含 PHP 块的 HTML
Ending a PHP block while in an else if to output HTML which itself contains PHP blocks
所以,我有以下 else if
:
else if (isset($_POST['orderID'])) {
/* Update order. */
$orderID = $_POST['orderID'];
$link = getConn();
$custInfo = getCustData($orderID, $link, $tableName);
//$custInfo contains [id, order_id, student, firstname, lastname, email, address, phone, price, size, anchovies,
//pepperoni, peppers, olives, onions, createdatetime]
?>
<form id='updateOrder' name='orderID' method='post' action='vieworder.php'>
<h3>What Size of Pizza Would You Like?</h3>
Small <input id='small' type='radio' name='pizzaSize' value='small' <?php echo ($custInfo[9]=='small')?'checked':''?>/>
Medium <input id='medium' type='radio' name='pizzaSize' value='medium' <?php echo ($custInfo[9]=='medium')?'checked':''?>/>
Large <input id='large' type='radio' name='pizzaSize' value='large' <?php echo ($custInfo[9]=='large')?'checked':''?>/>
<br>
<h3>Add Extra Toppings</h3>
Anchovies <input id='anchovies' type='checkbox' name='anchovies' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Pineapples <input id='pineapples' type='checkbox' name='pineapples' value='yes'<?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Pepperoni <input id='pepperoni' type='checkbox' name='pepperoni' value='yes'<?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Olives <input id='olives' type='checkbox' name='olives' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Onions <input id='onions' type='checkbox' name='onions' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Peppers <input id='peppers' type='checkbox' name='peppers' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
<h3>Enter your details:</h3>
First Name: <input id='forename' type='text' name='forename' value="<?php echo htmlentities($custInfo[3]); ?>" required/>
<br>
Surname: <input id='surname' type='text' name='surname' value="<?php echo htmlentities($custInfo[4]); ?>" required/>
<br>
Address: <textarea id='address' name='address' rows='5' cols='30' value="<?php echo htmlentities($custInfo[6]); ?>" required></textarea>
<br>
Email Address: <input id='email' type='email' name='email' value="<?php echo htmlentities($custInfo[5]); ?>" required/>
<br>
Phone Number: <input id='phoneNumber' type='text' name='phone' value="<?php echo htmlentities($custInfo[7]); ?>" required/>
<br>
Tick here if you are a student: <input id='studentDiscount' type='checkbox' name='student'<?php echo ($custInfo[2]=='Y')?'checked':''?>/>
<br>
<button type='submit' value='Place Order'>Submit Order</button>
</form>
<?php
}
我在得到$custInfo
后关闭else if
开始的PHP块,然后开始重新显示用户之前填写的html表单.我在 HTML 中使用了更多 PHP 块来填写表单,因为它是使用存储在 $custInfo
中的值创建的。然后我开始另一个 PHP 块并关闭 else if 并且文件继续。
初始化 $custInfo
后调用 var_dump($custInfo)
给出预期结果:
array(17) { ["id"]=> string(3) "193" ["order_id"]=> string(13) "5550a328ddb36" ["student"]=> string(1) "Y" ["firstname"]=> string(6) "Peadar" ["lastname"]=> string(11) "Ó Duinnín" ["email"]=> string(15) "email@email.com" ["address"]=> string(32) "Line 1, Line 2, City, County." ["phone"]=> string(10) "0870123456" ["price"]=> string(5) "15.00" ["size"]=> string(5) "large" ["anchovies"]=> string(1) "Y" ["pineapples"]=> string(1) "N" ["pepperoni"]=> string(1) "N" ["peppers"]=> string(1) "Y" ["olives"]=> string(1) "Y" ["onions"]=> string(1) "Y" ["createdatetime"]=> string(19) "2015-05-11 13:40:08" }
但是,我得到以下输出:
这是范围问题还是我在这里做错了什么?如果需要,我可以提供更多代码或 JSFiddle。提前致谢!
由于您的数组 $custInfo
是一个关联数组,因此数字偏移量将不可用。您需要使用分配给它的关联密钥,而不是 9
或 10
。
所以,我有以下 else if
:
else if (isset($_POST['orderID'])) {
/* Update order. */
$orderID = $_POST['orderID'];
$link = getConn();
$custInfo = getCustData($orderID, $link, $tableName);
//$custInfo contains [id, order_id, student, firstname, lastname, email, address, phone, price, size, anchovies,
//pepperoni, peppers, olives, onions, createdatetime]
?>
<form id='updateOrder' name='orderID' method='post' action='vieworder.php'>
<h3>What Size of Pizza Would You Like?</h3>
Small <input id='small' type='radio' name='pizzaSize' value='small' <?php echo ($custInfo[9]=='small')?'checked':''?>/>
Medium <input id='medium' type='radio' name='pizzaSize' value='medium' <?php echo ($custInfo[9]=='medium')?'checked':''?>/>
Large <input id='large' type='radio' name='pizzaSize' value='large' <?php echo ($custInfo[9]=='large')?'checked':''?>/>
<br>
<h3>Add Extra Toppings</h3>
Anchovies <input id='anchovies' type='checkbox' name='anchovies' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Pineapples <input id='pineapples' type='checkbox' name='pineapples' value='yes'<?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Pepperoni <input id='pepperoni' type='checkbox' name='pepperoni' value='yes'<?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Olives <input id='olives' type='checkbox' name='olives' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Onions <input id='onions' type='checkbox' name='onions' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
Peppers <input id='peppers' type='checkbox' name='peppers' value='yes' <?php echo ($custInfo[10]=='Y')?'checked':''?>/>
<br>
<h3>Enter your details:</h3>
First Name: <input id='forename' type='text' name='forename' value="<?php echo htmlentities($custInfo[3]); ?>" required/>
<br>
Surname: <input id='surname' type='text' name='surname' value="<?php echo htmlentities($custInfo[4]); ?>" required/>
<br>
Address: <textarea id='address' name='address' rows='5' cols='30' value="<?php echo htmlentities($custInfo[6]); ?>" required></textarea>
<br>
Email Address: <input id='email' type='email' name='email' value="<?php echo htmlentities($custInfo[5]); ?>" required/>
<br>
Phone Number: <input id='phoneNumber' type='text' name='phone' value="<?php echo htmlentities($custInfo[7]); ?>" required/>
<br>
Tick here if you are a student: <input id='studentDiscount' type='checkbox' name='student'<?php echo ($custInfo[2]=='Y')?'checked':''?>/>
<br>
<button type='submit' value='Place Order'>Submit Order</button>
</form>
<?php
}
我在得到$custInfo
后关闭else if
开始的PHP块,然后开始重新显示用户之前填写的html表单.我在 HTML 中使用了更多 PHP 块来填写表单,因为它是使用存储在 $custInfo
中的值创建的。然后我开始另一个 PHP 块并关闭 else if 并且文件继续。
初始化 $custInfo
后调用 var_dump($custInfo)
给出预期结果:
array(17) { ["id"]=> string(3) "193" ["order_id"]=> string(13) "5550a328ddb36" ["student"]=> string(1) "Y" ["firstname"]=> string(6) "Peadar" ["lastname"]=> string(11) "Ó Duinnín" ["email"]=> string(15) "email@email.com" ["address"]=> string(32) "Line 1, Line 2, City, County." ["phone"]=> string(10) "0870123456" ["price"]=> string(5) "15.00" ["size"]=> string(5) "large" ["anchovies"]=> string(1) "Y" ["pineapples"]=> string(1) "N" ["pepperoni"]=> string(1) "N" ["peppers"]=> string(1) "Y" ["olives"]=> string(1) "Y" ["onions"]=> string(1) "Y" ["createdatetime"]=> string(19) "2015-05-11 13:40:08" }
但是,我得到以下输出:
这是范围问题还是我在这里做错了什么?如果需要,我可以提供更多代码或 JSFiddle。提前致谢!
由于您的数组 $custInfo
是一个关联数组,因此数字偏移量将不可用。您需要使用分配给它的关联密钥,而不是 9
或 10
。