尝试使用 isset 设置值但仍然出错
Trying to use isset to set value but still get error
我是 php 的新手,正在尝试创建购物车页面。
我得到了使用 $_POST 数组发送的产品 ID、选项和价格。
我希望 $ino
保存 Id 的值- 我这样做了。
我遇到一个重复出现的错误。
Notice: Undefined index: 3 in /home/sl0/s3196040/public_html/wp/a3/cart.php on line 84
Name: Notice: Undefined index: 3 in /home/sl0/s3196040/public_html/wp/a3/cart.php on line 71
Price: Notice: Undefined index: 3 in /home/sl0/s3196040/public_html/wp/a3/cart.php on line 74**
我知道我想 "isset" $ino 检查它是否设置,但我想我有?
如果我像 $pumps[3]['Title']
这样手动输入值,它可以工作,但如果我像这样 $pumps[$ino]['Title']
.
使用 $ino 变量,它就会失败
这是在 $_POST 数组中 ( [Id] => 3 [option] => english [qty] => 3
define("TITLE", "Product Page");
include_once('tools.php');
topModule('Business Name - Home', 'showSpecials()');
print_r($pumps[3]['Price']);
print_r($_POST);
// if i have the ID of the product, i need to turn it into a variables that the cart can display
if (isset ( $_POST ['Id'] )) {
// Check the item is not already in the cart
if (!in_array($_POST ["Id"], $_SESSION['cart'])) {
// Add new item to cart
$_SESSION ['cart'][] = $_POST["Id"];
}
}
else if (isset ( $_POST ['delete'] )) { // a remove button has been clicked
// Remove the item from the cart
if (false !== $key = array_search($_POST['delete'], $_SESSION['cart'])) {
unset($_SESSION['cart'][$key]);
}
// Empty Cart
else if (isset ( $_POST ["delete"] )) { // remove item from cart
unset ( $_SESSION ['cart'] );
}
}
if (isset ( $_SESSION ["cart"] )) {
if (isset($_POST['submit'])) {
$ino = $_POST['submit'];
}
?>
<form action='(omitted link)'
target='_blank' method='post'
enctype=''>
<table>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Action</th>
</tr>
<?php
// Set a default total
$total = 0;
foreach ( $_SESSION['cart'] as $ino ) {
?>
<tr>
<td>
Name: <?php echo $pumps[$ino]['Title']; ?> // error here
</td>
<td>
Price: <?php echo $pumps[$ino]['Price']; ?> // error here
</td>
<td>
Qty: <?php echo $_POST[$ino]['qty']; ?> // error here
</td>
<td>
<button type='submit' name='delete' value='<?php echo $ino; ?>'>Remove</button>
</td>
</tr>
<?php
$total += $pumps[$ino]['Price']; // error here
} // end foreach
?>
Total: $<?php echo $total; ?>
<tr>
<td colspan="2">Total: $<?php echo($total); ?></td>
<td><input type='submit' value='Checkout' /></td>
</tr>
<tr>
<td><button type='submit' name='clear'>Clear cart</button></td>
</tr>
</table>
</form>
<?php } ?>
<?php
endModule(); // Now a function call
?>
我的 $pumps
数组的内容
Array
(
[1] => Array
(
[OID] => 01PRO
[Title] => ProphetX
[Description] => The Sequential Prophet X is a potent fusion of samples-plus-synthesis and is Dave Smith�s most ground-breaking evolution of the Prophet yet.
[Option] =>
[Price] => ,500
[Image] =>
)
[2] => Array
(
[OID] => 03REV
[Title] => ProphetRev 2
[Description] => The Prophet Rev2 is Dave Smith�s reimagining of his Prophet �08 poly synth � a modern classic that has appeared on countless recordings and stages since its debut in 2007
[Option] =>
[Price] => ,000
[Image] =>
)
[3] => Array
(
[OID] => 03REV
[Title] => Rev2 Desktop
[Description] => The Prophet Rev2 desktop module is just as powerful and easy to use as its counterpart, the Prophet Rev2 keyboard.
[Option] =>
[Price] => ,500
[Image] =>
)
[4] => Array
(
[OID] => 04OB6
[Title] => OB-6
[Description] => The OB-6� is a once-in-a-lifetime collaboration between the two most influential designers in poly synth history, Dave Smith and Tom Oberheim.
[Option] =>
[Price] => ,000
[Image] =>
)
[5] => Array
(
[OID] => 05OB6
[Title] => OB-6 Desktop
[Description] => The OB-6� desktop module is just as powerful and easy to use as its counterpart, the OB-6 Keyboard.
[Option] =>
[Price] => ,500
[Image] =>
)
[6] => Array
(
[OID] => 06PR6
[Title] => Prophet 6
[Description] => The Prophet-6 is Dave Smith�s tribute to the poly synth that started it all�the Sequential Prophet-5. But it�s not simply a reissue of a classic. Rather, as Dave puts it, �It�s the result of our effort to build the most awesome-sounding, modern analog poly synth possible.
[Option] =>
[Price] => ,500
[Image] =>
)
[7] => Array
(
[OID] => 07PR6
[Title] => Prophet 6 Desktop
[Description] => The Prophet-6 desktop module is every bit as powerful and easy to use as its counterpart, the Prophet-6 Keyboard. The module has all of the same controls as the keyboard version and provides the same immediacy and ease of use � with absolutely no menu diving. As with the Prophet-6 Keyboard, all parameters are at your fingertips, with full-sized knobs and switches and a comfortable, intuitive layout.
[Option] =>
[Price] => ,000
[Image] =>
)
[8] => Array
(
[OID] => 08PR2
[Title] => Pro 2
[Description] => The Pro 2 is Dave Smith�s flagship mono synth, and as Dave himself puts it, his �most powerful mono synth ever.�
[Option] =>
[Price] => ,000
[Image] =>
)
[9] => Array
(
[OID] => 09TEM
[Title] => Tempest
[Description] => Tempest is the brainchild of legendary instrument designers, Dave Smith and Roger Linn.
[Option] =>
[Price] => ,000
[Image] =>
)
)
$_SESSION['cart']
的内容
Array
(
[0] =>
[1] => 2
[2] => 9
)
代码比较乱,仅供学习,谢谢!
您的代码在第 71 行失败,即 <?php echo $_POST[$ino]['qty']; ?>
,因此您的 $pumps
变量没有问题,但您的 $_POST 变量不包含值为 $ino
的键。所以你必须检查这个变量并相应地改变你的索引。
$ino = $_POST['submit'];
行也没有效果,因为你用 foreach ( $_SESSION['cart'] as $ino )
覆盖了这个变量
要确保它存在,请执行以下操作:
foreach ( $_SESSION['cart'] as $ino ) {
if (isset($pumps[$ino])) {
?>
<tr>
<td>
Name: <?php echo $pumps[$ino]['Title']; ?> // error here
</td>
<td>
Price: <?php echo $pumps[$ino]['Price']; ?> // error here
</td>
<td>
Qty: <?php echo $_POST[$ino]['qty']; ?> // error here
</td>
<td>
<button type='submit' name='delete' value='<?php echo $ino; ?
>'>Remove</button>
</td>
</tr>
<?php
$total += $pumps[$ino]['Price']; // error here
}
} // end foreach
?>
我是 php 的新手,正在尝试创建购物车页面。
我得到了使用 $_POST 数组发送的产品 ID、选项和价格。
我希望 $ino
保存 Id 的值- 我这样做了。
我遇到一个重复出现的错误。
Notice: Undefined index: 3 in /home/sl0/s3196040/public_html/wp/a3/cart.php on line 84 Name: Notice: Undefined index: 3 in /home/sl0/s3196040/public_html/wp/a3/cart.php on line 71 Price: Notice: Undefined index: 3 in /home/sl0/s3196040/public_html/wp/a3/cart.php on line 74**
我知道我想 "isset" $ino 检查它是否设置,但我想我有?
如果我像 $pumps[3]['Title']
这样手动输入值,它可以工作,但如果我像这样 $pumps[$ino]['Title']
.
这是在 $_POST 数组中 ( [Id] => 3 [option] => english [qty] => 3
define("TITLE", "Product Page");
include_once('tools.php');
topModule('Business Name - Home', 'showSpecials()');
print_r($pumps[3]['Price']);
print_r($_POST);
// if i have the ID of the product, i need to turn it into a variables that the cart can display
if (isset ( $_POST ['Id'] )) {
// Check the item is not already in the cart
if (!in_array($_POST ["Id"], $_SESSION['cart'])) {
// Add new item to cart
$_SESSION ['cart'][] = $_POST["Id"];
}
}
else if (isset ( $_POST ['delete'] )) { // a remove button has been clicked
// Remove the item from the cart
if (false !== $key = array_search($_POST['delete'], $_SESSION['cart'])) {
unset($_SESSION['cart'][$key]);
}
// Empty Cart
else if (isset ( $_POST ["delete"] )) { // remove item from cart
unset ( $_SESSION ['cart'] );
}
}
if (isset ( $_SESSION ["cart"] )) {
if (isset($_POST['submit'])) {
$ino = $_POST['submit'];
}
?>
<form action='(omitted link)'
target='_blank' method='post'
enctype=''>
<table>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Action</th>
</tr>
<?php
// Set a default total
$total = 0;
foreach ( $_SESSION['cart'] as $ino ) {
?>
<tr>
<td>
Name: <?php echo $pumps[$ino]['Title']; ?> // error here
</td>
<td>
Price: <?php echo $pumps[$ino]['Price']; ?> // error here
</td>
<td>
Qty: <?php echo $_POST[$ino]['qty']; ?> // error here
</td>
<td>
<button type='submit' name='delete' value='<?php echo $ino; ?>'>Remove</button>
</td>
</tr>
<?php
$total += $pumps[$ino]['Price']; // error here
} // end foreach
?>
Total: $<?php echo $total; ?>
<tr>
<td colspan="2">Total: $<?php echo($total); ?></td>
<td><input type='submit' value='Checkout' /></td>
</tr>
<tr>
<td><button type='submit' name='clear'>Clear cart</button></td>
</tr>
</table>
</form>
<?php } ?>
<?php
endModule(); // Now a function call
?>
我的 $pumps
数组的内容
Array
(
[1] => Array
(
[OID] => 01PRO
[Title] => ProphetX
[Description] => The Sequential Prophet X is a potent fusion of samples-plus-synthesis and is Dave Smith�s most ground-breaking evolution of the Prophet yet.
[Option] =>
[Price] => ,500
[Image] =>
)
[2] => Array
(
[OID] => 03REV
[Title] => ProphetRev 2
[Description] => The Prophet Rev2 is Dave Smith�s reimagining of his Prophet �08 poly synth � a modern classic that has appeared on countless recordings and stages since its debut in 2007
[Option] =>
[Price] => ,000
[Image] =>
)
[3] => Array
(
[OID] => 03REV
[Title] => Rev2 Desktop
[Description] => The Prophet Rev2 desktop module is just as powerful and easy to use as its counterpart, the Prophet Rev2 keyboard.
[Option] =>
[Price] => ,500
[Image] =>
)
[4] => Array
(
[OID] => 04OB6
[Title] => OB-6
[Description] => The OB-6� is a once-in-a-lifetime collaboration between the two most influential designers in poly synth history, Dave Smith and Tom Oberheim.
[Option] =>
[Price] => ,000
[Image] =>
)
[5] => Array
(
[OID] => 05OB6
[Title] => OB-6 Desktop
[Description] => The OB-6� desktop module is just as powerful and easy to use as its counterpart, the OB-6 Keyboard.
[Option] =>
[Price] => ,500
[Image] =>
)
[6] => Array
(
[OID] => 06PR6
[Title] => Prophet 6
[Description] => The Prophet-6 is Dave Smith�s tribute to the poly synth that started it all�the Sequential Prophet-5. But it�s not simply a reissue of a classic. Rather, as Dave puts it, �It�s the result of our effort to build the most awesome-sounding, modern analog poly synth possible.
[Option] =>
[Price] => ,500
[Image] =>
)
[7] => Array
(
[OID] => 07PR6
[Title] => Prophet 6 Desktop
[Description] => The Prophet-6 desktop module is every bit as powerful and easy to use as its counterpart, the Prophet-6 Keyboard. The module has all of the same controls as the keyboard version and provides the same immediacy and ease of use � with absolutely no menu diving. As with the Prophet-6 Keyboard, all parameters are at your fingertips, with full-sized knobs and switches and a comfortable, intuitive layout.
[Option] =>
[Price] => ,000
[Image] =>
)
[8] => Array
(
[OID] => 08PR2
[Title] => Pro 2
[Description] => The Pro 2 is Dave Smith�s flagship mono synth, and as Dave himself puts it, his �most powerful mono synth ever.�
[Option] =>
[Price] => ,000
[Image] =>
)
[9] => Array
(
[OID] => 09TEM
[Title] => Tempest
[Description] => Tempest is the brainchild of legendary instrument designers, Dave Smith and Roger Linn.
[Option] =>
[Price] => ,000
[Image] =>
)
)
$_SESSION['cart']
的内容
Array
(
[0] =>
[1] => 2
[2] => 9
)
代码比较乱,仅供学习,谢谢!
您的代码在第 71 行失败,即 <?php echo $_POST[$ino]['qty']; ?>
,因此您的 $pumps
变量没有问题,但您的 $_POST 变量不包含值为 $ino
的键。所以你必须检查这个变量并相应地改变你的索引。
$ino = $_POST['submit'];
行也没有效果,因为你用 foreach ( $_SESSION['cart'] as $ino )
要确保它存在,请执行以下操作:
foreach ( $_SESSION['cart'] as $ino ) {
if (isset($pumps[$ino])) {
?>
<tr>
<td>
Name: <?php echo $pumps[$ino]['Title']; ?> // error here
</td>
<td>
Price: <?php echo $pumps[$ino]['Price']; ?> // error here
</td>
<td>
Qty: <?php echo $_POST[$ino]['qty']; ?> // error here
</td>
<td>
<button type='submit' name='delete' value='<?php echo $ino; ?
>'>Remove</button>
</td>
</tr>
<?php
$total += $pumps[$ino]['Price']; // error here
}
} // end foreach
?>