2015-01-10 35 views
0

如何將此表連接成3個部分?一個用於初學者,主菜和甜點。php/MySQL - 如何將產品表連接在一起

這裏是我到目前爲止的代碼:

<?php 

session_start(); 

$page = 'index.php'; 

mysql_connect('localhost','root','') or die(mysql_error()); 
mysql_select_db('cart') or die(mysql_error()); 

if (isset($_GET['add'])) { 
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add'])); 
    while ($quantity_row = mysql_fetch_assoc($quantity)) { 
     if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) { 
     $_SESSION['cart_' . (int)$_GET['add']] +='1'; 
     header('Location: ' . $page); 

    } 
} 
header('Location: ' . $page); 

} 

if (isset($_GET['remove'])) { 
    $_SESSION['cart_'.(int)$_GET['remove']]--; 
    header ('Location: ' . $page); 

} 

if (isset($_GET['delete'])) { 
    $_SESSION['cart_' . (int)$_GET['delete']]='0'; 
    header ('Location: ' . $page); 
} 



function products() { 
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC'); 
    if (mysql_num_rows($get) == 0) { 
     echo "There are no products to display."; 
    } 
    else { 

    while ($get_row = mysql_fetch_assoc($get)) {?> 
     <center> 
     <table border=2 width=90% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2> 
     <th>View</th> 
     <th>Dish</th> 
     <th>Description</th> 
     <th>Item Price</th> 
     <tr> 
     <td>  
     <a href="ice.png"</a> 
     </td> 
     <td> 
     <?echo '<p>'.$get_row['name']?> 
     </td> 
     <td> 
     <?echo $get_row['description']?> 
     </td> 
     <td> 
     <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> 
     </td> 
     </tr> 
     </table></center> 

    <?} 

} 


} 

這當前顯示:

enter image description here

但最好我不喜歡的標題爲每對項目!

+0

是不是簡單的HTML? (而不是所有這些標籤)? –

回答

0

請嘗試

?> 
<center> 
<table border=2 width=90% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2> 
<tr> 
<th>View</th> 
<th>Dish</th> 
<th>Description</th> 
<th>Item Price</th> 
<tr> 
<? 
while ($get_row = mysql_fetch_assoc($get)) {?> 
    <tr> 
    <td>  
    <a href="ice.png"</a> 
    </td> 
    <td> 
    <?echo '<p>'.$get_row['name']?> 
    </td> 
    <td> 
    <?echo $get_row['description']?> 
    </td> 
    <td> 
    <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> 
    </td> 
    </tr> 
<? 
} 
?> 
</table></center> 
<? 

,而不是while循環:

while ($get_row = mysql_fetch_assoc($get)) {?> 
    <center> 
    <table border=2 width=90% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2> 

    .............. 

    </table></center> 

<?} 

我希望它能幫助。

編輯

請考慮mysql PHP函數變爲mysqliPDO因爲由PHP團隊爲過時mysql家族標記。 在這裏您可以找到更多關於:mysqliPDO

1

您在while循環中調用table and table header。所以,出於這個原因。試試這個變化

function products() { 
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC'); 
    if (mysql_num_rows($get) == 0) { 
     echo "There are no products to display."; 
    } 
    else { 
     echo "<center>\n"; 
     echo " <table border=2 width=90% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2>\n"; 
     echo "  <tr>\n"; 
     echo "  <th>View</th>\n"; 
     echo "  <th>Dish</th>\n"; 
     echo "  <th>Description</th>\n"; 
     echo "  <th>Item Price</th>\n"; 
     echo "  </tr>\n"; 
     while ($get_row = mysql_fetch_assoc($get)) { 

     ?> 
     <tr> 
      <td>  <a href="ice.png"</a> </td> 
      <td> <?echo '<p>'.$get_row['name']?> </td> 
      <td> <?echo $get_row['description']?> </td> 
      <td> <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> </td> 
     </tr> 
     <? 
     } 
     echo "</table>\n"; 
     echo "</center>\n"; 
    } 
}   
+0

哇,非常感謝這很好!你知道我怎樣才能爲每件物品使用單獨的圖像? – Oscar

+0

哪裏會來Img src?如果按照產品,那麼'​​'改變'​​' –

+0

嗯,這似乎表明它在所有項目仍當我點擊它顯示的圖像:(你能不能幫我解決這個問題? – Oscar

-1

我想象你的表將有一個「類型」字段包含「啓動器」,「主」或「甜點」。如果沒有,我不知道你將如何區分產品。與只有頂部的表頭

$starters = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 AND type = 'starter' ORDER BY id DESC'); 

$mains = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 AND type = 'main' ORDER BY id DESC'); 

$desserts = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 AND type = 'dessert' ORDER BY id DESC'); 

然後在你的函數循環每個組分別:

我會得到首發,電源和甜點分別這樣第一。

function products() { 
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC'); 
    if (mysql_num_rows($get) == 0) { 
     echo "There are no products to display."; 
    } 
    else { 
    echo <<<HERE 
<center> 
     <table border=2 width=90% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2> 
     <th>View</th> 
     <th>Dish</th> 
     <th>Description</th> 
     <th>Item Price</th> 
     <tr> 
HERE; 

    while ($get_row = mysql_fetch_assoc($starters)) {?> 

     <td>  
     <a href="ice.png"</a> 
     </td> 
     <td> 
     <?echo '<p>'.$get_row['name']?> 
     </td> 
     <td> 
     <?echo $get_row['description']?> 
     </td> 
     <td> 
     <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> 
     </td> 
     </tr> 


    <?} 

    while ($get_row = mysql_fetch_assoc($mains)) {?> 

     <td>  
     <a href="ice.png"</a> 
     </td> 
     <td> 
     <?echo '<p>'.$get_row['name']?> 
     </td> 
     <td> 
     <?echo $get_row['description']?> 
     </td> 
     <td> 
     <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> 
     </td> 
     </tr> 


    <?} 

    while ($get_row = mysql_fetch_assoc($desserts)) {?> 

     <td>  
     <a href="ice.png"</a> 
     </td> 
     <td> 
     <?echo '<p>'.$get_row['name']?> 
     </td> 
     <td> 
     <?echo $get_row['description']?> 
     </td> 
     <td> 
     <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> 
     </td> 
     </tr> 


    <?} 
echo "</table></center>"; 
} 

這可能是重構,但我認爲這是你正在尋找的東西。