maliary Posted July 26, 2007 Share Posted July 26, 2007 Hi, I want to make a display from a table in the following format GroupName1 fruit1 fruit2 fruit3 GroupResults1 GroupName2 fruit1 fruit2 fruit3 GroupResults2 This is my code:below: Where can I tweak it? $sql ="SELECT DISTINCT $dbtable.serial_value,$dbtable.type,$$dbtable.group_id,$dbtable.groupresults,table.name,$table.nr,$table.normals,$table.msr_unit FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE $dbtable.job_id = '$batch_nr' ORDER BY group_id DESC"; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ if ($rows['group_id'] != $lastgroup) { // Print Group Name echo "<tr> <td colspan=3 align=left><strong>Group: ".$rows['group_id']."</strong></td> </tr>\n"; } echo "<tr> <td width=50> </td> <td width=200 height=25 align=center>".$rows['name']."</td> $lastgroup = $rows['group_id']; } Quote Link to comment Share on other sites More sharing options...
maliary Posted July 26, 2007 Author Share Posted July 26, 2007 The sql select query should not really matter. It could be very simple e.g. SELECT * FROM $dbtable Quote Link to comment Share on other sites More sharing options...
maliary Posted July 26, 2007 Author Share Posted July 26, 2007 From this code i get the following output: $sql ="SELECT DISTINCT $dbtable.serial_value,$dbtable.type,$$dbtable.group_id,$dbtable.groupresults,table.name,$table.nr,$table.normals,$table.msr_unit FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE $dbtable.job_id = '$batch_nr' ORDER BY group_id DESC"; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ if ($rows['group_id'] != $lastgroup) { // Print Group Name echo "<tr> <td colspan=3 align=left><strong>Group: ".$rows['group_id']."</strong></td> </tr>\n"; } echo "<tr> <td width=50> </td> <td width=200 height=25 align=center>".$rows['name']."</td><tr>"; echo"<tr><td width=50>".$rows['groupresults']."</td></tr>"; $lastgroup = $rows['group_id']; } groupname fruitname groupresults fruitname2 groupresults while it should be like: groupname fruitname fruitname2 groupresults Quote Link to comment Share on other sites More sharing options...
maliary Posted July 26, 2007 Author Share Posted July 26, 2007 At the end of each group loop it need to display group results. How do can I stop it after a given loop? Quote Link to comment Share on other sites More sharing options...
maliary Posted July 26, 2007 Author Share Posted July 26, 2007 Am sorry I've been very vague on this. This is what I would like: What i mean is this, The code currently displays database out put in this format without the part I have commented out GroupName : Flower rose petal GroupName : Animal goat cow I would like to display a field group result at the end of each group. Thus: GroupName : Flower rose petal Results GroupName : Animal goat cow Results How can I do this? Its just a display thing. $sql ="SELECT DISTINCT $dbtable.serial_value,$dbtable.type,$dbtable.results,$dbtable.group_id,$dbtable.groupresults,table.n ame,$table.nr,$t able.normals,$table.msr_unit FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE $dbtable.job_id = '$batch_nr' ORDER BY group_id DESC"; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ if ($rows['group_id'] != $lastgroup) { // Print Group Name echo "<tr> <td colspan=3 align=left><strong>Group: ".$rows['group_id']."</strong></td> </tr>\n"; } echo "<tr> <td width=50> </td> <td width=200 height=25 align=center>".$rows['name']."</td><tr>"; # the results which should be at the bottom of every group //echo "<tr> //<td width=50> </td> //<td width=200 height=25 //align=center>".$rows['results']."</td><tr>"; $lastgroup = $rows['group_id']; } [code=php:0] Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 26, 2007 Share Posted July 26, 2007 <?php $sql ="SELECT DISTINCT $dbtable.serial_value,$dbtable.type,$dbtable.results,$dbtable.group_id,$dbtable.groupresults,table.n ame,$table.nr,$t able.normals,$table.msr_unit FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE $dbtable.job_id = '$batch_nr' ORDER BY group_id DESC"; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ //New Group Name if ($rows['group_id'] != $lastgroup) { //Display last subtotal if not first group if ($lastgroup) { echo "<tr> <td width=\"50\"> </td> <td width=\"200\" height=\"25\" align=\"center\">$subtotal</td> </tr>"; } //Reset the subtotal $subtotal = 0; //Print the group name echo "<tr> <td colspan=2 align=left><strong>Group: ".$rows['group_id']."</strong></td> </tr>\n"; } //Display the record echo "<tr> <td width=50> </td> <td width=200 height=25 align=center>".$rows['name']."</td> <tr>"; //Add to subtotal $subtotal++; //Set the lastgroup value $lastgroup = $rows['group_id']; } //Show the last subtotal row echo "<tr> <td width=\"50\"> </td> <td width=\"200\" height=\"25\" align=\"center\">$subtotal</td> </tr>"; ?> Quote Link to comment Share on other sites More sharing options...
maliary Posted July 27, 2007 Author Share Posted July 27, 2007 Thanks a lot, I really appreciate your help. The code doesn't display the results part. It only displays for one result at the bottom of the wrong group. I've added an extra line of code next to $subtotal <?php $sql ="SELECT DISTINCT $dbtable.serial_value,$dbtable.type,$dbtable.results,$dbtable.group_id,$dbtable.groupresults,table.n ame,$table.nr,$t able.normals,$table.msr_unit FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE $dbtable.job_id = '$batch_nr' ORDER BY group_id DESC"; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ //New Group Name if ($rows['group_id'] != $lastgroup) { //Display last subtotal if not first group if ($lastgroup) { echo "<tr> <td width=\"50\"> </td> <td width=\"200\" height=\"25\" align=\"center\">$subtotal</td> <td width=\"200\" height=\"25\" align=\"center\">Results: $rows['notes']</td> // New line of code here </tr>"; } //Reset the subtotal $subtotal = 0; //Print the group name echo "<tr> <td colspan=2 align=left><strong>Group: ".$rows['group_id']."</strong></td> </tr>\n"; } //Display the record echo "<tr> <td width=50> </td> <td width=200 height=25 align=center>".$rows['name']."</td> <tr>"; //Add to subtotal $subtotal++; //Set the lastgroup value $lastgroup = $rows['group_id']; } //Show the last subtotal row echo "<tr> <td width=\"50\"> </td> <td width=\"200\" height=\"25\" align=\"center\">$subtotal</td> <td width=\"200\" height=\"25\" align=\"center\">Results: $rows['notes']</td> // New line of code here </tr>"; ?> Quote Link to comment Share on other sites More sharing options...
maliary Posted July 27, 2007 Author Share Posted July 27, 2007 Am sure its a slight correction on the if....I think Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2007 Share Posted July 27, 2007 Ok, you need to be specific by what you mean by "results". I assumed you menat the number of records, but now you are displaying $rows['notes']. By my logic that would only show the notes for the last record in a group. So what, exactly, do you want the results entry to be? Quote Link to comment Share on other sites More sharing options...
maliary Posted July 28, 2007 Author Share Posted July 28, 2007 It only displays the $rows['notes'] once under the wrong group. For the other groups that get displayed no $rows['notes'] are displayed. Let me attach a dump of the two tables. This is how it prints out Group: Serology CD4/CD8 9 VIRAL LOAD DNA 1 Notes larvae seen in alchohol content 17 Group: Endocrine/Tumor_Marker THYROID ANTIBODIES 2 Notes 15 [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
maliary Posted July 28, 2007 Author Share Posted July 28, 2007 The php script [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 It only displays the $rows['notes'] once under the wrong group. For the other groups that get displayed no $rows['notes'] are displayed. Let me attach a dump of the two tables. This is how it prints out Group: Serology CD4/CD8 9 VIRAL LOAD DNA 1 Notes larvae seen in alchohol content 17 Group: Endocrine/Tumor_Marker THYROID ANTIBODIES 2 Notes 15 How should it print out? Quote Link to comment Share on other sites More sharing options...
maliary Posted July 29, 2007 Author Share Posted July 29, 2007 It should be this way, Group: Serology CD4/CD8 9 VIRAL LOAD DNA 1 Notes tests seen in serology 17 Group: Endocrine/Tumor_Marker THYROID ANTIBODIES 2 Notes larvae seen in alchohol content 15 The correct $rows['notes'] output as per the group. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 The notes you print are from the new group record you have just read, but you output them at the end of the previous group. Store the notes field in, say, $savenotes, and output that when you start a new group. After you exit the loop, you then have to output the saved notes and total for the final group. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 29, 2007 Share Posted July 29, 2007 Here's revised code that should work for your needs: <?php $sql ="SELECT DISTINCT $dbtable.serial_value,$dbtable.type,$dbtable.results,$dbtable.group_id,$dbtable.groupresults,table.n ame,$table.nr,$t able.normals,$table.msr_unit FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE $dbtable.job_id = '$batch_nr' ORDER BY group_id DESC"; $res = mysql_query($sql) or die(mysql_error()); while ($rows = mysql_fetch_assoc($res)){ //New Group Name if ($rows['group_id'] != $lastgroup) { //Display last notes value if exists if ($lastnotes) { echo "<tr> <td width=\"50\"> </td> <td width=\"200\" height=\"25\" align=\"center\">$subtotal</td> <td width=\"200\" height=\"25\" align=\"center\">Results: $lastnotes</td> // New line of code here </tr>"; } //Set the lastnotes value $lastnotes = $rows['notes']; //Print the group name echo "<tr> <td colspan=2 align=left><strong>Group: ".$rows['group_id']."</strong></td> </tr>\n"; } //Set the lastgroup value $lastgroup = $rows['group_id']; //Display the record echo "<tr> <td width=50> </td> <td width=200 height=25 align=center>".$rows['name']."</td> <tr>"; } //Show the last notes value echo "<tr> <td width=\"50\"> </td> <td width=\"200\" height=\"25\" align=\"center\">$subtotal</td> <td width=\"200\" height=\"25\" align=\"center\">Results: $lastnotes</td> // New line of code here </tr>"; ?> Quote Link to comment Share on other sites More sharing options...
maliary Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks Guys! I appreciate your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.