【发布时间】:2015-10-22 09:04:47
【问题描述】:
我从 phpMyAdmin 中的两个不同表中导入了一组不同的值,一个来自 Category 表(其中 cat_id 是 PK),另一个来自 Item 表;其中(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)。
【问题讨论】:
-
category是包含您提供的数据的物理表吗?还是一种观点?您能否提供有关您的物品存储方式的更多信息? -
@HPierce 是的,category 是一个物理表,其中存储了数据。项目根据类别 ID(来自类别表)存储到项目表。就像我说的,我在 MySQL 中有两个物理表
-
我现在已经编辑了帖子,提供了更多信息。