busnut Posted February 26, 2009 Share Posted February 26, 2009 The script below is something that i've been trying to work out for the past week or so how to display how many buses of each make of bus in its depot and how many buses for the whole depot. The best I can come up with is the total of all buses in all depots, and only the total of buses will display for the last depot Here is the script: $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>"; $totdepot=0; } $ThisChassisName = $row_allocation['chassisbody']; if ($ThisChassisName != $ChassisToDisplay){ $ChassisToDisplay= $ThisChassisName ; echo "<br><i>$ChassisToDisplay</i><br>"; } echo $row_allocation['busno']; echo " "; $totbuses=$totbuses+1; $totdepot=$totdepot+1; } { echo "<p>total buses for $DepotNameToDisplay: $totdepot"; echo "<hr>"; } } echo "<p>$totbuses buses allocated!"; What its suppose to look like is this: depotname make of bus with total of buses of this make in this depot list of buses total of buses in depot then loops back to make of bus until no more different make of buses available, then loops to the next depot and keeps going and once all depots/makes/buses are displayed, then displays total and then displays total amt of all buses And lastly, its suppose to display the <hr> after each depot, which once again will only show once on the very last depot One option I tried kept calculating something that was giving some ridiculous figure, so would appreciate some coding assistance please! Quote Link to comment https://forums.phpfreaks.com/topic/146966-display-totals-of-each-type/ Share on other sites More sharing options...
sasa Posted February 26, 2009 Share Posted February 26, 2009 try <?php $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; $not_start = false; while($row_allocation=mysql_fetch_assoc($allocation)) { $ThisDepotName = $row_allocation['depot']; if ($ThisDepotName != $DepotNameToDisplay){ if ($not_start){ echo "<p>total buses for $DepotNameToDisplay: $totdepot"; echo "<hr>"; } $DepotNameToDisplay = $ThisDepotName; echo "<p><b>$DepotNameToDisplay</b>"; $totdepot=0; $not_start = true; } $ThisChassisName = $row_allocation['chassisbody']; if ($ThisChassisName != $ChassisToDisplay){ $ChassisToDisplay= $ThisChassisName ; echo "<br><i>$ChassisToDisplay</i><br>"; } echo $row_allocation['busno']; echo " "; $totbuses=$totbuses+1; $totdepot=$totdepot+1; } echo "<p>total buses for $DepotNameToDisplay: $totdepot"; echo "<hr>"; echo "<p>$totbuses buses allocated!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/146966-display-totals-of-each-type/#findComment-771602 Share on other sites More sharing options...
busnut Posted February 26, 2009 Author Share Posted February 26, 2009 Hey, that worked out brilliantly thanks 'sasa', most appreciated. Except can I stretch the friendship abit more, to somehow get a total of how many buses of each bus make also? ie (part in bold): depot bus make (total of buses listed of this make) buses buses buses buses bus make (total of buses listed of this make) buses buses buses buses (total) I tried to add a $totmake in there to display how many buses of each bus make ($chassistodisplay) there is, but failed All the responses I got after trying in different areas using a similar format to what had been done gave me '0' counts or some ridiculous high number. And once again, thanks for what you've done so far, certainly something I hadn't even thought of. Quote Link to comment https://forums.phpfreaks.com/topic/146966-display-totals-of-each-type/#findComment-771647 Share on other sites More sharing options...
busnut Posted March 17, 2009 Author Share Posted March 17, 2009 Sorry to shunt this topic back to the top, but im still having difficulty in getting the total counts of each make of bus in each depot The best I got was an unrealistic figure and that was because it kept adding rather each set of totals. I'm still working on the last code posted by 'sasa' as the total depot count works perfectly, just need the individual bus make count to work perfectly (please). Help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/146966-display-totals-of-each-type/#findComment-786414 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.