0

i'm realy confused on how to do this.. deperately need help.
i have two tables as below:

table1- qid(primarykey), qtitle
table2- aid(primarykey), atitle, qid

example,

table1- (qid=1; qtitle=fruits), (qid=2; qtitle=vehicle)

table2- (aid=1; atitle=apple; qid=1), (aid=2; atitle=banana; qid=1), (aid=3; atitle=car; qid=2), (aid=4; atitle=bike; qid=2), (aid=5; atitle=bus; qid=2);


when i retrieve the data from database, it should display like below:

Fruits
apple
banana

Vehicle
car
bike
bus

can anyone help me with this please.. thanks in advance...

Edited by wayz1229: n/a

4
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by kitschkath
0

A simple query would easily accomplish that, or am I not following what you are looking to do?

$fruits=mysql_query("SELECT atitle FROM table2 WHERE qid='1' ");
while($row=mysql_fetch_array($fruits)){
$fruit_type=$row['atitle'];

echo ''.$fruit_type.'' ; }

That would output a list of fruits from your table. You can do the same for vehicles.

0

The below code will show the results just like you wanted:

<?php
$query1 = "SELECT * FROM table1";
$result1 = mysql_query($query1);
while ($row1 = mysql_fetch_array($result1)) {
$qid = $row1['qid'];
$qtitle = $row1['qtitle'];
$query2 = "SELECT * FROM table2 WHERE qid='$qid'";
$result2 = mysql_query($query2);
echo "<span style=\"text-decoration:underline; font-weight:bold;\">".$qtitle."</span><br />";
while ($row2 = mysql_fetch_array($result2)) {
echo "".$atitle."<br />";
}
echo "<br />";
}
?>

~G

Edited by Graphix: n/a

0

thanks alot Mr. Graphix.. The code works perfectly.. thank you so much..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.