I have two tables like this.
books
=====
b_id a_id book_name
1 1 ABC
2 1 DEF
3 2 GHI
authors
=======
a_id author_name
1 A1
2 A2
I need a table like this.
S.No BookName
-----------------
A1
==
1 ABC
2 DEF
A2
==
1 GHI
What i'm planning to do is 1. Do a while loop and get the author name first and print it 2. Use the author id inside the first loop and do another one iteration to get the list of book list for each author.
A sample code is like this:
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>S.No</th>
<th>Book Name</th>
</tr>
</thead>
<?php
$mysql_query1 = "my sql query to get the author name list first";
$results = mysql_query ( $mysql_query1 ) or die ( mysql_error () );
while($row = mysql_fetch_array($results)){
?>
<tbody>
<tr>
<td style="background:none;"><u><?php echo $row['author_name']; ?></u></td>
</tr>
<?php
$result = mysql_query ( "mysql query to get the list of books for each author with a where condition like this where a_id=$row['a_id']" ) or die ( mysql_error () );
$book_count = mysql_num_rows($result);
while($row1 = mysql_fetch_array($result)){
?>
<tr>
<?php for($i=1;$i<=$book_count;$i++){ ?>
<td><?php echo $i; ?> </td>
<td><?php echo $row1['book_name']; ?> </td>
<?php } ?>
</tr>
<?php
}
?>
</tbody>
<?php } ?>
</table>
My friends insisted me that the above method is a old one, and there is something to do with just few line codes. Is it?
If yes, could someone redefine my code and give me.
Thanks, Kimz