我是初学者(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)
?>
一切都很好显示没有问题....我的问题,如果点击主要类别让我这个类别还没有主题。 ....我需要代码选择用户是否在主类别中选择主要类别回显子类别 - 如果选择子类别只显示子类别中的数据或如果用户选择主要类别回声盟友主题在此主要类别中 示例::我有类别Photosession和子类别 - &gt; photosession 4月 - photosession Septmber - photosession Jan - photosession 2月 当我选择主类别photosession时,我需要显示所有子类别或主类别中子类别中的所有主题
注意 - &gt;我按catname选择主题
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>
';
}}}}}
?>
我认为我的问题很简单,但我无法理解这一点......我需要帮助请... !!
答案 0 :(得分: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>";