首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从数据库中收集所有信息

从数据库中收集所有信息
EN

Stack Overflow用户
提问于 2018-09-03 23:53:34
回答 2查看 59关注 0票数 0

我为我妻子的珠宝网站创建了一个数据库。当我试图从数据库中收集产品页面的信息时,我只能获得我放入数据库中的最后一项。我最初是从一个教程中获得代码的,为了获得任何项目,我必须在它上面工作。基本上,我需要访问所有的产品,但我只有一个。有人能告诉我我错过了什么吗?

调出这些项目的代码:

代码语言:javascript
复制
$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"];
    }
}

下面是表格的代码:

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2018-09-04 00:08:45

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

代码语言:javascript
复制
while($row = $result->fetch_assoc()) {
    $item_number[] = $row["item_number"];
    $price[] = $row["price"];
    $desc[] = $row["description"];
    $category[] = $row["category"];
}

然后使用for以如下方式显示HTML表:

代码语言:javascript
复制
<?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		
	}
?>

我把你的$pid换成了$item_number$i。

票数 0
EN

Stack Overflow用户

发布于 2018-09-04 00:21:45

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

代码语言:javascript
复制
<?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; ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52152997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档