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...

Recommended Answers

All 4 Replies

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.

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

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

can you somehow show me the odbc version of this code???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.