[PHP]Looping until the parent category does not have a parent category
Hi, I am making a blogging software, and I'm stuck.
I just finished making sub categories, and its working great...except for the category display (You know, like on these forums its RaGEZONE - MMOr... > Evolution : Ragezone > Coder's Paradise)
I have a category, Test, a subsection, Test 2, and a subsection of that subsection, Test 3.
When I view Test, I see Main Blog > Test.
When I view Test 2, I see Main Blog > Test > Test 2
However, when I view Test 3, I just see Main Blog > Test 2 > Test 3
Here's my code for the displaying:
PHP Code:
echo "<div id=\"content\">";
echo " <img src=\"style/blog/images/424px-Blue_folder_seth_yastrov_01_svg_mini.png\"> <a href=\"".$rootdir."index.php\">".$sitename."</a> ";
if($act == 'viewblogsection')
{
$quickid = mysql_real_escape_string(stripslashes($_GET['id']));
$quickscript = "SELECT * FROM `blog_section` WHERE id='".$quickid."'";
$quickquery = mysql_query($quickscript) or die(mysql_error());
$quickfetch = mysql_fetch_array($quickquery);
$parent = $quickfetch['parent'];
$quickscripta = "SELECT * FROM `blog_category` WHERE id='".$parent."'";
$quickquerya = mysql_query($quickscripta);
$quickfetcha = mysql_fetch_array($quickquerya);
$quickscriptb = "SELECT * FROM `blog_section` WHERE id='".$quickfetch['fparent']."'";
$quickqueryb = mysql_query($quickscriptb);
$quickscriptbcount = mysql_num_rows($quickqueryb);
$quickfetchb = mysql_fetch_array($quickqueryb);
echo " » <a href=\"".$rootdir."index.php?act=viewcat&id=".$quickfetcha['id']."\">".$quickfetcha['name']."</a> ";
if($quickscriptbcount != 0)
{
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$quickfetchb['id']."\">".$quickfetchb['name']."</a>";
}
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$quickid."\">".$quickfetch['name']."</a> ";
}
if($act == 'viewcat')
{
$quickid = mysql_real_escape_string(stripslashes($_GET['id']));
$quickscript = "SELECT * FROM `blog_cat` WHERE id='".$quickid."'";
$quickquery = mysql_query($quickscript) or die(mysql_error());
$quickfetch = mysql_fetch_array($quickquery);
echo " » <a href=\"".$rootdir."index.php?act=viewcat&id=".$quickfetch['id']."\">".$quickfetch['name']."</a><br> ";
}
if($act == 'blog')
{
$quickid = mysql_real_escape_string(stripslashes($_GET['id']));
$quickscript = "SELECT * FROM `blogs` WHERE id='".$quickid."'";
$quickquery = mysql_query($quickscript) or die(mysql_error());
$quickfetch = mysql_fetch_array($quickquery);
$blog = "SELECT * FROM `blog_section` WHERE id='".$quickfetch['cid']."'";
$blogquery = mysql_query($blog) or die(mysql_error());
$blogfetch = mysql_fetch_array($blogquery);
$parent = $blogfetch['parent'];
$quickscripta = "SELECT * FROM `blog_cat` WHERE id='".$parent."'";
$quickquerya = mysql_query($quickscripta);
$quickfetcha = mysql_fetch_array($quickquerya);
echo " » <a href=\"".$rootdir."index.php?act=viewcat&id=".$quickfetcha['id']."\">".$quickfetcha['name']."</a> ";
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$blogfetch['id']."\">".$blogfetch['name']."</a> ";
echo " » <a href=\"".$rootdir."index.php?act=blog&id=".$quickfetch['id']."\">".$quickfetch['title']."</a>";
}
echo "</div>";
So, for the blog_sub part, how can I make it keep doing that until the parent blog section's fparent is 0?
Re: [PHP]Looping until the parent category does not have a parent category
Originally I wrote a script for somethin like this but I forgot where I placed it, I'll dig it up for you in a couple minutes.
Re: [PHP]Looping until the parent category does not have a parent category
Quote:
how can I make it keep doing that until the parent blog section's fparent is 0?
PHP Code:
while($fpatent != 0)
{
//keep doing that
}
?
Re: [PHP]Looping until the parent category does not have a parent category
Quote:
Originally Posted by
s-p-n
PHP Code:
while($fpatent != 0)
{
//keep doing that
}
?
That wouldn't work, cuz he's notrunning a check on the parent. Just the current section.
This is all you needed to provide. O_O
PHP Code:
if($act == 'viewblogsection')
{
$quickid = mysql_real_escape_string(stripslashes($_GET['id']));
$quickscript = "SELECT * FROM `blog_section` WHERE id='".$quickid."'";
$quickquery = mysql_query($quickscript) or die(mysql_error());
$quickfetch = mysql_fetch_array($quickquery);
$parent = $quickfetch['parent'];
$quickscripta = "SELECT * FROM `blog_category` WHERE id='".$parent."'";
$quickquerya = mysql_query($quickscripta);
$quickfetcha = mysql_fetch_array($quickquerya);
$quickscriptb = "SELECT * FROM `blog_section` WHERE id='".$quickfetch['fparent']."'";
$quickqueryb = mysql_query($quickscriptb);
$quickscriptbcount = mysql_num_rows($quickqueryb);
$quickfetchb = mysql_fetch_array($quickqueryb);
echo " » <a href=\"".$rootdir."index.php?act=viewcat&id=".$quickfetcha['id']."\">".$quickfetcha['name']."</a> ";
if($quickscriptbcount != 0)
{
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$quickfetchb['id']."\">".$quickfetchb['name']."</a>";
}
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$quickid."\">".$quickfetch['name']."</a> ";
}
Re: [PHP]Looping until the parent category does not have a parent category
:wink:
Well obviously he would predefine it and run some sort of continuous check that eventually results in $fparent being 0.
For example,
PHP Code:
$fparent = 21;
while($fparent != 0)
$fparent--;
Re: [PHP]Looping until the parent category does not have a parent category
Quote:
Originally Posted by
s-p-n
:wink:
Well obviously he would predefine it and run some sort of continuous check that eventually results in $fparent being 0.
For example,
PHP Code:
$fparent = 21;
while($fparent != 0)
$fparent--;
But then the max number of sub-categories I can have is 21.
I need unlimited sub categories.
Re: [PHP]Looping until the parent category does not have a parent category
What's this thing called anyways?
Is it like the legend?
I mean the RaGeZone-...> Evolution >... thing.
Re: [PHP]Looping until the parent category does not have a parent category
Quote:
Originally Posted by
LuckySevenz
What's this thing called anyways?
Is it like the legend?
I mean the RaGeZone-...> Evolution >... thing.
Cool story bro.
Re: [PHP]Looping until the parent category does not have a parent category
Re: [PHP]Looping until the parent category does not have a parent category
Quote:
Originally Posted by
LuckySevenz
But then the max number of sub-categories I can have is 21.
I need unlimited sub categories.
Well that was just an example. Simply to give you an idea of where to go next. If you don't understand it, then I can understand how it didn't help ;)
You would change '21' to whatever the number would be (like mysql_num_rows($query). But really, I don't know what you want, so I can't really help you until you get into more detail and summarize your code into a quarter of the size you posted.
This snippet may help you with that,
PHP Code:
<?php
if($act == 'viewblogsection')
{
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query('SELECT * FROM blog_section
JOIN blog_categroy ON(blog_category.id = blog_section.parent)
JOIN blog_section ON(blog_section.id = blog_section.fparent)
WHERE blog_section.id = "'.$id.'"') or die(mysql_error());
$row = mysql_fetch_assoc($query);
die('<pre>'.print_r($row).'</pre>');
echo " » <a href=\"".$rootdir."index.php?act=viewcat&id=".$row['id']."\">".$row['name']."</a> ";
if(mysql_num_rows($query) != 0)
{
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$row['id']."\">".$row['name']."</a>";
}
echo " » <a href=\"".$rootdir."index.php?act=viewblogsection&id=".$id."\">".$row['name']."</a> ";
}
?>
I don't know where the problem is, I just took the first block of code for examination. I'm not going to sit here for an hour trying to figure out your code and the problem your having. If you can't narrow it down to just a few lines, then you need to do more investigating to find the real problematic lines, or shorten down your code. In the mean time, show me the die() message so I can get a better feel for your SQL database, thus inching closer to figuring out your problem..
You might try commenting out the die message just to see what happens without it. Though I doubt it'll work since I'm a bit clueless. :$:
Re: [PHP]Looping until the parent category does not have a parent category
This is what I'm using for poseidonBB:
Try making a column in the table called parent_list or something like that.
In that, put all of the ids of the parents.
For example, if Cat 1 is a regular forum, Cat 2 is a sub of that, and Cat 3 is a sub of that, in parent_list, for Cat 3's parent list, put 1, 2 (The ids of cat1 and cat2).
For Cat 2, just put 1, and for Cat 1, put -1 (there is no parent for Cat 1)
Now, in your 'viewblog' act or whatever, put $data = $quickfetch['parent_list'];
After that, put $parent_array = explode(',', $data);
Now, just do a foreach loop and echo out the link and name.