如何扩展从 MySQL 导入的类别列表
how to expand a category list imported from MySQL
我从 phpMyAdmin 中的两个不同 table 导入了一组不同的值,一个来自类别 table(其中 cat_id 是 PK),另一个来自项目 table;其中(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
table 有一个名为 item_name
和 item_description
的字段。您的 table 可能 具有不同的列名称,您需要更改它以匹配您的列名称。
有关 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
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='c.php?id=$cat_idd[$i]'>".$cat_namee[$i]."(".$run.")</a><br><br>";
}
?>
<?php
require('config.php');
if(isset($_GET['id']))
{
$cat_id = $_GET['id'];
$que = "SELECT * FROM item WHERE cat_id = '$cat_id'";
$run = mysql_query($que);
?>
<br><br>
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?> />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>
我从 phpMyAdmin 中的两个不同 table 导入了一组不同的值,一个来自类别 table(其中 cat_id 是 PK),另一个来自项目 table;其中(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
table 有一个名为 item_name
和 item_description
的字段。您的 table 可能 具有不同的列名称,您需要更改它以匹配您的列名称。
有关 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
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='c.php?id=$cat_idd[$i]'>".$cat_namee[$i]."(".$run.")</a><br><br>";
}
?>
<?php
require('config.php');
if(isset($_GET['id']))
{
$cat_id = $_GET['id'];
$que = "SELECT * FROM item WHERE cat_id = '$cat_id'";
$run = mysql_query($que);
?>
<br><br>
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?> />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>