我从 phpMyAdmin 的两个不同表中导入了一组不同的值,一个来自类别表(其中 cat_id 是主键),另一个来自项目表;其中(cat_id 是 FK)。
<?php
require ('config.php');
$que = "SELECT * FROM category";
$run = mysql_query($que);
$j = 0;
while($row = mysql_fetch_array($run))
{
$cat_idd[$j] = $row['cat_id'];
$cat_namee[$j] = $row['cat_name'];
$j++;
}
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
}
?>
这是我目前所拥有的:
现在我该如何扩展它?或者,例如,如何在同一页面或下一页显示存储在名为 Clothing 的类别中的两个项目?由于此代码仅帮助我查看存储在数据库中的项目数,
这是我的表格:
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr bgcolor = 'yellow'>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr class = "odd">
<?php
require ('config.php');
$que = "SELECT * FROM category";
$run = mysql_query($que);
$j = 0;
while($row = mysql_fetch_array($run))
{
$cat_idd[$j] = $row['cat_id'];
$cat_namee[$j] = $row['cat_name'];
$j++;
}
我想到了使用数组函数将值存储在类别中,即 Clothing(2) => 2
。
function array_insert(&$array, $position, $insert)
{
if (is_int($position)) {
array_splice($array, $position, 0, $insert);
} else {
$pos = array_search($position, array_keys($array));
$array = array_merge(
array_slice($array, 0, $pos),
$insert,
array_slice($array, $pos)
);
}
}
$arr = array();
$arr[] = 2;
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]'";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
array_insert($arr, 0, "$run");
// echo $arr[0];
}
$que2 = "SELECT * FROM item WHERE cat_id = '1'"; //instead of using '1' i wish to use the one user clicks on!
$res2 = mysql_query($que2);
while($row = mysql_fetch_array($res2))
{
$i_id = $row['item_id'];
$i_namee = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_namee; ?></td>
<td><?php echo $i_price; ?></td>
<td><input type="checkbox" name="addcart" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php } ?>
<br><br>
</table>
<input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart" style="float:right; margin-top: 30px; margin-right: 10px;">Add to Cart</button>
上图是我想要的结果,这个是针对 Clothing(2) 的。我希望它随着用户点击其他内容而改变,例如 Shoes(1)。
最佳答案
您已发出正确的查询以获取有关某项的所有数据信息。但是,您没有获取每个字段的内容,而是选择了只计算受影响的行数。通过使用 mysql_fetch_array()
,您实际上可以获取每个字段的内容并可以显示结果。
根据您的代码考虑以下示例:
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
while($row = mysql_fetch_array($result)){
//Change ['item_name'] and ['item_description'] to match your DB schema.
echo "item name: $row['item_name']";
echo "item description: $row['item_description']";
}
}
$row
是一个包含您要查找的所有数据的数组(既是关联的又是数字索引的)。在我的示例中,我假设 items
表有一个名为 item_name
和 item_description
的字段。您的表可能具有不同的列名称,您需要更改它以匹配您的列名称。
关于 mysql_fetch_array()
的更多信息:http://php.net/manual/en/function.mysql-fetch-array.php
最后,您应该知道 mysql
扩展自 PHP 5.5 起已弃用。您应该认真考虑迁移到 mysqli
或 PDO
。
关于 mysqli
的更多信息:http://php.net/manual/en/book.mysqli.php
关于 PDO
的更多信息:http://php.net/manual/en/book.pdo.php
关于php - 如何扩展从 MySQL 导入的类别列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733388/