dongsaolian8786 2013-11-10 20:02
浏览 42
已采纳

我无法以正确的方式放置最后一个div。 而Loop

i was unable to place last div in proper way.

my output from database for query:

SELECT a.id, a.name, b.sub_id, b.sub_name FROM cat a, sub_cat b WHERE a.id = b.cat_id

will be:

|  id  |  name     | sub_id | sub_name                  |
|  1   | moblie    |   1    | Phone                     |
|  1   | mobile    |   2    | Mobile Phones Accessories |
|  2   | Lifestyle |   3    | Arts                      |
|  2   | Lifestyle |   4    | Books                     |

i want to make this structure:

    <div id="moblie" class="tab-pane active">
<a href="?cat=1&amp;sub_cat=1">Phones</a>
<a href="?cat=1&amp;sub_cat=2">Mobile Phones Accessories</a>
</div>
<div id="Lifestyle " class="tab-pane">
<a href="?cat=2&amp;sub_cat=3">Arts</a>
<a href="?cat=2&amp;sub_cat=4">Books</a>
</div>

My Original Php Code :

<?php $query = "SELECT a.id, a.name, b.sub_id, b.sub_name FROM cat a, sub_cat b WHERE a.id = b.cat_id";
        $result = mysql_query($query);
        $i=0; 
        $a=1; 
        $b=0; 
        while($row = mysql_fetch_assoc($result)){
            $i++;
            if($i == 1){$set = "active";}else{$set = NULL;}
            $hre=strtolower(preg_replace("/[^a-zA-Z]+/", "", $row['name'])); 
            while($a == $row['id']){ $a++;?>
            <div id="<?=$hre?>" class="tab-pane <?=$set?>">
            <?php }?>
            <a href="?cat=<?=$row['id']?>&sub_cat=<?=$row['sub_id']?>"><?=$row['sub_name']?></a>
            <?php while($b == $row['id']){ $b++;?></div>
            <?php }
        }?>

Output I am receiving :

<div id="mobilephones" class="tab-pane active">
  <a href="?cat=1&amp;sub_cat=1">Mobile Phones</a>
  <a href="?cat=1&amp;sub_cat=2">Mobile Phones Accessories</a>
<div id="homelifestyle" class="tab-pane ">
  <a href="?cat=2&amp;sub_cat=3">Art - Collectibles - Hobbies</a>
  <a href="?cat=2&amp;sub_cat=4">Books - Magazines</a>
  </div></div>

My Idea : as it was last while loop how can it understand upcoming value to print div.

I have tried in different ways not not found solution.Please help me out.

Note:I know how to make it with loop quires.but,I want to do it through single mysql query.

  • 写回答

2条回答 默认 最新

  • dongpenggan6812 2013-11-10 20:12
    关注

    Important

    Avoid using mysql_* functions! They are deprecated.

    For your solution, you can simply use this way:

    <?php
        $result = mysql_query($query);
        $flag = 0;
        while (false !== ($data = mysql_fetch_array($result)))
        {
            if ($data["id"] != $flag)
            {
                if ($data["id"] != 1) echo "</div>
    ";
                echo '<div id="' . $data["name"] . '" class="tab-pane', ($data["id"] == 1) ? ' active' : '', '">', "
    ";
            }
            echo '<a href="?cat=' . $data["id"] . '&amp;sub_cat=' . $data["sub_id"] . '">' . $data["sub_name"] . '</a>', "
    ";
            $flag = $data["id"];
        }
    ?></div>
    

    Output

    <div id="mobile" class="tab-pane active">
      <a href="?cat=1&amp;sub_cat=1">Phone</a>
      <a href="?cat=1&amp;sub_cat=2">Mobile Phones Accessories</a>
    </div>
    <div id="Lifestyle" class="tab-pane">
      <a href="?cat=2&amp;sub_cat=3">Arts</a>
      <a href="?cat=2&amp;sub_cat=4">Books</a>
    </div>
    

    This should probably do the trick. Let me know the results in the comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化