为什么我在 php 中出现未定义变量错误
why do i get undefined variable error in php
我正在尝试为手机创建一个小型购物车,我已经到了可以使用的阶段,现在我正在尝试在我的 header 中反映我的 $item_total 成本我有我的图标然后我得到这个未定义的变量错误,
请注意,这个 $item_total 变量在我的程序下面起作用,但拒绝在我的 header 中回显,那里有我的购物车图标。
我会更详细地粘贴我的代码
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="add/jquery.mobile-1.4.5.min.css" />
<link rel="stylesheet" href="themes/jqm-icon-pack-fa.css" />
<link rel="stylesheet" href="themes/mine.css" />
<link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
<script src="add/jquery.js"></script>
<script src="add/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<br /> <div class="ui-bar-b" data-role="navbar">
<ul>
<li><a href="#" data-icon="bars" data-theme="c">elete</a></li>
<li><a href="#" data-icon="shopping-cart" data-theme="d" data-ajax="false"><?php
echo "$".$item_total; ?>
// **my cart in the header where i try to echo my total
and i get the undefined error** //View Cart
</a></li>
</ul>
</div>
<!-- /header -->
<div role="main" class="ui-content">
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="app.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<br />
<form method="post" action="accept.php">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["name"]; ?><br /><input type="hidden" name="itemname[]" value="<?php echo $item["name"]; ?>"></strong></td>
<td><?php echo $item["code"]; ?></td>
<td><?php echo $item["quantity"]; ?><br /><input type="hidden" name="itemqt[]" value="<?php echo $item["quantity"]; ?>"><input type="hidden" name="itemcd[]" value="<?php echo $item["code"]; ?>"></td>
<td align=right><?php echo "$".$item["price"]; ?><br /><input type="hidden" name="itemprice[]" value="<?php echo $item["price"]; ?>"></td>
<td><a href="app.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><p><strong>Total:</strong>
<br /><input type="text"
value="<?php echo "$".$item_total; ?>"
name="total">// **this is where its echoed without an error**
//
</p>
<p>
<input type="submit" name="button" id="button" value="Submit">
</p></td>
</tr>
</tbody>
</table> </form>
<?php
}
?>
</div>
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="app.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
</div>
</div><!-- /content -->
<!-- /page -->
</body>
</html>
我的问题是为什么我一开始就收到这个错误,然后它又起作用了。
使用phpisset
在起始页添加此代码
<?php
start_session(); //ignore if your session already statred
foreach ($_SESSION["cart_item"] as $item){
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<li>
<a href="#" data-icon="shopping-cart" data-theme="d" data-ajax="false">
<?php
if(isset($item_total))
echo "$".$item_total;
?>
或尝试此代码:-
if(isset($item_total)){
echo "$".$item_total;
}else{
echo "price not available";
}
使用此代码:-
<li>
<a href="#" data-icon="shopping-cart" data-theme="d" data-ajax="false">
<?php
if(isset($item_total)){
echo "$".$item_total;
}else{
echo "[=12=]";
}
问题是您在 header 之后定义 $item_total 并在其之前回显(在 Header 中)。所以基本上如果你在定义之前使用变量 PHP 会抛出一个未定义的错误。您可以做的是编写一个单独的 class 来计算 $item_total 并在 class 中执行所有购物逻辑。在 header 之前包含 class,这样您将在 header 中获得 $item_total 值,并使用 header.Hope 中的值来解决您的问题。
我正在尝试为手机创建一个小型购物车,我已经到了可以使用的阶段,现在我正在尝试在我的 header 中反映我的 $item_total 成本我有我的图标然后我得到这个未定义的变量错误, 请注意,这个 $item_total 变量在我的程序下面起作用,但拒绝在我的 header 中回显,那里有我的购物车图标。 我会更详细地粘贴我的代码
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="add/jquery.mobile-1.4.5.min.css" />
<link rel="stylesheet" href="themes/jqm-icon-pack-fa.css" />
<link rel="stylesheet" href="themes/mine.css" />
<link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
<script src="add/jquery.js"></script>
<script src="add/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<br /> <div class="ui-bar-b" data-role="navbar">
<ul>
<li><a href="#" data-icon="bars" data-theme="c">elete</a></li>
<li><a href="#" data-icon="shopping-cart" data-theme="d" data-ajax="false"><?php
echo "$".$item_total; ?>
// **my cart in the header where i try to echo my total
and i get the undefined error** //View Cart
</a></li>
</ul>
</div>
<!-- /header -->
<div role="main" class="ui-content">
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="app.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<br />
<form method="post" action="accept.php">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["name"]; ?><br /><input type="hidden" name="itemname[]" value="<?php echo $item["name"]; ?>"></strong></td>
<td><?php echo $item["code"]; ?></td>
<td><?php echo $item["quantity"]; ?><br /><input type="hidden" name="itemqt[]" value="<?php echo $item["quantity"]; ?>"><input type="hidden" name="itemcd[]" value="<?php echo $item["code"]; ?>"></td>
<td align=right><?php echo "$".$item["price"]; ?><br /><input type="hidden" name="itemprice[]" value="<?php echo $item["price"]; ?>"></td>
<td><a href="app.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><p><strong>Total:</strong>
<br /><input type="text"
value="<?php echo "$".$item_total; ?>"
name="total">// **this is where its echoed without an error**
//
</p>
<p>
<input type="submit" name="button" id="button" value="Submit">
</p></td>
</tr>
</tbody>
</table> </form>
<?php
}
?>
</div>
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="app.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
</div>
</div><!-- /content -->
<!-- /page -->
</body>
</html>
我的问题是为什么我一开始就收到这个错误,然后它又起作用了。
使用phpisset
在起始页添加此代码
<?php
start_session(); //ignore if your session already statred
foreach ($_SESSION["cart_item"] as $item){
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<li>
<a href="#" data-icon="shopping-cart" data-theme="d" data-ajax="false">
<?php
if(isset($item_total))
echo "$".$item_total;
?>
或尝试此代码:-
if(isset($item_total)){
echo "$".$item_total;
}else{
echo "price not available";
}
使用此代码:-
<li>
<a href="#" data-icon="shopping-cart" data-theme="d" data-ajax="false">
<?php
if(isset($item_total)){
echo "$".$item_total;
}else{
echo "[=12=]";
}
问题是您在 header 之后定义 $item_total 并在其之前回显(在 Header 中)。所以基本上如果你在定义之前使用变量 PHP 会抛出一个未定义的错误。您可以做的是编写一个单独的 class 来计算 $item_total 并在 class 中执行所有购物逻辑。在 header 之前包含 class,这样您将在 header 中获得 $item_total 值,并使用 header.Hope 中的值来解决您的问题。