从数据库收集所有信息

时间:2018-09-03 15:53:34

标签: php mysqli

我为我的妻子珠宝网站创建了一个数据库。当我尝试从数据库中收集产品页面的信息时,我只能获取我放入数据库中的最后一个项目。我最初是从教程中获得代码的,因此必须对其进行处理才能获得所有项目。基本上,我需要访问所有产品,但是我只能得到一个。有人可以告诉我我所缺少的吗?

显示项目的代码:

$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"];
    }
}

这是表格的代码:

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

2 个答案:

答案 0 :(得分:0)

基本解决方案: 尝试将数据分配给数组而不是像这样的变量:

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

,然后使用for来显示HTML表,如下所示:

<?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		
	}
?>

我用$ item_number [$ i]替换了您的$ pid。

答案 1 :(得分:0)

假设您尝试显示所有项目。您可以使用一个函数为所有项目生成html,并在函数外部声明一个包含所有html的变量。然后,您可以在页面的任何地方回显该信息。

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