2013-05-18 17 views
0

我有一個網上書店,我出售書籍。我在這裏存儲購物車的數據。當選擇兩本或更多本書時,可能沒有全部可用的書。我只能檢查一本可用或不可用的書。但是,當兩本或兩本以上的書籍被選中訂購時,我無法完成。我爲一本書做的是:來自MySQL的PHP​​可用性檢查Jquery

$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). 
    } 
} 

這裏$購物車是保存在像1,2,3,4會話的書籍車。 現在我需要通過重定向到另一個頁面來顯示哪些書無法購買。

任何人都可以幫我解決這個問題嗎?

+0

所以每個物理本書都有自己的行中的數據庫?對於一個給定的標題,掌握數量會不會更容易? –

回答

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. 

我告訴你的方法,你可以提高自己。代碼可能包含錯誤,您可以改進它。

0

創建一個數組並將id和availaibilty作爲鍵和值對,並檢查每個id的可用性。

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_ *,因爲他們已被棄用

+0

1數據庫查詢就夠了。超過1個查詢會導致應用程序變慢。看到我的查詢下面。 –

+0

@ web2students.com你是對的..但多數民衆贊成在OP問題,所以我沒有關心該 – alwaysLearn

+0

節省資金,節省內存和計算機硬盤和節約用水總是好習慣。 :P只是在開玩笑...... :) –

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 
} 
+0

是的,這一個適用於我,但我需要類似的東西會在10-15secs之後由ajax自動檢查它現在是否可用。有可能嗎? –

+0

你需要在客戶端使用AJAX來做到這一點。 – Barmar

+0

你能爲我做那個嗎? –