Forum Moderators: coopster
I am posted a message a little while back but I am yet to solve my issue.
I am a PHP newbie and I am having a great deal of trouble trying to code some dynamic breadcrumbs by pulling data from a database. Below is the code that I have written but it doesn't seem to work as I intend it to.
The script as it stands only displays the CatName if there is a CatParent. If there is no CatParent, nothing is displayed. The CatParent is never displayed.
The code:
$query = "SELECT CatID, CatName CatParent FROM categories WHERE CatParent ='$_GET[CatID]'";
$result = mysql_query($query) or die('<p>Error: ' . mysql_error().'.</p>');
$result = mysql_query($query);
if(mysql_num_rows($result))
{
while($row = mysql_fetch_row($result))
{
list($CatID, $CatName, $CatParent) = $row;
}
}
echo "<p><a href=\"index.php\">Home > $CatParent > $CatName</p>";
I think it requires some sort of array with an if/then statement? But I code be very wrong.
Any help would be greatly appreciated.
Cheers. :)
"<p><a href=\"index.php\">Home > $CatParent > $CatName</p>";
where do you want the </a>? if you want the breadcrumb to lead you on to the $CatName, then probably youd also like to have $CatParent within anchor tags.
what kind of urls are you looking at? by that i mean:
are you looking at index.php?CatID=1 ?
what happens when there is no $CatID available from the URL?
finally the code you have- whats wrong with it apart from the points that Blackie has mentioned?
may be you could tell us a little more about your problem. its a pity my earlier post obviously did not work for you, though elsewhere its working great!
Thanks for the help.
The comma being left out was just a typo.
The problem is that if there is no CatParent, i.e., CatParent = Null, then nothing is displayed. However, if there is a CatParent, then only the first Child category from the CatParent is displayed. For example: Home > 6 > Directories - where home is the hardcoded link, 6 = the CatParent and Directories is the first child category of 6.
I am pulling the data via a link the index page: <a href=\"http://localhost/site/test.php?CatID=6\">Search Engines</a>
I hope that it is clear.
Cheers for all your help.
You could create a whitelist to use which will only allow known CatID's to be selected. reference [webmasterworld.com]
Related info can be found here [webmasterworld.com]. The idea is to prevent someone from running a query that you did not anticipate and which could be harmful. Learning to verify input and applying safe practices now will certainly save you from potential attacks tomorrow.
However, if there is a CatParent, then only the first Child category from the CatParent is displayedI would have guessed that only the last Child category would be displayed. Will this work; moving the link so that it is echoed with each iteration of the while loop.
while($row = mysql_fetch_row($result))
{
list($CatID, $CatName, $CatParent) = $row;
echo "<p><a href=\"index.php\">Home > $CatParent > $CatName</a></p>";
}
}
By moving that line up inside the while command all the child categories are displayed.
One last issue to to nut out.
If there is not CatParent, i.e., CatParent = null, nothing is displayed.
Do I have to write an if/then statement to check if there is no CatParent, and if there isn't one then echo something else?
Cheers.
else($row = mysql_fetch_row($result))
{
list($CatID, $CatName, $CatParent) = $row;
echo "<p><a href=\"index.php\">Home</a> > CatParent</p>";
}
Once again, thank you very much for all your help!
Cheers.
else($row = mysql_fetch_row($result))
{
list($CatID, $CatName, $CatParent) = $row;
echo "<p><a href=\"index.php\">Home</a> > CatParent</p>";
}
You might this:
else
{
echo "<p><a href=\"index.php\">Home</a> > CatParent</p>";
}