0

我有一个在线书店,我在那里卖书。在这里,我将购物车的数据存储在会话中。When two or more books are selected there may not be all the books available. 我只能检查一本书是否可用。But I can't make it through when two or more books selected for ordering. 我为一本书所做的是:

    $result = mysql_query("SELECT id FROM book_table WHERE id IN ($cart)");
while ($row = mysql_fetch_array($result)) {
    $q=mysql_num_rows(mysql_query("SELECT available FROM book_table WHERE id='$row[0]'"));
    if($q!=0){
        //order goes fore processing.
    }
    else{
        //books not available.
        $q2=mysql_fetch_array(mysql_query("SELECT name FROM book_table WHERE id='$row[0]'"));
        echo "The book: $q2[0] Not Available";
        //If there is more than 1 books in the cart and one or two books of them are not in the stock, its showing the order cant be processed(But I want to show which books aren't available).
    }
}

这里 $cart 是在 1,2,3,4 这样的会话中保存的书籍购物车。现在我需要通过重定向另一个页面来显示哪些书不可购买。

谁能帮我解决这个问题?

4

4 回答 4

0
$all_available = true;
$orders = array();
$result = mysql_query("SELECT id, available, name FROM book_table WHERE id IN ($cart)");
while ($row = mysql_fetch_assoc($result)) {
  if ($row['available')) {
    $orders[] = $row['id'];
  } else {
    $all_available = false;
    echo "The book: " . $row['name'] . " is not available.\n"
  }
}
if ($all_available) {
  // Process $orders
} else {
  // Send redirect
}
于 2013-05-18T05:51:07.323 回答
0

你可以像下面这样,

$query=mysql_query("SELECT id,available FROM book_table WHERE id IN ($cart) ");
$books_available_num = mysql_num_rows($query);
$books_order_num = count(explode(",",$cart));//i think there should be better way to do this...
if($books_available_num==0){
    //no book is available
}elseif($books_available_num==$books_order_num ){
    //all books are available
    while($fetch=mysql_fetch_array($query)){
        //order done successfully. 
    }

}else{
    //some are available some are not.
    while($fetch=mysql_fetch_array($query)){
        if($fetch['status']=="available"){
             //place in order
        }else{
            //this is not available 
        }
    }
}
//rest of code. I think now you can do yourself.

我告诉你方法,你可以提高自己。代码可能包含错误,您可以改进它。

于 2013-05-18T05:39:55.677 回答
0

创建一个数组并将 id 和可用性作为键和值对并检查每个 id 的可用性。

于 2013-05-18T05:41:41.780 回答
0

您可以使用 ajax,而无需重定向到其他页面

$.ajax({
          url     :'yourpage.php',
          type    :'post',
          sucess  : function(data){ alert(data)}
       })

你的 php 代码需要一些修改

    $result = mysql_query("SELECT id FROM book_table WHERE id IN ($cart)");
    $error  = array();
while ($row = mysql_fetch_array($result)) {
$q=mysql_num_rows(mysql_query("SELECT available FROM book_table WHERE id='$row[0]'"));
if($q!=0){
    //order goes fore processing.
}
else{
    //books not available.
    $q2=mysql_fetch_array(mysql_query("SELECT name FROM book_table WHERE id='$row[0]'"));
    $error[] = "The book: $q2[0] Not Available";
    //If there is more than 1 books in the cart and one or two books of them are not in the stock, its showing the order cant be processed(But I want to show which books aren't available).
}

  if (!empty(error))
  {
        // print array as you want
  }
  else
   {
       echo "order successfully placed"
   }
}

查看更多关于jquery ajax

如果您想$error在会话中重定向到其他页面。我认为其他任何事情都无法帮助您

注意 :避免使用 mysql_* 因为它们已被弃用

于 2013-05-18T05:46:14.180 回答