MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2000, 01:57 AM
ranesh_w ranesh_w is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Colombo, Western, Sri Lanka
Posts: 41 ranesh_w User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
categories table info :
id: unique auto increment
parent_id: all master categories =0
sub categories = id of its master category
name : name of category


The script I've written is supposed to each master category in the table followed immediately by any sub-categories it may have. If a particular master category does not have any subcategories it should fetch the next master category.

My script works fine up to the point where the table contains a few subcategories before the next master category. At this point it lists out the subcategory (which has already been listed earlier within a master category) again assuming that it is a master category.

All I need if an if or while statement in my script. I've tried different variations, but nothing worked.

Here's the script :


<html>
<body>
<?php
mysql_connect(.....);
mysql_select_db(...);
$result = mysql("select * from categories where id!=0");
$num = mysql_numrows($result);
$i = 0;

while($i < $num) {

echo "<p>";

echo mysql_result($result,$i,"name");

echo "<p>";

$pid=mysql_result($result,$i,"id");

$subcategory=mysql_query("select name from categories where parent_id=$pid");

$num2=mysql_numrows($subcategory);

$j=0;

while ($j<$num2){

echo "<p>";

print (" ");

echo mysql_result($subcategory,$j,"name");

$j++;}

$i++;}

?>

</body>
</html>

This is what I have in my mind.

Immediately after the first 'while' there should be something like this :

$parent=mysql_result($result,$i,"parent_id");

if ($parent=0) :


and just before $i++:}


endif;


That's the idea. But it doesn't work. I get a blank screen.

Somebody please help me

Thanks a lot to anyone who will be able to help me out.

Reply With Quote
  #2  
Old September 6th, 2000, 09:40 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
You are selecting all categories in your first query, so sub-categories will be included. Then you select sub-categories again by the parent id. Naturally, they'll show up twice.

Modify your first query to exclude non-toplevel categories.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > condition within a loop??


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT