2017-02-23 81 views
-1

我的問題是,通過PHP輸出的SQL數據顯示在一個垂直單列中。我怎樣才能輸出每行6個Sql數據

我想要的是要顯示的數據在6行的行中,然後創建一個新行,顯示下一個6,等等。

代碼:

<?php # DISPLAY COMPLETE PRODUCTS PAGE. 

# Access session. 
session_start() ; 

# Redirect if not logged in. 
if (!isset($_SESSION[ 'user_id' ])) { require ('login_tools.php') ; load() ; } 

# Set page title and display header section. 
$page_title = 'Shop' ; 
include ('includes/header.html') ; 

# Open database connection. 
require ('..\connect_db.php') ; 

# Retrieve items from 'shop' database table. 
$q = "SELECT * FROM shop" ; 
$r = mysqli_query($dbc, $q) ; 
if (mysqli_num_rows($r) > 0) 
{ 

    # Display body section. 
    echo '<UL> 
     <li><a href="shop_details.php?id=1">View Item</a></li> 
     <li><a href="shop_details.php?id=2">View Item</a></li>   
     <li><a href="shop_details.php?id=3">View Item</a></li> 
    </UL> 
    '; 


    #Displays every item in the store. 
    echo '<table><tr>'; 

$formatting = 0; 
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
    { 
     echo $formatting; 
     if ($formatting = 6){ 
     echo '<td><strong>' . $row['item_name'] .'</strong><br><span  style="font-size:smaller">'. 
$row['item_desc'] . '</span><br><img src='. $row['item_img'].' height=200px><br>$' . $row['item_price'] . 
'<br><a href="shop_details.php?id='.$row['item_id'].'">View Item</a></td></tr><tr>'; 
     $formatting = 0; 
    } 
    else 
    { 
     echo $formatting; 
     echo '<td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. 
$row['item_desc'] . '</span><br><img src='. $row['item_img'].' height=200px><br>$' . $row['item_price'] . 
'<br><a href="shop_details.php?id='.$row['item_id'].'">View Item</a></td>'; 
     $formatting = $formatting + 1; 
    } 
} 


echo '</tr></table>'; 

    # Close database connection. 
    mysqli_close($dbc) ; 
} 
    # Or display message. 
else { echo '<p>There are currently no items in this shop.</p>' ; } 

    # Create navigation links. 
    echo '<p><a href="cart.php">View Cart</a> | <a href="forum.php">Forum</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ; 

    # Display footer section. 
    include ('includes/footer.html') ; 

    ?> 
+0

SELECT * FROM shop LIMIT 6 –

+0

這個'if($ formatting = 6)'不會做你期望的!您正在分配,而不是比較。應該是'=='或'===' – Jeff

+0

謝謝你的迴應。它的工作原理,但只顯示數據庫的前6項,有沒有辦法創建另一行顯示下一個6? –

回答

0

嘗試下面的代碼,這將每六行之後創建新表。你可以相應地做出改變。

<?php # DISPLAY COMPLETE PRODUCTS PAGE. 

# Access session. 
session_start() ; 

# Redirect if not logged in. 
if (!isset($_SESSION[ 'user_id' ])) { require ('login_tools.php') ; load() ; } 

# Set page title and display header section. 
$page_title = 'Shop' ; 
include ('includes/header.html') ; 

# Open database connection. 
require ('..\connect_db.php') ; 

# Retrieve items from 'shop' database table. 
$q = "SELECT * FROM shop" ; 
$r = mysqli_query($dbc, $q) ; 
if (mysqli_num_rows($r) > 0) 
{ 



    # Display body section. 
    echo '<UL> 
     <li><a href="shop_details.php?id=1">View Item</a></li> 
     <li><a href="shop_details.php?id=2">View Item</a></li>   
     <li><a href="shop_details.php?id=3">View Item</a></li> 
    </UL> 
    '; 


    #Displays every item in the store. 
    echo '<table>'; 

$formatting = 0; 
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
    { 
     echo $formatting; 
    if ($formatting == 5){ 
    echo '</table><table><tr><td><strong>' . $row['item_name'] .'</strong><br><span  style="font-size:smaller">'. 
$row['item_desc'] . '</span><br><img src='. $row['item_img'].' height=200px><br>$' . $row['item_price'] . 
'<br><a href="shop_details.php?id='.$row['item_id'].'">View Item</a></td></tr>'; 
$formatting = 0; 
} 
else 
    { 
     echo $formatting; 
     echo '<tr><td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. 
$row['item_desc'] . '</span><br><img src='. $row['item_img'].' height=200px><br>$' . $row['item_price'] . 
'<br><a href="shop_details.php?id='.$row['item_id'].'">View Item</a></td></tr>'; 
$formatting ++; 
} 
     } 


    echo '</table>'; 

     # Close database connection. 
    mysqli_close($dbc) ; 
    } 
    # Or display message. 
    else { echo '<p>There are currently no items in this shop.</p>' ; } 

    # Create navigation links. 
    echo '<p><a href="cart.php">View Cart</a> | <a href="forum.php">Forum</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ; 

    # Display footer section. 
    include ('includes/footer.html') ; 

    ?> 
+0

我試過了這段代碼,它使數據出現在每列只有一個項目的列中。任何想法如何讓它在每一行顯示更多的項目? –