我目前正在创建一个演示注册系统(我会注意到这不是作业或作业)我希望如果用户已购买该项目,则禁用“添加到购物车”(并且之前的项目,它反映在数据库中
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
//Query to get item information
$results = $mysqli->query("SELECT * FROM events ORDER BY eventid");// WHERE sort = '$sort'
$remaining = $mysqli->query("SELECT * FROM orders");
$obj_r = $remaining->fetch_object(); // obj_r means objects remaining
//Query to disable already purchaced items
$QgetSignups = $mysqli->query("SELECT s.eventid, o.status FROM signups s JOIN orders o ON s.orderid = o.orderid WHERE s.userid = $userid AND o.status = 1");
$signedup = array();
while($row = mysqli_fetch_assoc($QgetSignups))
//while($row = $QgetSignups->fetch_array(MYSQLI_NUM))
{
array_push($signedup,$row["eventid"]);
}
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="images/'.$obj->image.'"></div>';
echo '<div class="product-content"><h4>'.$obj->name.'</h4>';
echo '<div class="product-desc">'.$obj->description.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->price.' | ';
echo 'Items Left: '.$obj->max.' | ';
echo '<input type="hidden" style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />'; // OLD CODE TO DISPLAY QTY BOX: Qty: <input style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />';
if($signupAmount[$row['eventid']] >= $row['max'])
{
echo '<button class="add_to_cart">Add To Cart</button>';
}
if(in_array($row['eventid'],$signedup))
{
print_r($signedup);
//echo '<button DISABLED class="add_to_cart">Add To Cart</button>';
}
echo '</div></div>';
echo '<input type="hidden" name="product_code" value="'.$obj->name.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
脚本当前尝试检查所购买的商品是否与“eventid”匹配(即代表商品编号的数字)。到目前为止,我试图通过将项目编号推入数组,然后使用in_array来检查它,但是如果我使用方法while($row = mysqli_fetch_assoc($QgetSignups))
,则in_array不起作用(它拉动了数组编号,但是in_array没有正确检查它是否存在),如果我使用//while($row = $QgetSignups->fetch_array(MYSQLI_NUM))
,则array_push不会收集值,但是in_array方法会尝试显示数组(尽管它是空的)。
答案 0 :(得分:0)
您没有在当前while循环中迭代$ row,其中存在以下代码。
if (in_array($row['eventid'], $signedup)) {
print_r($signedup);
//echo '<button DISABLED class="add_to_cart">Add To Cart</button>';
}
我不确定您的数据库架构是如何设置的,但可能应该如下:
if (in_array($obj->eventid, $signedup)) {
print_r($signedup);
//echo '<button DISABLED class="add_to_cart">Add To Cart</button>';
}