Forum Moderators: coopster

Message Too Old, No Replies

PHP Breadcrumb Help

         

field4000

6:14 am on Jul 15, 2005 (gmt 0)

10+ Year Member



Hey all,

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. :)

Blackie

8:32 am on Jul 15, 2005 (gmt 0)

10+ Year Member



You missed a comma after CatName
$query = "SELECT CatID, CatName, CatParent FROM categories WHERE CatParent ='$_GET[CatID]'";

And you dont need second mysql_query after you run the first one
$result = mysql_query($query) or die('<p>Error: ' . mysql_error().'.</p>');

$result = mysql_query($query);

maxi million

9:39 am on Jul 15, 2005 (gmt 0)

10+ Year Member



"<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!

field4000

10:05 am on Jul 15, 2005 (gmt 0)

10+ Year Member



Hey,

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.

grandpa

10:12 am on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



field4000, in addition to the previous posts you might want to consider another change to improve security. With this line you are allowing user input straight into your sql statement without first verifying it: $query = "SELECT CatID, CatName, CatParent FROM categories WHERE CatParent ='$_GET[CatID]'"; The variable $_GET[CatID] can be changed to any value by anyone.

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 displayed
I 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>";
}
}

field4000

10:23 am on Jul 15, 2005 (gmt 0)

10+ Year Member



Thanks grandpa,

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.

grandpa

10:40 am on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can add an else statement to your existing if statement.

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</a></p>";
}
}
else {
echo "<p>Something else</p>";
}

field4000

1:22 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



The code belew dosen't seem to work. I take it I have to somehow redeclare the variables pulled from the database?

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.

grandpa

8:05 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this part of the code where you want to echo something else if CatParent is NULL? If it is, it won't work because your query is not returning anything for NULL, or for any other value of $_GET[CatID] which does not match CatParent.

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