我一直在努力解决这个问题,所以我最终决定寻求帮助。我正在尝试编写一个由mysql数据库中的信息填充的html列表。我正在尝试编写一个php循环(或多个循环)来完成此任务。它开始变得复杂,因为列表中有多个节点。我尝试了很多不同的方法,但无法获得理想的结果。任何帮助将不胜感激!
列表由具有各种“类别”和“子类别”的项填充。该列表应按ASC顺序按客户名称,然后是类别,然后是子类别,然后是部件号。有些项目只有一个类别,没有子类别。有些商品没有类别,应该只列在客户名下。所以列表应该是这样的......
customer1表
- 类别1
- 子类别1
- 第1部分(项目与类别和子类别)
- 第2部分
- 子类别2
- 第3部分
- 类别2
- 第4部分(只有类别的项目)
- 第5部分
- 第6部分(没有类别或子类别的项目
- 第7部分
顾客2
- 类别1
- 子类别1
- 第1部分(项目与类别和子类别)
- 第2部分
- 子类别2
- 第3部分
等......
希望这很清楚。
这是我第一次尝试这个问题而且接近了。它只是将物品放在错误的地方(不知道为什么)。
<?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>
当然希望有人可以提供帮助!
谢谢!
答案 0 :(得分:0)
你发布的第一个代码似乎没问题,但它太长了,我无法弄明白。基本上你需要的是while循环中的while循环,依此类推所有类别,子类别等......
我不知道你的数据库结构,所以你可能需要更改一些列名,但除此之外应该有效:
$query1 = "the customer query";
while($customerRow = mysql_fetch_array(mysql_query($query1,$con))) {
echo '<ul><li>';
echo $customerRow['customer'];
$query2 = "the category query";
while($categoryRow = mysql_fetch_array(mysql_query($query2,$con))) {
echo '<ul><li>';
echo $categoryRow['category'];
$query3 = "the subcategory query";
while($subcategoryRow = mysql_fetch_array(mysql_query($query3,$con))) {
echo '<ul><li>';
echo $subcategoryRow['subcategory'];
echo '</li></ul>';
}
echo '</li></ul>';
}
echo '</li></ul>';
}
答案 1 :(得分:0)
你应该实现递归算法。
数据库提示...... 仅包含类别和部件的简单示例:
- Category1 - Subcategory1 - Part1 - Part2 - Subcategory2 - Part3 - Category2 - Subcategory3 - Part4 - Part5 - Part6 - Part7 - Part8
Categories_table id name parent user_id 1 category 1 0 1 2 category 2 0 1 3 Subcategory1 1 1 4 Subcategory2 1 1 5 Subcategory3 2 1
Parts_table name category Part1 3 Part2 3 Part3 4 Part4 5 Part5 2 Part6 2 Part7 0 Part8 0
你明白了......
然后是show:
$query_user = mysql_query("select * from users",$con);
while($row_user = mysql_fetch_array($query_user)) {
echo "<ul>".$row_user['id'];
show_category($row_user['id'],0);
echo "</ul>";
}
function show_category($user,$parent){
global $con;
echo "<ul>";
$query_cat = mysql_query("select * from categories where user_id=".$user." and parent=".$parent,$con);
while($row_cat = mysql_fetch_array($query_cat)) {
echo "<li>".$row_cat['name'];
show_category($user,$row_cat['id']);
echo "</li>";
}
$query_parts = mysql_query("select * from parts where category=".$parent,$con);
while($row_parts = mysql_fetch_array($query_parts)) {
echo "<li>".$row_parts['name']."</li>";
}
echo "</ul>";
}
答案 2 :(得分:0)
这将是一个很长的答案,但我想我可以帮助你。首先,让我们直截了当地了解您将如何设置数据库。我知道你的例子是'客户',但我的例子中我称之为'用户':
1)您有一个用户表。该表应该有一个'id'字段,它是一个int 11,主键,自动增量。你会有更多的字段,但'id'就是我所关心的。
2)您需要一个“类别”表来确定该类别是“父”类别还是“子”类别。结构基本上应该是'id'(int 11,主键,自动增量),'name'(varchar),'user_id'(int 11),'parent_id'(int 11)。在您的上述要求中,类别1,类别2等等是“父”类别,因此parent_id应为0表示它们没有父类。对于子类别,您应该使用类别的id作为子类别。例如,如果类别1的id为1,则该类别的子类别的parent_id为1
3)最后你有一个零件表。再次有一个'id'(int 11,主键,自动增量),'user_id'(int 11)和'category_id'(int 11)。显然你可以把其他领域放在这里,但那些是我关注的三个领域。如果零件不在类别中,则category_id为0,否则使用类别或子类别的id。
这为您提供了一个完善的数据库结构,用于映射用户,类别和部件的关系。现在,您如何将所有数据转化为可用的数据?数组。构建一个关联数组,帮助您按需要显示数据。如果不写一百万行PHP,这就是要点。
$arr_user = array();
$sql = mysql_query('SELECT * FROM users ORDER BY name ASC', $con);
while($user = mysql_fetch_object($sql))
{
$arr_user[$user->id]['name'] = $user->name;
$sql2 = mysql_query('SELECT * FROM parts WHERE user_id='.$user->id.' AND category_id=0 ORDER BY name ASC', $con);
while($part = mysql_fetch_object($sql2))
{
$arr_user[$user->id]['parts'][$part->id] = $part->name;
}
$sql3 = mysql_query('SELECT * FROM categories WHERE user_id='.$user->id.' AND parent_id=0 ORDER BY name', $con);
while($category = mysql_fetch_object($sql3))
{
$sql4 = mysql_query('SELECT * FROM parts WHERE user_id='.$user->id.' AND category_id='.$category->id.' ORDER by name', $con);
while($part = mysql_fetch_object($sql4))
{
$arr_user[$user->id]['category'][$category->id]['name'] = $category->name;
$arr_user[$user->id]['category'][$category->id]['parts'][$part->id] = $part->name;
}
$sql5 = mysql_query('SELECT * FROM categories WHERE parent_id='.$category->id.' ORDER BY name', $con);
while($subcat = mysql_fetch_object($sql5))
{
$sql6 = mysql_query('SELECT * FROM parts WHERE category_id='.$subcat->id.' ORDER BY name', $con);
while($part = mysql_fetch_array($sql6))
{
$arr_user[$user->id]['category']['subcat'][$subcat->id]['name'] = $subcat->name;
$arr_user[$user->id]['category']['subcat'][$subcat->id]['parts'][$part->id] = $part->name;
}
}
}
}
if( sizeof( $arr_user ) )
{
foreach( $arr_user as $k=>$v )
{
echo $v['name']; //echo the user's name
if( isset( $v['category'] ) && sizeof( $v['category'] ) )
{
foreach( $v['category'] as $ck=>$cv )
{
echo $cv['name']; //echo category name
if( isset( $cv['subcat']) && sizeof( $cv['subcat'] ) )
{
foreach( $cv['subcat'] as $sk=>$sv )
{
echo $sv['name']; //echo subcat name
if( isset( $sv['parts'] ) && sizeof( $sv['parts'] ) )
{
foreach( $sv['parts'] as $spk=>$spv )
{
echo $spv; //echo part name
}
}
}
}
if( isset($cv['parts']) && sizeof($cv['parts']) )
{
foreach( $cv['parts'] as $cpk=>$cpv )
{
echo $cpv; //echo part name
}
}
}
}
if( isset($v['parts']) && size($v['parts']) )
{
foreach( $v['parts'] as $pk=>$pv )
{
echo $pv;
}
}
}
}