Php 从数据库收集所有信息
我为我妻子的珠宝网站创建了一个数据库。当我试图从数据库中收集产品页面的信息时,我只能得到我放在数据库中的最后一项。我最初是从一个教程中获得代码的,为了获得任何项目,我必须对其进行操作。基本上我需要访问所有的产品,但我只有一个。有人能告诉我我错过了什么吗 用于显示项目的代码:Php 从数据库收集所有信息,php,mysqli,Php,Mysqli,我为我妻子的珠宝网站创建了一个数据库。当我试图从数据库中收集产品页面的信息时,我只能得到我放在数据库中的最后一项。我最初是从一个教程中获得代码的,为了获得任何项目,我必须对其进行操作。基本上我需要访问所有的产品,但我只有一个。有人能告诉我我错过了什么吗 用于显示项目的代码: $id = ''; if( isset( $_GET['id'])) { $id = $_GET['id']; } $sql = "SELECT * FROM products WHERE cate
$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>
.jpg“width=“142”height=“188”alt=”“/>
基本解决方案:
尝试将数据分配给数组,而不是这样的变量:
while($row=$result->fetch_assoc()){
$item_number[]=$row[“item_number”];
$price[]=$row[“price”];
$desc[]=$row[“description”];
$category[]=$row[“category”];
}
假设您正在尝试显示所有项目。您可以使用函数为所有项目生成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; ?>
你应该得到所有属于附件的物品。你只输出了物品编号
,虽然看起来像。你只得到一个或多个物品编号吗?虽然你必须对每件物品重复echo,但你只为第一件物品做。谢谢!这很有魅力!所有的产品都出来了。我妻子会非常高兴的。我只有最后一个问题。当我运行代码时,我收到一个错误,上面写着:注意:C:\xampp\htdocs\pinkys_pearls\accessories.php中的数组到字符串转换,在第85行$Array上,如何使其消失并显示信息?因为您可能会回显数组而不是数组大小写例如:检查是否回显$price而不是$desc[$I]或者只需给出代码,我将解决它:)