SkyRanger Posted October 29, 2008 Share Posted October 29, 2008 I am having a small problem I am trying to pull info from my database when something changes. Example 1: $busstatus = $bstat; if ($busstatus = "running) echo "All buses are running"; else echo "The following buses are delayed or canceled"; In the database I would have: bus 1 - running bus 2 - running bus 3 - running bus 4 - running bus 5 - running So if all the buses are running it would say "All buses running" but if one of them got changed: bus 1 - running bus 2 - delayed bus 3 - running bus 4 - running bus 5 - canceled I am trying to get the else statement to say: The following buses are delayed or canceled Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/ Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 well, you could set $bstat = 1 if all buses are running, otherwise to an array of canceled/delayed buses. then your code would be: if ($busstatus == 1) { //or "running" i guess it doesn't matter echo "all busses running"; } else{ echo "The following buses are delayed or canceled: <br />"; foreach($busstatus as $bus){ echo $bus, "<br />"; } } is this what you meant? Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-677688 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 Can you post the part where you pull the data from the database? You would have to loop through the query results and find out if all the buses are running and set a flag if 1 or more aren't. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-677700 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 $sql = "SELECT * FROM ".$prefix."_bus_monitor"; $result = $db->sql_query($sql); while ( $row = $db->sql_fetchrow($result) ) { $busstatus = $row['brun']; } So what I am trying to do is get it to say if no buses are delayed then post: All buses running normally. But if there are a buses delayed or canceled like in the example above: bus 2 - delayed bus 5 - canceled Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678194 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 ~bump~ Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678200 Share on other sites More sharing options...
samshel Posted October 30, 2008 Share Posted October 30, 2008 Try something like this...code is not tested.. $sql = "SELECT * FROM ".$prefix."_bus_monitor"; $result = $db->sql_query($sql); $arrNotRunning = array(); while ( $row = $db->sql_fetchrow($result) ) { if($row['brun'] != 'running') $arrNotRunning[] = $row['brun']; } if(count($arrNotRunning) == 0) { echo "All buses running normally.<br />"; } else { echo "The following buses are delayed or canceled: <br />"; $intBus = 1; foreach($arrNotRunning as $strBusStatus){ echo "Bus No: ".$intBus.": ".$strBusStatus."<br/>"; $intBus++; } } Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678208 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 Almost got it, but I only need it to show the buses that are canceled and/or delayed. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678215 Share on other sites More sharing options...
samshel Posted October 30, 2008 Share Posted October 30, 2008 please post the ouput it showing now. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678221 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 The following buses are delayed or canceled: Bus No: 1: Running Bus No: 2: Delayed Bus No: 3: Cancelled Bus No: 4: Running Bus No: 5: Running Bus No: 6: Running Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678496 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 change this: if($row['brun'] != 'running') $arrNotRunning[] = $row['brun']; to either this: if($row['brun'] != 'Running') $arrNotRunning[] = $row['brun']; or to this (probably better): if(strcasecmp($row['brun'], 'running') == 0) $arrNotRunning[] = $row['brun']; Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678505 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 Almost, not it is showing the opposite to what I need: The following buses are delayed or canceled: Bus No: 1: Running Bus No: 2: Running Bus No: 3: Running Bus No: 4: Running Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678508 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 lol, damn. i'm being silly again. i need to get some sleep... if(strcasecmp($row['brun'], 'running') != 0) $arrNotRunning[] = $row['brun']; should work and please drop the first ":" on each line... Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678510 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 Woohoo, it worked, thanks. That is 1/2 of my code done, Now just to get the rest of the mysql data to output. Thanks guys for all of your help. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678554 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 Well I thought I had it, sorry for being a pain guys. I am also trying to get some more data to output with the delayed/canceled status. What I am trying to get is: Bus no 1: School Name Delayed Bus no 2: School Name Canceled etc. I can get the school name to display but for some reason, it is only displaying 1 school name for all entries. I tried a number of different things, and so far...all wrong...lol $arrNotRunning = array(); while ( $row = $db->sql_fetchrow($result) ) { if(strcasecmp($row['brun'], 'running') != 0) $arrNotRunning[] = $row['brun']; $bschool = $row['bschool']; } if(count($arrNotRunning) == 0) { $content = "All buses running normally.<br />"; } else { $content = "The following buses are delayed or canceled: <br />"; $intBus = = 1; foreach($arrNotRunning as $strBusStatus){ $content .= "Bus ".$intBus.": ".$bschool." ".$strBusStatus."<br/>"; $intBus++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678587 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 change: if(strcasecmp($row['brun'], 'running') != 0) $arrNotRunning[] = $row['brun']; $bschool = $row['bschool']; to: if(strcasecmp($row['brun'], 'running') != 0){ $arrNotRunning['status'][] = $row['brun']; $arrNotRunning['school'][] = $row['bschool']; } and use $arrNotRunning for everything. otherwise use 2 separate arrays and coordinate their output. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678599 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 This is what I ended up with: The following buses are delayed or canceled: Bus 1: Array Bus 2: Array I may have to try and coordinate the arrays for their output. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678629 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 This is what I ended up with: The following buses are delayed or canceled: Bus 1: Array Bus 2: Array I may have to try and go the other route and try to coordinate the arrays Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678632 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 Sorry double posted Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678634 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 :-\ you need to change the code that echo's too: $loopCount = count($arrNotRunning['status']); for($i = 0; $i < $loopCount; $++i){ $content .= "Bus ".$i.": ".$arrNotRunning['school'][$i]." ".$arrNotRunning['status'][$i]."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678636 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 Ok, Guess I missed something: $sql = "SELECT * FROM ".$prefix."_bus_monitor"; $result = $db->sql_query($sql); $arrNotRunning = array(); while ( $row = $db->sql_fetchrow($result) ) { if(strcasecmp($row['brun'], 'running') != 0){ $arrNotRunning['status'][] = $row['brun']; $arrNotRunning['school'][] = $row['bschool']; } if(count($arrNotRunning) == 0) { $content = "All buses running normally.<br />"; } else { $content = "The following buses are delayed or canceled: <br />"; $loopCount = count($arrNotRunning['status']); for($i = 0; $i < $loopCount; $++i){ $content .= "Bus ".$i.": ".$arrNotRunning['school'][$i]." ".$arrNotRunning['status'][$i]."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678641 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 lol that's my bad: for($i = 0; $i < $loopCount; $++i){ should be for($i = 0; $i < $loopCount; ++$i){ also, change: $content .= "Bus ".$i.": " to $content .= "Bus ".($i + 1).": " otherwise you'll get bus no. 0... Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678642 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 lol, yeah, I caught the ++$i but trying to find the other error, Thanks for your help so far, it is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678645 Share on other sites More sharing options...
SkyRanger Posted October 30, 2008 Author Share Posted October 30, 2008 D'oh, Nevermind, found it stupid me forgot to add a } at the end, works now, thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/#findComment-678651 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.