Trying to print/echo a statement if results are nil

Hi guys

This is a basic subject but I am struggling to get some code working.
The code below was written by someone else and I am trying to amend it. This code goes out in an email to users. It is meant to populate details from the database but when there are no results, it should give a line of text ‘No results found.’

The code works perfectly well except for the last bit. If there are no results to show, it is not giving the message to the user.

I have tried to change it to an echo statement but still no success. I’m probably missing something really obvious. Any help appreciated.

Many thanks
Glenn

Code Sample:

<?php if($user->email_include_all == User::EMAIL_INCLUDE_ALL){?>
<h3>All overdue renewals:</h3>
<table>
	<tr>
		<th>End User</th>
		<th>Vendor</th>
		<th>Expiration Date</th>
	</tr>	
<?php
    
    $current_date = date('Y-m-d');
    $next_date = date('Y-m-d', strtotime($current_date . '-365 days'));
 $renewals_query_all = Renewal::find()->where([
        '>',
        'expiration_date',
        $next_date
    ])
        ->andWhere([
        '<',
        'expiration_date',
        $current_date
    ])
        ->andWhere([
        'IN',
        'id',
        $renewals_ids_all
    ]);
    
    foreach ($renewals_query_all->batch(500) as $renewals) {
        
        if (! empty($renewals)) {
            foreach ($renewals as $renewal) {
                ?>
<tr>
  <td><a href="<?= Url::toRoute(['company/view-renewal','id'=>$renewal->id])?>"><?= $renewal->end_user; ?></a></td>
  <td><?= $renewal->vendor_name; ?></td>
  <td><?= $renewal->expiration_date; ?></td>
</tr>

<?php
            }
        } else {
            ?>
<h3>No overdue results found.</h3>
<?php
        }
    }
    
    ?></table>
<?php }?>

It looks like there are classes and methods at play here that we are not party to, so hard advise accurately.
Based on assumptions about the unknown methods, I’ll take a guess and think that the if condition should be around the $renewals_query_all = part.
Otherwise you are running a foreach regardless of whether the query returns any result.

It would probably help a great deal to separate all this logic and querying away from the html. Deal with the data you want to retrieve, then once you have it all and have processed it all, then you can start formatting it into some html.

1 Like

I think you’re right, the issue here is that the “No overdue results” message is in the else clause for the if (! empty ($renewals)){ line, and as $renewals is the “internal” variable for the surrounding foreach() loop, will it ever be empty? Unless the renewals query returns an array or object that includes empty results, which would make no sense.

1 Like

I prefer to do all PHP and Mysql server side processing at the very top of the PHP web page.

First create the default “No overdue results found” $message.

Then try an if(…) and if successful populate a new empty $message.

Create the HTML table and insert <?PHP echo $message; ?> into the table.

This method is cleaner, far more readable, easier to debug and less jumping into and out of PHP.

2 Likes

Backon the desktop and above post expired :frowning:

Try this:

<?php
  
  $message = NULL;  

  if($user->email_include_all == User::EMAIL_INCLUDE_ALL)
  {
    $current_date = date('Y-m-d');
    $next_date    = date('Y-m-d', strtotime($current_date . '-365 days') );

    // BEWARE BELOW - ONLY LINEFEED AFTER ____TMP
    $sql = <<< ____TMP
    where (
        [
        '>',
        'expiration_date',
        $next_date
        ]
    )
    ->andWhere(
      [
        '<',
        'expiration_date',
        $current_date
      ]
    )
    ->andWhere(
      [
        'IN',
        'id',
        $renewals_ids_all
      ]
    );
____TMP;
// BEWARE ABOVE - ONLY LINEFEED AFTER ____TMP;    
  // echo $sql;          

  $renewals_query_all = Renewal::find()->$sql;
  foreach ($renewals_query_all->batch(500) as $renewals)
  {
    if (! empty($renewals))
    {
      $message = '';  
      foreach ($renewals as $renewal)
      {
        $viewRenewal = Url::toRoute(
          [
            'company/view-renewal',
            'id' => $renewal->id
          ]
        );
        $tmp = <<< ______TMP
          <tr>
            <td>
              <a href="$viewRenewal">
                {$renewal->end_user}
              </a>
            </td>
            <td> {$renewal->vendor_name} </td>
            <td> {$renewal->expiration_date} </td>
          </tr>
______TMP;
        $message .= $tmp;  
      }//foreach

    }//endif ( empty($renewals) )  

  }//foreach ($renewals_query_all->batch(500) as $renewals)

}//if($user->email_include_all == User::EMAIL_INCLUDE_ALL)


// OUTPUT =================================================
    if($message)
    {
      echo '<table>
	        <tr>
		      <th>End User</th>
		      <th>Vendor</th>
		      <th>Expiration Date</th>
	        </tr>	
            ';
        echo $message; >
      echo '</table>';
    }else{
      echo '<h3>No overdue results found.</h3>';        
    }//endif($message)

Thank you John! I really appreciate that. I have updated my file which runs on a daily cron job. I will let it run in a few hours and see how it turns out. Will update afterwards.

Again, many thanks.
Glenn

1 Like

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