我一直在努力解决这个问题,所以我最终决定寻求帮助。我正在尝试编写一个由 mysql 数据库中的信息填充的 html 列表。我正在尝试编写一个 php 循环(或多个循环)来完成此操作。它开始变得复杂,因为列表中有多个节点。我尝试了许多不同的方法,但无法获得预期的结果。任何帮助将不胜感激!
该列表由具有各种“类别”和“子类别”的项目填充。该列表应按 ASC 顺序按客户名称、类别、子类别和零件编号排列。有些项目只有一个类别,没有子类别。有些项目没有类别,应该只是列在客户的名字下。所以列表应该是这样的......
客户
1 - 类别
1 - 子类别
1 - 第 1 部分(包含类别和子类别的项目)
- 第 2 部分
- 子类别
2 -第 3 部分
- 类别2
- 第 4 部分(只有一个类别的项目) - 第 5 部分
-第 6 部分(
没有类别或子类别的项目
- 第 7 部分
客户
2 - 类别
1 - 子类别 1
- 第 1 部分(包含类别和子类别的项目)
- 第 2 部分
- 子类别2
-第 3 部分
ETC......
希望这足够清楚。
这是我第一次尝试解决这个问题,它接近了。它只是将物品放在错误的地方(不知道为什么)。
<?php
$con = mysql_connect("localhost:3306","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("pcu_ops", $con);
$sql_customer="SELECT DISTINCT customer FROM common_parts ORDER BY customer ASC";
$result_customer=mysql_query($sql_customer,$con);
if (!mysql_query($sql_customer,$con))
{
die('Error: ' . mysql_error());
}
?>
<table border="0">
<colgroup>
<col width="300px" valign="top">
<col width="90%">
</colgroup>
<tr>
<td valign="top">
<!-- Add a <div> element where the tree should appear: -->
<div id="common_parts_tree">
<ul>
<?php
while ($row_customer = mysql_fetch_array($result_customer)) {
echo '<li class="expanded folder">'.$row_customer['customer'];
echo '<ul>';
$customer=$row_customer['customer'];
$sql_category="SELECT DISTINCT category FROM common_parts WHERE customer='$customer' ORDER BY customer ASC";
$result_category=mysql_query($sql_category,$con);
if (!mysql_query($sql_category,$con)) {
die('Error: ' . mysql_error());
}
while ($row_category = mysql_fetch_array($result_category)) {
if ($row_category['category'] != '') {
echo'<li class="expanded folder">'.$row_category['category'];
echo '<ul>';
$category=$row_category['category'];
$sql_subcategory="SELECT DISTINCT subcategory FROM common_parts WHERE (customer='$customer' AND category='$category') ORDER BY subcategory ASC";
$result_subcategory=mysql_query($sql_subcategory,$con);
if (!mysql_query($sql_subcategory,$con)) {
die('Error: ' . mysql_error());
}
while ($row_subcategory = mysql_fetch_array($result_subcategory)) {
if ($row_subcategory['subcategory'] != '') {
echo'<li class="expanded folder">'.$row_subcategory['subcategory'];
echo '<ul>';
$subcategory=$row_subcategory['subcategory'];
$sql_pn="SELECT DISTINCT pn FROM common_parts WHERE (customer='$customer' AND category='$category' AND subcategory='$subcategory') ORDER BY pn ASC";
$result_pn=mysql_query($sql_pn,$con);
if (!mysql_query($sql_pn,$con)) {
die('Error: ' . mysql_error());
}
while ($row_pn = mysql_fetch_array($result_pn)) {
$pn=$row_pn['pn'];
echo '<li><a href="includes/phpscripts/part_quick_view.php?pn='.$pn.'&customer='.$customer.'" target="contentFrame">'.$pn.'</a>';
}
echo '</ul>';
}
else {
if ($row['subcategory'] == '') {
$sql_pn="SELECT DISTINCT pn FROM common_parts WHERE (customer='$customer' AND category='$category') ORDER BY pn ASC";
$result_pn=mysql_query($sql_pn,$con);
if (!mysql_query($sql_pn,$con)) {
die('Error: ' . mysql_error());
}
while ($row_pn = mysql_fetch_array($result_pn)) {
$pn=$row_pn['pn'];
echo '<li><a href="includes/phpscripts/part_quick_view.php?pn='.$pn.'&customer='.$customer.'" target="contentFrame">'.$pn.'</a>';
}
}
}
}
echo '</ul>';
}
else {
echo '<li><a href="includes/phpscripts/part_quick_view.php?pn='.$pn.'&customer='.$customer.'" target="contentFrame">'.$pn.'</a>';
}
}
echo '</ul>';
}
?>
</div>
</td>
<td>
<iframe src="" name="contentFrame" width="100%" height="500" scrolling="yes" marginheight="0" marginwidth="0" frameborder="0">
<p>Your browser does not support iframes</p>
</iframe>
</td>
</tr>
<tr>
<td colspan="2">
<center>
<form id="rcv_common_parts">
<input type="hidden" id="pn" name="pn"/>
<input type="hidden" id="customer" name="customer"/>
<table class="table">
<tr>
<td>Quantity to Receive:</td>
<td><input type="text" name="qty" /></td>
</tr>
</table>
</form>
</center>
</td>
</tr>
</table>
这是我最近的尝试。我放弃了第一种方法并开始尝试使用此文件。仍然没有任何运气。
<?php
$con = mysql_connect("localhost:3306","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("pcu_ops", $con);
$sql="SELECT * FROM common_parts ORDER BY customer ASC, category ASC, subcategory ASC, pn ASC";
$result=mysql_query($sql,$con);
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
<table border="0">
<colgroup>
<col width="300px" valign="top">
<col width="90%">
</colgroup>
<tr>
<td valign="top">
<!-- Add a <div> element where the tree should appear: -->
<div id="common_parts_tree">
<ul>
<?php
$row = mysql_fetch_array($result);
echo '<li class="expanded folder">'.$row['customer'];
echo '<ul>';
while (($row['category'] != NULL) && ($row['subcategory'] != NULL)) {
echo '<li class="expanded folder">'.$row['category'];
echo '<ul>';
echo '<li class="expanded folder">'.$row['subcategory'];
echo '<ul>';
echo '<li><a href="includes/phpscripts/part_quick_view.php?pn='.$row['pn'].'&customer='.$row['customer'].'" target="contentFrame">'.$row['pn'].'</a>';
echo '</ul>';
echo '</ul>';
}
echo '</ul>';
?>
</div>
</td>
<td>
<iframe src="" name="contentFrame" width="100%" height="500" scrolling="yes" marginheight="0" marginwidth="0" frameborder="0">
<p>Your browser does not support iframes</p>
</iframe>
</td>
</tr>
<tr>
<td colspan="2">
<center>
<form id="rcv_common_parts">
<input type="hidden" id="pn" name="pn"/>
<input type="hidden" id="customer" name="customer"/>
<table class="table">
<tr>
<td>Quantity to Receive:</td>
<td><input type="text" name="qty" /></td>
</tr>
</table>
</form>
</center>
</td>
</tr>
</table>
当然希望有人能帮忙!
谢谢!