简体   繁体   中英

gathering all info from database

I created a database for my wifes jewelry site. When i tried to gather the information from the database for the product page I only was able to get the last item I had put inside the database. I originally got the code from a tutorial and had to work on it in order to get any items at all. Basically I need to access all the product but I only get one. Can someone show me what I am missing?

The code to bring up the items:

$id = ''; 
if( isset( $_GET['id'])) {
    $id = $_GET['id']; 
}       
$sql = "SELECT * FROM products WHERE category = 'Accessories'";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);

if ($result->num_rows > 0) {

    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $item_number = $row["item_number"];
        $price = $row["price"];
        $desc = $row["description"];
        $category = $row["category"];
    }
}

This is the code for the table:

<table width="100%" border="2" cellspacing="0" cellpadding="15">
    <tr>
        <td width="19%" valign="top"><img src="pictures/inventory/<?php echo $pid; ?>.jpg" width="142" height="188" alt="<?php echo $item_number; ?>" /><br />
            <a href="pictures/inventory/<?php echo $pid; ?>.pngjpg">View Full Size Image</a>
        </td>
        <td width="81%" valign="top">
            <h3 class="Item"><?php echo $item_number; ?></h3>
            <p>
                <?php echo "$".$price; ?>
                <br />
                <br />
                <?php echo $desc; ?>
                <br />
            </p>
            <form  id="form1" name="form1" method="post" action="cart.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $pid; ?>" />
                <input class="button" type="submit" name="button" id="button" value="Add to Shopping Cart" />
            </form>
        </td>
    </tr>
</table>

Basic Solution: try to assign the data to arrays instead of variables like that:

 while($row = $result->fetch_assoc()) { $item_number[] = $row["item_number"]; $price[] = $row["price"]; $desc[] = $row["description"]; $category[] = $row["category"]; } 

and then use a for to display your HTML table like that:

 <?php for ($i=0; $i <sizeof($item_number) ; $i++) { ?> <table width="100%" border="2" cellspacing="0" cellpadding="15"> <tr> <td width="19%" valign="top"><img src="pictures/inventory/<?php echo $item_number[$i]; ?>.jpg" width="142" height="188" alt="<?php echo $item_number[$i]; ?>" /><br /> <a href="pictures/inventory/<?php echo $item_number[$i]; ?>.pngjpg">View Full Size Image</a></td> <td width="81%" valign="top"><h3 class="Item"><?php echo $item_number[$i]; ?></h3> <p><?php echo "$".$price[$i]; ?><br /> <br /> <?php echo $desc[$i]; ?> <br /> </p> <form id="form1" name="form1" method="post" action="cart.php"> <input type="hidden" name="pid" id="pid" value="<?php echo $item_number[$i]; ?>" /> <input class="button" type="submit" name="button" id="button" value="Add to Shopping Cart" /> </form> </td> </tr> </table> <?php } ?> 

I replaced your $pid with $item_number[$i].

Assuming you are trying to show all items. You could use a function to generate the html for all the items and outside the function declare a variable containing all the html. You can then echo that anywhere in the page.

<?php

function generateTableHtml() {

$tableHtml  = '<table width="100%" border="2" cellspacing="0" cellpadding="15">';

while($row = $result->fetch_assoc()) {

    $tableHtml .= '<tr>';
    $tableHtml .= '<td width="19%" valign="top">';
    $tableHtml .= '<img src="pictures/inventory/' . $row['item_number'] . '.jpg" width="142" height="188" alt="' . $row['item_number'] . '" /><br />';
    $tableHtml .= '<a href="pictures/inventory/' . $row['item_number'] '.pngjpg">View Full Size Image</a></td>';
    $tableHtml .= '<td width="81%" valign="top"><h3 class="Item">' . $row['item_number'] . '</h3>';
    $tableHtml .= '<p>' . $row['price'] . '<br/><br/>';
    $tableHtml .= $row['description'];
    $tableHtml .= '<br />';
    $tableHtml .= '</p>';
    $tableHtml .= '<form  id="form1" name="form1" method="post" action="cart.php">';
    $tableHtml .= '<input type="hidden" name="pid" id="pid" value="'. $pid . '" />'
    $tableHtml .= '<input class="button" type="submit" name="button" id="button" value="Add to Shopping Cart" />';
    $tableHtml .= '</form>';
    $tableHtml .= '</td>';
    $tableHtml .= '</tr>';

}

$tableHtml .= '<table>';

return $tableHtml;

}


$tableHtml = generateTableHtml();



?>


<!-- the rest of your html -->

<?php echo $tableHtml; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM