php中的树结构

时间:2013-12-06 12:38:34

标签: php jquery mysql jquery-dynatree

我正在尝试获得以下结构

  1. 图书

    a.Harry Potter

  2. 衣服

    一个。衬衫

  3. 从数据库中获取所有数据时,这样

    。我使用php和iquery做了这个,但我得到了以下结构 -

    1. 图书

      一个。哈利波特

      湾衬衫

    2. 衣服

      一个。哈利波特

      湾衬衫

    3. 所以任何人都可以告诉我哪里出错了。我的代码是

      <?php include 'dbconnect.php' ?>
      <?php include 'header.php' ?>
      <div id="wrapper">
          <div id="content">
              <div id="rightnow">
                  <h3 class="reallynow"></h3>
                  <br/>
                  <script src="jquery.js" type="text/javascript"></script>
                  <script src="jquery-ui.custom.js" type="text/javascript"></script>
                  <script src="jquery.cookie.js" type="text/javascript"></script>
                  <!-- Include the basic stylesheet: -->
                  <script src="jquery.dynatree.js" type="text/javascript"></script>
                  <script type="text/javascript">
                      $(function () {
                          $("#tree").dynatree({
                              title: "Sample Theming",
                              // Image folder used for data.icon attribute.
                              imagePath: "skin-custom/",
                              onActivate: function (node) {
                                  return true;
                              }
                          });
                      });
                  </script>
                  <?php
                  $x1 = array();
                  $x2 = array();
                  $sql_1 = mysqli_query($con, "SELECT * FROM jsrao_db4");
                  while ($row_1 = mysqli_fetch_array($sql_1)) {
                      array_push($x1, $row_1["cat_name"]);
                  }
                  $sql_2 = mysqli_query($con, "
                      SELECT * FROM jsrao_db5 WHERE pro_id = $i
                  ");
                  while ($row_2 = mysqli_fetch_array($sql_2)) {
                      array_push($x2, $row_2["pro_name"]);
                  }
                  ?>
                  <div id="tree">
                      <ul>
                          <?php foreach ($x1 as $p) {
                              echo "<li class='folder'>$p"; ?>
                              <ul>
                              <?php foreach ($x2 as $q) {
                                  echo "<li>$q";
                              } ?>
                              </ul><?php } ?>
                      </ul>
                  </div>
              </div>
          </div>
      </div> 
      

4 个答案:

答案 0 :(得分:0)

您是否尝试关闭“li”元素?

 <div id="tree">
        <ul>
            <?php foreach($x1 as $p){  echo "<li class='folder'>$p</li>" ;?>
        <ul>
                <?php foreach($x2 as $q) { echo"<li>$q</li>"; } ?>
        </ul><?php } ?>

        </ul>
        </div>
</div>

答案 1 :(得分:0)

而不是使用数组推送你可以尝试这样

$x1    = array();
$query = $this->db->query("SELECT * FROM  jsrao_db4 ");
$rows  = $query->result();

foreach ($rows as $row) {
    array_push($x1, $row->cat_name);
    $query = $this->db->query("SELECT * FROM  jsrao_db5 WHERE id=$row->id");
    $rs = $query->result();
    foreach ($rs as $r) {
        $x1[$row->cat_name] = array();
        array_push($x1[$row->cat_name], $r->pro_name);
    }
}
?>
<div id="tree">
    <ul>
        <?php foreach ($x1 as $p) {
            echo "<li class='folder'>$p"; ?>
            <ul>
            <?php foreach ($x1[$p] as $q) {
                echo "<li>$q";
            } ?>
            </ul><?php } ?>
    </ul>
</div>
</div>
</div>

答案 2 :(得分:0)

而不是使用li你可以尝试这样谢谢

<div id="tree">
    <?php foreach ($x1 as $p) {
        echo "<li class='folder'>$p</li>"; ?>
        <ul>
            <li>
            <?php foreach ($x2 as $q) {
                echo "<li>$q</li>";
            } ?>
            </li>
        </ul>
    <?php } ?>
</div>

答案 3 :(得分:0)

使用这样的结构谢谢

<?php
$select_maincat = "SELECT * FROM wp_categories";
$qry_maincat = mysql_query($select_maincat);
while ($data_maincat = mysql_fetch_assoc($qry_maincat)) { ?>
    <ul>
        <li> <?php echo $data_maincat['cat_name']; ?>
            <ul>
            <?php
            $select_subcat = "select * from wp_subcategories where cat_id=" . $data_maincat['id'];
            $qry_subcat = mysql_query($select_subcat);
            while ($data_subcat = mysql_fetch_assoc($qry_subcat)) { ?>
                <li><?php echo $data_subcat['subcat_name']; ?></li>
            <?php } ?>
            </ul>
    </ul>
<?php } ?>