busnut Posted February 2, 2009 Share Posted February 2, 2009 G'day Got this bit of code that works to a degree, but needs fixing and I have absolutely no idea <? echo "<table border=1>"; $list3=mysql_query("select busno, depot, chassisbody from busfleet WHERE active='Y' ORDER BY depot ASC, chassisbody ASC, busno + 0 ASC "); // Show records by while loop. while($row_list3=mysql_fetch_assoc($list3)) { echo "<tr><td>"; echo $row_list3['depot']; echo "</td><td>"; { echo $row_list3['chassisbody']; echo "</td><td>"; { echo $row_list3['busno']; } } echo "</tr>"; } echo "</table>"; ?> What it does is searches the database for all buses that are active "Y", then is to dispaly the depot (once, not repeated for each record), then display the bus type (once, not repetated for each record), then display the list of bus numbers of that bus type in that depot... currently, what it is showing is this: depot - make of bus - bus depot - make of bus - bus depot - make of bus - bus and keeps repeating... where i'm trying to get it to do this: depot - make of bus - list of buses then next make of bus in that depot - list of buses and looping that way until the next depot then repeats from there! Any help appreciated. I'm actually somewhat stoked that I got this little bit of code to work so far without for once asking for help... Link to comment https://forums.phpfreaks.com/topic/143472-display-by-looping/ Share on other sites More sharing options...
trq Posted February 2, 2009 Share Posted February 2, 2009 Can you indent your code so it is legible? You appear to have alot of un-required braces. Link to comment https://forums.phpfreaks.com/topic/143472-display-by-looping/#findComment-752601 Share on other sites More sharing options...
busnut Posted February 2, 2009 Author Share Posted February 2, 2009 Can you indent your code so it is legible? You appear to have alot of un-required braces. like this? (if not, I apologise, not really sure what you mean...) <? echo "<table border=1>"; $list3=mysql_query("select busno, depot, chassisbody from busfleet WHERE active='Y' ORDER BY depot ASC, chassisbody ASC, busno + 0 ASC "); while($row_list3=mysql_fetch_assoc($list3)) { echo "<tr><td>"; echo $row_list3['depot']; echo "</td><td>"; { echo $row_list3['chassisbody']; echo "</td><td>"; { echo $row_list3['busno']; } } echo "</tr>"; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/143472-display-by-looping/#findComment-752604 Share on other sites More sharing options...
trq Posted February 2, 2009 Share Posted February 2, 2009 Why have you got all thoise extra braces in there? <?php echo "<table border='1'>"; $list3=mysql_query("select busno, depot, chassisbody from busfleet WHERE active='Y' ORDER BY depot ASC, chassisbody ASC, busno + 0 ASC "); while($row_list3=mysql_fetch_assoc($list3)) { echo "<tr><td>"; echo $row_list3['depot']; echo "</td><td>"; echo $row_list3['chassisbody']; echo "</td><td>"; echo $row_list3['busno']; echo "</tr>"; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/143472-display-by-looping/#findComment-752607 Share on other sites More sharing options...
busnut Posted February 2, 2009 Author Share Posted February 2, 2009 Why have you got all thoise extra braces in there? The braces, ok why they are in there was I thought if I braced each field type it would help do what i'm trying to achieve, however either way does the same result which isn't quite what i'm looking for. I've been looking up looping and arrays and am really unsure if i'm on the right path. Link to comment https://forums.phpfreaks.com/topic/143472-display-by-looping/#findComment-752609 Share on other sites More sharing options...
busnut Posted February 3, 2009 Author Share Posted February 3, 2009 Ok, i've gotten something to work courtesy of a friend, so not fully sure if there is a shorter way around this, but can't seem to get it into a table format without going pair shaped, and also can only get one form of total (and thats counting all displayed records), where I also want an individual count of all buses on each make in each depot and then the total of buses in each depot (hope that makes sense). This is the code that is somewhat working <? $allocation=mysql_query("select busno, depot, chassisbody from busfleet WHERE active='Y' ORDER BY depot ASC, chassisbody ASC, busno + 0 ASC "); $DepotNameToDisplay = "abc"; $ChassisToDisplay= "abc"; $totbuses=0; { while($row_allocation=mysql_fetch_assoc($allocation)) { $ThisDepotName = $row_allocation['depot']; if ($ThisDepotName != $DepotNameToDisplay){ $DepotNameToDisplay = $ThisDepotName; echo "<p><b>$DepotNameToDisplay</b>"; } $ThisChassisName = $row_allocation['chassisbody']; if ($ThisChassisName != $ChassisToDisplay){ $ChassisToDisplay= $ThisChassisName ; echo "<br><i>$ChassisToDisplay</i><br>"; } echo $row_allocation['busno']; echo " "; $totbuses=$totbuses+1; } } echo "<p>$totbuses buses allocated!"; echo "<hr>"; ?> And yes, there probably is a good chance i've got an extra brace in there that isn't required. So any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/143472-display-by-looping/#findComment-753308 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.