如何以6行输出SQL数据



我的问题是,通过PHP输出的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' ) ;
    ?>

尝试以下代码,这将在每六行之后创建一个新表。您可以进行相应的更改。

<?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' ) ;
    ?>

相关内容

  • 没有找到相关文章

最新更新