<?php
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["ids"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["ids"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["ids"], $_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty){
$result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
mysql_query("INSERT INTO cart(cookieId, id, qty) values('" . GetCartId() . "', $itemId, $qty)");
//printf ("Inserted records: %d\n", mysql_affected_rows());
}
else
{
// This item already exists in the users cart,
// we will update it instead
mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
}
}
function UpdateItem($itemId, $qty)
{
mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
//printf ("Updated records: %d\n", mysql_affected_rows());
}
function RemoveItem($itemId)
{
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
}
echo $ids;
?>
<?php
function ShowCart()
{
$result = mysql_query("SELECT
cart.id cart_id,
cart.id cart_id,
cart.cartId cartId,
cart.cookieId cookie_Id,
cart.qty qt_y,
cdkb.id cdkb_id,
cdkb.name name,
cdkb.image image,
cdkb.price price,
dkb.id dkb_id,
dkb.name name1,
dkb.image image1,
dkb.price price2,
dbl.product_id product_id,
dbl.price price3,
dbl.variety variety,
dbl.description description
FROM
cart
LEFT OUTER JOIN cdkb
ON cart.id = cdkb.id
LEFT OUTER JOIN dkb
ON cart.id = dkb.id
LEFT OUTER JOIN dbl
ON dbl.id = dkb.id
WHERE
cart.cookieId ='" . GetCartId() . "' ' ORDER BY cdkb.name AND dkb.name ASC");?>
<div id="cart">
<div id="group">
<div id="quantity">Qty</div>
<div id="cartpic">Pic</div>
<div id="product">Product</div>
<div id="cartprice">Price</div>
<div id="remove">Remove</div>
</div>
<?php
$totalCost=0;
foreach($result as $row)
{
if($row['ckb_id']){
// Increment the total cost of all items
$totalCost += ($row["qt_y"] * $row["price"]);
?>
<div id="cart1">
<select name="<?php echo $row["cdkb_id"];?>" onChange="UpdateQty(this)">
<?php print($row["cdkb_id"]);?>
<?php
for($i = 1; $i <= 30; $i++)
{
echo "<option ";
if($row["qt_y"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</div>
<div id="cart2">
<img src="images/logopic.gif"<?php /*?><?php echo $row["image"]; ?><?php */?> alt="we" width="60" height="50" />
</div>
<div id="cart3"><p><?php echo $row["name"]; ?></p></div>
<div id="cart4"><p>
$<?php echo number_format($row["price"], 2, ".", ","); ?></p></div>
<div id="cart5">
<p><?php
printf('<a href="cart.php?action=remove_item&id=%d&ids=%d®ister=%s">Remove</a>', $_GET['id'], $row['cdkb_id'], $_GET['register']);
?></p></div>
<hr size="1" color="red" >
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
<?php
}
?>
<?php
else {
?>
<?php // Increment the total cost of all items
$totalCost += ($row["qt_y"] * $row["price2"]);
?>
<div id="cart1">
<select name="<?php echo $row["dkb_id"];?>" onChange="UpdateQty(this)">
<?php print($row["dkb_id"]);?>
<?php
for($i = 1; $i <= 30; $i++)
{
echo "<option ";
if($row["qt_y"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</div>
<div id="cart2">
<img src="images/logopic.gif"<?php /*?><?php echo $row["image2"]; ?><?php */?> alt="we" width="60" height="50" />
</div>
<div id="cart3"><p><?php echo $row["name2"]; ?></p></div>
<?php foreach ($row['dkb_id'] as $variety) {?>
<div id="cart4">
<?php echo"<p>".$variety['variety']. " ~ " . $variety['price3'] . "</p>" ?>
<p>$<?php echo number_format($row["price3"], 2, ".", ","); ?></p></div>
<?php } ?>
<div id="cart5">
<p><?php
printf('<a href="cart.php?action=remove_item&id=%d&ids=%d®ister=%s">Remove</a>', $_GET['id'], $row['dkb_id'], $_GET['register']);
?></p></div>
<hr size="1" color="red" >
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
<font face="verdana" size="2" color="black" style="clear:right;">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>
<?php } ?>
<?php } ?>
<div id="shopping">
<a href=<?php echo "exa2.php?id=".intval($id). '®ister='. $_GET['register']. ""?>> Keep Shopping</a></div>
<?php /*?><tr>
<td colspan="4">
<font face="verdana" size="1" color="black">
<a href=<?php echo "cart.php?id=".intval($id). '®ister='. $_GET['register']. ""?>>Your Shopping Cart</a></font></td>
</tr><?php */?>
</tr>
</table>
</table>
嗨,对于那些评论过的人 Help with a unexpected T_ELSE 这是我无法在其他帐户中发布的后续帖子,因为可访问性问题。
在上面的代码中,查询会有一个if和else语句序列导致解析errror:语法错误,因为它会导致Parse错误:语法错误,意外T_ELSE。查看if和else语句设计以及可能导致此错误的原因。
if {
for (blabla) {
if {
}// end of if statement inside the for loop
} //end of for loop.
} end of if statement
else{ // line 379
}// end of else statement
我认为导致上面的T_ELSE语法错误正如我在上一篇文章中所说的那样是for循环中的if语句。 Php认为else语句与for循环中的if语句一起工作,我想要的是for循环外的if语句和else语句一起工作。错误如下所示:
PS可能导致解析错误的所有内容都低于查询。感谢。
if和else之间的预期显示看起来像
foreach($result as $row)
{
if (table cdkb) {
[1]qyt image name price remove
1 --- marina $18.90 remove?
}end of if statement
Else table dkb {
1]qyt Image Name Price Remove
1 --- marina Small Tray $18.90 remove?
Medium Tray $30.24
Large Tray $35.90
} // end of else statement
}//end of while loop
这是INTENDED显示后面的数组结构!
<?php
$result = array(
'0' => array('cdkb_id' => 'id 1',
'image' => 'img 1',
'name' => 'name 1',
'price' => 'price 1'
),
'1' => array('image1' => 'img 2',
'name1' => 'name 2',
'dkb_id' => array('0' => array('variety' => 'variety 2-1',
'price3' => 'price 2-1'
),
'1' => array('variety' => 'variety 2-2',
'price3' => 'price 2-2'
),
'2' => array('variety' => 'variety 2-3',
'price3' => 'price 2-3'
)
)
),
'2' => array('cdkb_id' => 'id 3',
'image' => 'img 3',
'name' => 'name 3',
'price' => 'price 3'
),
'3' => array('image1' => 'img 4',
'name1' => 'name 4',
'dkb_id' => array('0' => array('variety' => 'variety 4-1',
'price3' => 'price 4-1'
),
'1' => array('variety' => 'variety 4-2',
'price3' => 'price 4-2'
),
'2' => array('variety' => 'variety 4-3',
'price3' => 'price 4-3'
)
唯一的问题是T_ELSE,因为for循环中的if和else语句之间有一个if ....
感谢。
答案 0 :(得分:2)
注意这一点:
<?php
}
?>
<?php
else {
?>
您是否尝试过不将if和else的结尾分成两个不同的代码块?我强烈怀疑解析器将保持这种上下文:第一个块用于终止if块,并且因为它不读取else,所以它假定你完成了它。因此,您在下一个块中的其他内容与if。
断开连接其他人会以某种方式连接到最里面的想法是完全错误的。如果可能的话,语法会太破碎了。
答案 1 :(得分:0)
您的代码肯定需要进行重大清理。缩进不是你在牙医诊所听到的。它用于为代码提供可视化结构,并使代码在一系列嵌套括号中一目了然。你基本上有 NO 缩进,除了奇怪的是,mysql查询,格式很精美。
由于PHP吐出了“意外的其他”,这意味着你的某个地方有太多或太少的括号,或者有这样的东西:
if ($condition) {
} else {
} else {
}
清理你的代码,使用正确的格式,我建议使用一个编辑器,比如VI,它可以帮助你找到丢失/额外的括号(VI中的'%'命令,在匹配的括号之间反弹)。
答案 2 :(得分:0)
@Mark Byers下面有一个更简单的上面代码版本,它有三个if语句两个在else之前,一个用于每个循环,另一个在for循环中我希望foreach循环中的一个用于配对else语句。
<?php
$totalCost=0;
foreach($result as $row)
{
if($row['ckb_id']){ if statement that will match the else statement..
// Increment the total cost of all items
$totalCost += ($row["qt_y"] * $row["price"]);
?>
<div id="cart1">
<select name="<?php echo $row["cdkb_id"];?>" onChange="UpdateQty(this)">
<?php print($row["cdkb_id"]);?>
<?php
for($i = 1; $i <= 30; $i++)
{
echo "<option ";
if($row["qt_y"] == $i) // if statement inside the for loop
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</div>
<div id="cart2">
<img src="images/logopic.gif"<?php /*?><?php echo $row["image"]; ?><?php */?> alt="we" width="60" height="50" />
</div>
<div id="cart3"><p><?php echo $row["name"]; ?></p></div>
<div id="cart4"><p>
$<?php echo number_format($row["price"], 2, ".", ","); ?></p></div>
<div id="cart5">
<p><?php
printf('<a href="cart.php?action=remove_item&id=%d&ids=%d®ister=%s">Remove</a>', $_GET['id'], $row['cdkb_id'], $_GET['register']);
?></p></div>
<hr size="1" color="red" >
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
<?php
} // end of if statement
?>
<?php
else { // beginning of else statement
?>
<?php // Increment the total cost of all items
$totalCost += ($row["qt_y"] * $row["price2"]);
?>
<div id="cart1">
<select name="<?php echo $row["dkb_id"];?>" onChange="UpdateQty(this)">
<?php print($row["dkb_id"]);?>
<?php
for($i = 1; $i <= 30; $i++)
{
echo "<option ";
if($row["qt_y"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</div>
<div id="cart2">
<img src="images/logopic.gif"<?php /*?><?php echo $row["image2"]; ?><?php */?> alt="we" width="60" height="50" />
</div>
<div id="cart3"><p><?php echo $row["name2"]; ?></p></div>
<?php foreach ($row['dkb_id'] as $variety) {?>
<div id="cart4">
<?php echo"<p>".$variety['variety']. " ~ " . $variety['price3'] . "</p>" ?>
<p>$<?php echo number_format($row["price3"], 2, ".", ","); ?></p></div>
<?php } ?>
<div id="cart5">
<p><?php
printf('<a href="cart.php?action=remove_item&id=%d&ids=%d®ister=%s">Remove</a>', $_GET['id'], $row['dkb_id'], $_GET['register']);
?></p></div>
<hr size="1" color="red" >
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
<font face="verdana" size="2" color="black" style="clear:right;">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>
<?php } ?>
<?php } ?>
<div id="shopping">
<a href=<?php echo "exa2.php?id=".intval($id). '®ister='. $_GET['register']. ""?>> Keep Shopping</a></div>
<?php /*?><tr>
<td colspan="4">
<font face="verdana" size="1" color="black">
<a href=<?php echo "cart.php?id=".intval($id). '®ister='. $_GET['register']. ""?>>Your Shopping Cart</a></font></td>
</tr><?php */?>
</tr>
</table>
</table>