【问题标题】:display categories and sub categories readmore page显示类别和子类别阅读更多页面
【发布时间】:2012-09-18 14:50:02
【问题描述】:

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

    catid      catname       parentid

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

我在 php 嵌套的 'ul' 中显示这个数据表(代码)->

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

一切都很好 显示没有问题 .... 我的问题,如果单击主类别 get me 此类别中还没有主题。 ....我需要代码来选择用户是否在主类别中选择主类别回显子类别 - 如果选择子类别,则仅在子类别中显示数据,或者如果用户在此主要类别中选择主类别回显主题 示例 :: 我有类别 Photosession 和子类别 -> photosession april - photosession Septmber - photosession Jan - photosession Feb 当我选择主类别照片时,我需要在主类别中显示所有子类别或子类别中的所有主题

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

    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>

    ';
    }}}}}
    ?> 

我认为我的问题很简单,但我不知道该怎么做...我需要帮助.. !!

【问题讨论】:

  • 请总结您的问题。
  • @rationalboss 谢谢你现在看看我的网站afrogfx.com/index.php 向左看你会看到类别 Tahrir Nights 和 Photo Sessions 类别编号 2 有子类别 2012 年 4 月点击 + 你会找到它 点击照片如果单击主类别向我显示子类别,并且如果单击子类别上的 clcik 向我显示子类别中的所有帖子,我将在此处获得非帖子的会话
  • @rationalboss 你懂马的问题吗??

标签: php mysql show categories


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2012-10-14
    相关资源
    最近更新 更多