English 中文(简体)
困难 PHP MySQL同时为html清单提供住所
原标题:Difficult PHP MySQL while loop for html list

我一直在为此而努力,似乎永远如此,因此我最后决定要求提供帮助。 我正试图撰写一份在我sql数据库中以Info为生的html名单。 我正试图写出一个Sandop(或多处)来做到这一点。 它开始变得复杂,因为名单上有多个节点。 我尝试了许多不同的方法,但无法取得预期结果。 任何帮助都将受到高度赞赏。

清单中包含有各种“类别”和“类别”的项目。 清单应当按客户名称排列,然后按类别列出,然后是次类,然后是部分编号。 有些项目只有一类,没有子类。 有些项目没有类别,只应在客户名下列出。 因此,名单应当照此办理......

Customer1
    - Category1
        - Subcategory1
                - Part1 (Item w/ Category & Subcategory)
                - Part2
        - Subcategory2
                - Part3
    - Category2
        - Part4 (Item with only a Category)
        - Part5
    - Part6 (Item with no Category or Subcategory
    - Part7
Customer2
    - Category1
        - Subcategory1
                - Part1 (Item w/ Category & Subcategory)
                - Part2
        - Subcategory2
                - Part3

......

Hopefully that is clear enough.

这是我第一次尝试解决这一问题,并且已经结束。 它只是把物品放在错误的地方(而不是为什么)。

    <?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>

这是我最近的尝试。 我放弃了第一种方法,并开始尝试这一档案。 仍然有uck。

<?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>

相信希望的人能够帮助!

感谢!

最佳回答

您应采用复读算法。

A tip for database... Simple example with categories and parts only:

 - 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

页: 1

接着是:

 $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>";
}
问题回答

你们颁布的第一部法典似乎ok,但太长了,我急切地表示了这一点。 基本来说,你们的所有需要都是在休息期间,就所有类别、子类而言。

我知道你的数据库结构,因此,你可能需要改变一些栏目的名称,但这项工作除外:

$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) You have a users table. That table should have an id field that is an int 11, primary key, auto-increment. You will have more fields but the id is all I am concerned with.
2) You need a categories table to determine if the category is a "parent" category or "child" category. The structure should basically be something like id (int 11, primary key, auto-increment), name (varchar), user_id (int 11), parent_id (int 11). In your requirements above, Category 1, Category 2, etc... are "parent" categories so the parent_id should be 0 meaning they have no parent. For subcategories, you should use the id of the category that is it a subcategory of. For example if Category 1 has an id of 1, a sub-category of that category would have a parent_id of 1.
3) Finally you have a parts table. Again have an id (int 11, primary key, auto-increment), user_id (int 11), and a category_id (int 11). You can obviously put other fields here but those are the three I am concerned with. If the part is not in a category, category_id is 0, otherwise use the id of the category or subcategory.

That gives you a sound database structure for mapping relationships for users, categories, and parts. Now, how do you get all that data into something usable? An array. Build an associative array that helps you display the data like you want it. Without writing a million lines of PHP, here is the gist.

$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;
            }
        }
    }
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签