簡體   English   中英

從mysql獲取主要類別的子類別

[英]Getting sub categories from mysql for the main category

我在從mysql數據庫中檢索子類別時遇到問題。我想顯示父類別的子類別。 我只能獲得主要類別的最后一個子類別。 前幾個子類別未顯示**。 在我的表中,**我有category_id和category_parent_id。其中category_parent_id的父類別為'0'。 。提前致謝

 <ul class="betterList">
 <?php 
        $con = mysql_connect("localhost","root","pwd") or die('couldnot connect to database'.mysql_error());
               mysql_select_db("DB",$con);
               $result=mysql_query("select * from table ")or die("No table available with this name"."<br/><br/>".mysql_error());
             while($row=mysql_fetch_array($result))
               {
                $parent_id=$row['category_parent_id'];
                $category_id=$row['category_id'];
                if($parent_id==0)

                {
?>
                <li>
                <?php echo $row['category_id'].$row['name_en-GB'];

                $result1=mysql_query("select * from table where category_parent_id=".$category_id)or die("No data available with this name"."<br/><br/>".mysql_error());
                 echo  $num_row    = mysql_num_rows($result1);

                    if($num_row>0) {
                        for($i=0;$i<$num_row;$i++)
                        {
                             while($row1=mysql_fetch_array($result1))
                             {

                  ?>
                                     <ul style="margin:0px;padding:0;">
                                        <li><?php echo $row1['name_en-GB']?></li>
                                     </ul>
                                     <?php
                             }
                        }
                    }

                ?>


                </li>

                <?php } ?>



    <?php }?> 
 </ul>

當我刪除結尾處的<li>標記並在結尾處保留之后時,雖然我可以顯示所有子類別,但css並未為此申請。 那里出了點問題,但我無法弄清楚

刪除以下內容,然后重試:

for($i=0;$i<$num_row;$i++)
{

哇 ! o_O您正在使用舊的mysql_ *函數...

您寫道: for($i=0;$i<$num_row;$i++)

和之后: while($row1=mysql_fetch_array($result1))

這兩條指令都在此查詢得到的每一行上循環。

刪除所有這些:

echo  $num_row    = mysql_num_rows($result1);

if($num_row>0) {
    for($i=0;$i<$num_row;$i++) {

因為這是沒有用的。

循環結果的唯一重要的事情是while($ row1 = mysql_fetch_array($ result1))

您也可以將mysql_fetch_array()替換為更輕便的mysql_fetch_assoc()。

您的代碼將被優化,但這應該可以解決您的問題。

與其進行嵌套循環,不如通過聯接獲取所有內容:

SELECT t1.category_id parent, t1.`name_en-GB` parent_name,
       t2.category_id child, t2.`name_en-GB` child_name
FROM table t1
JOIN table t2 ON t2.parent_category_id = t1.category_id
WHERE t1.parent_category_id = 0

那么您的循環將是:

$last_parent = null;
$first_cat = true;
while ($row = mysql_fetch_assoc($result)) {
    if ($row['parent'] != $last_parent) {
        $last_parent = $row['parent'];
        if (!$first_cat) { // Close out the last UL and LI
            echo '</ul></li>';
        } else {
            $first_cat = false;
        }
        echo '<li>' . $row['parent'] . $row['parent_name'];
        echo '<ul style="margin:0px;padding:0;">';
    }
    echo '<li>' . $row['child_name'] . </li>
}
if (!$first_cat) {
    echo '</ul></li>';
}

您的代碼中有太多的嵌套循環:您有一個forwhile循環,它們都試圖循環內部查詢的行。 另外,您將每個孩子放在自己的<ul> ,這可能不是您想要的。

請嘗試嘗試此解決方案是否適用於您,如果可行,請相應地調整您的代碼

        $result = mysql_query("select * from table WHERE category_parent_id = 0 ");

        while($row=mysql_fetch_array($result)) {

            $parent_id = $row['category_parent_id'];
            $query = mysql_query("select * from table where category_parent_id = {$parent_id}");

            while($sub_cats=mysql_fetch_array($query)) {
                echo '<pre>'.print_r($sub_cats).'</pre>';
            }

        } 

僅通過在while循環之前添加內部<ul>即可得到子類別。

        <?php 
          echo "<ul>";
          while($row1=mysql_fetch_array($result1))
             {
         ?>
               <li><?php echo $row1['name_en-GB']?></li>
                <?php
             }
                echo " </ul>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM