Jump to content

Select 1 row from 1 table and ALL rows from a second table , 2 while loops


Recommended Posts

hey all

i stuck with this, i spent 3 hours of internet search but didnt find solution.

i want to make a navigation like

nokia

  • nokia N95
  • nokia E71

samsung

  • samsung D500
  • D900

Sony Erricson

  • w110
  • p10

i made 2 tables , phone, model

phone > id, name

model > id, name, phoneid

 

i tried this query in php

select phone.id, phone.name, model.id, model.name, model.phoneid from phone, model where model.phoneid= phone.id

 

it shows

nokia

nokia N95

nokia

nokia E71

nokia

some model

nokia

some model

 

samsung

samsung D500

 

samsung

D900

and soo on

  if i use

$query= "select * from phone";
$result = mysql_query($query);
while ($row= mysql_fetch_array($result))
{
$id =$row['id'];
$phone= $row['name'];
echo "<h1>$phone</h1>";
echo "<ul>";
//[b]2nd while loop[/b]
$query2= "select * from model where phoneid='$id'";
$result2 = mysql_query($query2);
while ($row= mysql_fetch_array($result2))
{

$model= $row['name'];
echo "<li>$phone</li>";
}
echo "</ul>";

 

this code just fetch one result

nokia

    nokia n95

 

please help me , i shall be greatful.

Hi

 

You could do it as 2 seperate pieces of SQL, looping through one and for each entry performing the other and looping through what it returns.

 

However, a join would be most sensible, and just keep track of when the phoneid changes.

 

Something like:-

 

SELECT phone.id, phone.name, model.id, model.name FROM phone JOIN model ON phone.id = model.phoneid ORDER BY phone.id, model.id

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.