Jump to content

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/130618-solved-else-if-or-switch/
Share on other sites

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?

$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

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++;
   }
}

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'];

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++;
   }
}
?>

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.

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/>";
   } 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.