Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Navigation > Echo Parent and child
Old 06-23-2006, 07:14 PM PHP Navigation > Echo Parent and child
Junior Talker

Posts: 2
Trades: 0
Hi Webmaster-Talk.

What I am trying to is build a navigation menu which will echo all the categories from my mysql database with the specificic subcategoires below them.

My database contains: id, category, subcategory.

I.E

Category
-Subcategory

I can query the database so it echos all categories, and then echos all subcategories but the problem I have is each subcategory echos the same subcategory underneath it.

Cheers
scraptoft is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-23-2006, 11:15 PM Re: PHP Navigation > Echo Parent and child
Webmaster Talker

Posts: 626
Trades: 0
I have done something very similar to what you are asking... Could you please post your code and I'll compare it to mine.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 06-24-2006, 07:12 AM Re: PHP Navigation > Echo Parent and child
Junior Talker

Posts: 2
Trades: 0
I'll understand if you think my coding is a complete shambles, i'm really struggling on the mysql: ($title = the title passed in a url e.g http://domain.com/page.php?title=food

PHP Code:
include("dbc.php");
 
<?PHP
$query_sub 
"SELECT * FROM webstructure WHERE level_category = '$title' AND level = '3' ORDER BY name DESC LIMIT 10";
            
$result_sub mysql_query($query_sub
               or die(
"Invalid query: " mysql_error()); 
            while (
$row=mysql_fetch_array($result_sub)) {
            
$subcategory $row['name'];
        
?>

<?PHP

echo "";}

?>

<?PHP 
    
// Article rows
include("dbconnect.php");
   
   
$query_sub "SELECT * FROM webstructure WHERE level = '2' ORDER BY name ASC";
            
$result_sub mysql_query($query_sub
               or die(
"Invalid query: " mysql_error()); 
            while (
$row2=mysql_fetch_array($result_sub)) {
            
$category $row2['id'];

echo 
"<table border = '1'> <tr>";
echo 
"<td><br><b>$row2[2]</b> <br>";
if  (
$subcategory == $subcategory) echo "$subcategory </td>";
echo 
"</tr></table>";

?>

Last edited by scraptoft; 06-24-2006 at 07:14 AM..
scraptoft is offline
Reply With Quote
View Public Profile
 
Old 06-24-2006, 08:51 PM Re: PHP Navigation > Echo Parent and child
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I am confused as to what you are trying to do. Can you provide an example of the structure of your table `webstructure`?

I do see problems with your script sample above. After your first query, you are overwriting the var $subcategory for each returned row instead assining it to an array element.

Your second query is grabbing from the same table, why can't you combine the results from a single query? The category and subcategory needs some kind of relation so you can match each subcat with each cat.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-24-2006, 11:37 PM Re: PHP Navigation > Echo Parent and child
Webmaster Talker

Posts: 626
Trades: 0
Well... There are 4 things that I can see at first glance:

1. Your include() statement is outside of the php tags - you should put the include() inside the <?php ?> tags:
Code:
include("dbc.php");

2. Next, your while statement - $subcategory = $row['name'] is going to rewrite itself each time through the loop. You should try putting it into an array for example - $arr_subcategory[] = $row['name'] - then loop through the array to display, or just echo the $subcategory right in the while loop:
Code:
$query_sub = "SELECT * FROM webstructure WHERE level_category = '$title' AND level = '3' ORDER BY name DESC LIMIT 10";
            $result_sub = mysql_query($query_sub) 
               or die("Invalid query: " . mysql_error()); 
            while ($row=mysql_fetch_array($result_sub)) {
            $subcategory = $row['name'];
        
?>
3. The next while statement - again $category is going to be overwritten each time through the loop, you should assign it to $arr_category[] to create an array:
Code:
   $query_sub = "SELECT * FROM webstructure WHERE level = '2' ORDER BY name ASC";
            $result_sub = mysql_query($query_sub) 
               or die("Invalid query: " . mysql_error()); 
            while ($row2=mysql_fetch_array($result_sub)) {
            $category = $row2['id'];
4. Finally, I see a problem with the if statement - $subcategory will ALWAYS be equal to $subcategory as you are comparing it to itself.. For example, you are saying if 1 = 1 then echo 1. You need to compare it to something that is a different variable.
Code:
if  ($subcategory == $subcategory) echo "$subcategory </td>";
If I am off course here, just let me know and we'll see what we can do.

Last edited by jim.thornton; 06-24-2006 at 11:47 PM..
jim.thornton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Navigation > Echo Parent and child
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15557 seconds with 12 queries