Click to See Complete Forum and Search --> : sql looping problem


raj_2006
08-04-2006, 08:05 AM
Hi All,

I am making a dynamic menu using php/mysql.I want to display the below output

Country[label]
US
.....Ohio
Spain
.....Barcelona

If i click US then it will show ohio or if i click Spain then it will show Barcelona...this is the objective.Now i have written the code in this way

<tr>
<td>Country&gt;&gt;</td>
</tr>
<?
$chk="select * from juri where parent_id='0'";
$q_chk=mysql_query($chk) or die(mysql_error());
$tot=mysql_num_rows($q_chk);
?>
<tr>
<td><a href="index.php?type=cat">Add Country</a></td>
</tr>
<?
while($row1=mysql_fetch_array($q_chk))
{
$name=$row1['name'];
$master_id=$row1['master_id'];
$parent_id=$row1['parent_id'];
?>
<tr>
<td> <a href="index.php?show=subcat&master_id=<?=$master_id;?>">
<?=$name;?>
</a> </td>
</tr>
<?
if($_GET['show']=="subcat")
{
$master_id=$_GET['master_id'];
$mas_sql="select * from juri where parent_id='$master_id'";
$mas_q=mysql_query($mas_sql) or die(mysql_error());
while($row2=mysql_fetch_array($mas_q))
{
?>
<tr>
<td>.....
<?=$row2['name'];?>
</td>
</tr>
<?
} }}?>

Now after running the above code i am getting this output


Country[label]
US
.....Ohio
Spain
.....Ohio

I think it might be a problem of 2nd loop or somethng else.....really i dont have the idea.

Please suggest me a idea so that i can display the desired output

thanks for your suggestion in advance.........Raj

timmah
08-08-2006, 12:00 AM
try

<tr>
<td>Country&gt;&gt;</td>
</tr>
<?
$chk="select * from juri where parent_id='0'";
$q_chk=mysql_query($chk) or die(mysql_error());
$tot=mysql_num_rows($q_chk);
?>
<tr>
<td><a href="index.php?type=cat">Add Country</a></td>
</tr>
<?
while($row1=mysql_fetch_array($q_chk))
{
$name=$row1['name'];
$master_id=$row1['master_id'];
$parent_id=$row1['parent_id'];
?>
<tr>
<td> <a href="index.php?show=subcat&master_id=<?=$master_id;?>">
<?=$name;?>
</a> </td>
</tr>
<?

///NEW STUFF
if($_GET['show']=="subcat" && $master_id==$_GET['master_id'])
{

$mas_sql="select * from juri where parent_id='" .$_GET['master_id'] ."'";

/////
$mas_q=mysql_query($mas_sql) or die(mysql_error());
while($row2=mysql_fetch_array($mas_q))
{
?>
<tr>
<td>.....
<?=$row2['name'];?>
</td>
</tr>
<?
} }}?>

raj_2006
08-08-2006, 01:18 PM
Hi

Thanks for your suggestion.yeah its working but here is another problem that is

US
.....Ohio
Spain
.....Barcelona

Ohio is sub of US......now if i click Ohio then it should show if there is any sub sub category under ohio but clicking the ohio link it is going to the main page.

It should display like this

US
.....Ohio
...........X

......and this go on.

I think recursive function will be best for this type of display.But thinking how to write it.

Please suggest me.....Thanks in advance........Raj