sonal Posted November 29, 2011 Share Posted November 29, 2011 I have a query which retrieves member's name and books he/she read. The query returns name of the member, book title and book author. I want to print member name only once, and then a table with books names and authors in it. if(mysqli_num_rows($r) > 0 ) { //Here somewhere I need to print member name. But query result is not yet found. echo '<table align="center" cellspacing="5" cellpadding="5" width="75%"> <tr> <td align="left"><b>'. $t1 .'</a></b></td> <td align="left"><b>' . $a1 .'</a></b></td> </tr>'; while($row = mysqli_fetch_row($r)) { // Is there anything which can hold $membername = $row[1]...But how to print it above the table? //How can I print the name only once? echo '<tr> <td align="left">' . $row[6] . '</td> //book title <td align="left">' . $row[7] . '</td> //author } Link to comment Share on other sites More sharing options...
sonal Posted November 30, 2011 Author Share Posted November 30, 2011 If I do as following: after running query, $row = mysql_fetch_row($r) echo $row[1] //index for name. and then loop things... The loop misses out first record.. Link to comment Share on other sites More sharing options...
Larry Posted November 30, 2011 Share Posted November 30, 2011 What you want to do is create a flag variable. Assign it a false value before the loop. Then, within the loop, see if the value you want to print equals the value of that flag variable. If it doesn't, then it's a new member and you should print it, then assign that value to the flag variable. That will do it. Link to comment Share on other sites More sharing options...
sonal Posted November 30, 2011 Author Share Posted November 30, 2011 Thank you very much Larry. I am trying to do as you said... Link to comment Share on other sites More sharing options...
Recommended Posts