0

I'm beginner ( PHP & MySQL ) , I Have Problem With my Code In my website I created unlimited categories and subcategories - Table - >

    catid      catname       parentid

    1       |   animals   |     0
    2       |   vegs      |     0
    3       |   dog       |     1
    4       |   cat       |     1
    5       |   carrot    |     2

I display this datatable in php nested 'ul' like (Code)->

    <?php 

    mysql_select_db($db_name, $conn); // Change for your database
    $query_Recordset1 = "SELECT * FROM categories";
    $Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error()); // Change for your database

    //get all rows

    while ( $row = mysql_fetch_assoc($Recordset1) )
    {
$menu_array[$row['catid']] = array('catname' => $row['catname'],'parentid' =>         $row['parentid']);


    }
    //recursive function that prints categories as a nested html unordered list

    function generate_menu($parent)

    {

    $has_childs = false;

    //this prevents printing 'ul' if we don't have subcategories for this category



    global $menu_array;

    //use global array variable instead of a local variable to lower stack memory requierment



    foreach($menu_array as $key => $value)

    {

            if ($value['parentid'] == $parent) 

            {       

                    //if this is the first child print '<ul>'                       

                    if ($has_childs === false)

                    {

                            //don't print '<ul>' multiple times                             

                            $has_childs = true;

                             //echo '<ul>';
    echo '<ul id="categories">';



                    }

                     echo '<li><a href="category.php?catname=' . $value['catname'] . '/">' . $value['catname'] . '</a>';
    echo '<input type="hidden" value="' . $value['catname'] . '" />';
                    generate_menu($key);

                    //call function again to generate nested list for subcategories belonging to this category

                    echo '</li>';

            }

    }

    if ($has_childs === true) echo '</ul>';

    }

    //generate menu starting with parent categories (that have a 0 parent)
    ?>

Everything is good Showing No Problem .... My problem if click On main category get me There are no topics in this category yet. .... i need Code To select if User chose main category echo subcategory in main category -- If chose Subcategory just show data in subcategory Or If User chose main category echo ally topic in this main categories Example :: I have Category Photosession And sub category -> photosession april - photosession Septmber - photosession Jan - photosession Feb When i select the Main category photosession I need to show all sub category Or all topic in sub category in main category

  • Notice -> i select topic by catname

    echo '<li><a href="category.php?catname=' . $value['catname'] . '">' . $value['catname'] . '</a>';
    

This My Code to select Posts

    <?php
    $categories=$_GET['catname'];
    $sql = "SELECT * FROM categories WHERE catname='$categories' ";
    $result = mysql_query($sql);
    if(!$result){
    echo '<h1>The category could not be displayed, please try again later.</h1>' .         mysql_error();
    }else{
    if(mysql_num_rows($result) == 0){
    echo '<h1>This category does not exist.</h1>';
    }else{  
    while($row = mysql_fetch_assoc($result))
    {
    echo '<title>' . $row['catname'] . ' | E3rafly.com</title>';
    }
    ?>

    <?php
    $categories=$_GET['catname'];
    $sql = "SELECT  * FROM posts  WHERE categories='$categories' " or die         (mysql_error()); 
    $result = mysql_query($sql);
    if(!$result){
    echo '<h1>The topics could not be displayed, please try again later.</h1>';
    }else{
    if(mysql_num_rows($result) == 0)
    {
    echo '<h1>There are no topics in this category yet.</h1>';
    }else{  
    ?>

    <?php
    while($row = mysql_fetch_assoc($result)){   
    echo '
    <div class="span3 block">
    <div class="view view-first">
    <div class="tringle"></div>

    <a href="readmore.php?postid=' . $row['postid'] . '"><img src="admin/' . $row['thumbpath'] . '" title="' . $row['title'] . '" /></a>
    <div class="mask">
    <a href="admin/' . $row['imagepath'] . '" rel="prettyPhoto" class="info"></a>

    <a href="readmore.php?postid=' . $row['postid'] . '" class="link"></a>


    </div>
    </div>
     <div class="descr">
    <h4><a href="readmore.php?postid=' . $row['postid'] . '">',substr($row['title'],0,150),' ..</a></h4>
    <p>',substr($row['description'],0,200),'.</p>


     <div class="meta">
     <hr>

    <span class="meta_comment"><i class="icon-comment"></i> <strong>Comments:</strong><ahref="blog_single_i.html">11</a></span>
    <span class="meta_date"><i class="icon-calendar"></i>
    <strong>Date:</strong> <a href="readmore.php?postid=' . $row['postid'] . '">' .         $row['create_on'] . '</a></span>

    <div class="clearfix"></div>
    </div>
    </div>
    </div>

    ';
    }}}}}
    ?> 

I think My problem easy but i can't get idea to do it ... I need help Please .. !!

4

1 回答 1

0

这是显示类别和子类别的最佳答案

$con=mysqli_connect("localhost","root","","test");
$sql="select * from category where parent=0";
$result=mysqli_query($con,$sql);
echo "<ol>";
while($row=mysqli_fetch_array($result))
{
    echo "<li >".$row['name']."</li>";
    abc($row['id']);
}

function abc($id)
{
    global $con;
    $sql="select * from category where parent=$id";
    $result=mysqli_query($con,$sql);
    echo "<ol>";
    while($row=mysqli_fetch_array($result))
    {
        echo "<li>".$row['name']."</li>";
        abc($row['id']);
    }
    echo "</ol>";
}
echo "</ol>";
于 2014-07-12T18:51:32.970 回答