繁体   English   中英

显示类别和子类别Readmore页面

[英]display categories and sub categories readmore page

我是新手(PHP和MySQL),我的代码有问题在我的网站中,我创建了无限的类别和子类别-表->

    catid      catname       parentid

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

我在php嵌套'ul'中显示此数据表,如(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)
    ?>

一切都很好,没有问题....如果单击“主类别”,我的问题会告诉我该类别中没有主题。 ....我需要代码以选择用户是否在主类别中选择了主类别回声子类别-如果选择了子类别,则仅在子类别中显示数据,或者如果用户在此主类别中选择了主类别回声盟友主题,例如:我有类别Photosession和子类别-> photosession四月-photosession 9月-photosession一月-photosession二月当我选择主类别photosession时,我需要显示所有子类别或主类别中子类别的所有主题

  • 注意->我按猫名选择主题

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

我的代码选择帖子

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

    ';
    }}}}}
    ?> 

我认为我的问题很容易,但是我不知道该怎么做...我需要帮助。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM