Having trouble with "OR" and strpos();

Hello,

I want to echo the 1st record from a sql result that meets a condition.

    while($row = mssql_fetch_array($result)) {
  echo "<li><h3 ";
  
  if (strpos($row["Summary"],'URGENT')  !== false or strpos($row["Summary"],'Urgent')  !== false or strpos($row["Urgency"],'Priority 2 - High')  !== false or strpos($row["Urgency"],'Priority 1 - Emergency') !== false) { echo 'class="red" '; } ;
  
  if (strpos($row["Summary"],'URGENT') !== false) { ++$urgentTickets; } ;
  if (strpos($row["Summary"],'Urgent') !== false) { ++$urgentTickets;} ;
  if (strpos($row["Urgency"],'Priority 2 - High') !== false) { ++$urgentTickets;} ;
  if (strpos($row["Urgency"],'Priority 1 - Emergency') !== false) { ++$urgentTickets;};

     echo ">".$row["Ticket"]." - ".$row["Summary"]."</h3><p>".$row["Company"]."</p><span class='ui-li-count'>".round($row["Hrs"],2)."Hours</span></li>";
} 

This code will list out tickets, determines which ones are urgent (and other conditions) and makes them red. I want to be able to display a line that says “There are xx urgent tickets starting with ticket ###”

How do I determine the first urgent ticket?

Thanks,
Edwin

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

Hi Edwin,

One way would be to set empty variable ($firstUrgentTicket) outside of your while loop. Everytime you loop over a ticket, if that ticket is urgent AND $firstUrgentTicket is empty, assign the ticket ID to the variable.

If you’re interested, you could make your loop code a little more flexible by moving the urgency checking logic to its own function:

function isUrgent($row) {
    $isUrgent = false;
    $needles = array( 'urgent', 'priority 2 - high', 'priority 1 - emergency' );
    foreach ($needles as $needle) {
        if ( strpos( strtolower($row['summary']), $needle) !== false
            OR strpos( strtolower($row['urgency']), $needle) !== false )
        {
            $isUrgent = true;
        }
    }
    return $isUrgent;
}

Here, all the phrases to identify urgent tickets are stored in an array. If you ever need to add additional keywords to match, you can just add them to the array. As the logic is now contained in a function, it’s also easy to reuse anywhere else in your app that you need to check if a ticket is urgent, without having to duplicate code.

$firstUrgentTicket = null;
$urgentTicketCount = 0;

while($row = mssql_fetch_array($result)) {
    $class = '';
    $hours = round($row["Hrs"], 2);
  
    if ( isUrgent($row) ) {
        $class = 'red'
        $urgentTicketCount++;

        if ( ! $firstUrgentTicket ) {
            $firstUrgentTicket = $row['Ticket'];
        }
    }
 
    echo "<li><h3 class=\"$class\">$row[Ticket] - $row[Summary]</h3>";
    echo "<p>$row[Company]</p><span class=\"ui-li-count\">$hours Hours</span></li>";
}

echo "There are $urgentTicketCount urgent tickets starting with ticket $firstUrgentTicket";

Thanks for the reply. Looks like my boss wants me to work on something else instead of this issue. I will study this on my own time and see if i can understand your code and get it to work.

Thanks again.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.