2017-02-24 1 views
0

Ce que j'ai: My current viewComment afficher les données de la base de données MySQL dans un site Web PHP?

Ce que je veux: 6 items next to each other from the same database

Malheureusement, ma question précédente n'a pas répondu dans la mesure que je le voulais, donc voici une autre question.

Le code:

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

    ?> 

Merci pour l'aide.

+2

votre instruction if doit être if ($ formatting == 6) not $ formatting = 6. == compare deux valeurs, tandis que = assigne simplement une valeur et retourne vrai à chaque fois – Dimi

Répondre

0

Je suis un peu confus sur votre boucle en utilisant itérations .. Utilisez comme ça, plus propre .. aller chercher toutes vos lignes bien^_^

$result = $dbc->query("SELECT * FROM shop"); 
while($row = $result->fetch_assoc()){ 
echo "$row['item_name']"; 
} 
0

Je l'ai réparé. Voici le code.

<?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) 
{ 
    #<a href='page2.php?id=2489&user=tom'>link to page2</a> 


    # 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 = 1; 
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
    { 
     echo $formatting; 
    if ($formatting == 5){ 
    echo '<td width=20%><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 = 1; 
    } 
    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 ++; 
    } 
    } 


    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

courte est:

echo '<table><tr>'; 

$formatting = 0; 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 

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

    if ($formatting == 6) { 
     $formatting = 0; 
     echo '</tr><tr>'; 
    } 

    $formatting++; 
} 


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