0

I am trying to get this code to work. I am getting a list of results from my database which are less than 30 days old. And displaying them in my div.

This works fine, however if there are no results I want to echo out No Recent Activity, and I am unable to get this bit to work. I am using a MySQL num row count and else statement to try and get it to do what I need but It won't show no recent activity even if there is no results.

Please can someone show me where I am going wrong.

thanks,

Code:

<div class="dashboard_stats">
<?php require_once 'config.php'; ?>

<?php
$table = 'recent_activity';


echo '<div class="dashboard_stats_heading"><table class="table_other"><tr><td>Description</td><td>Reference</u></td><td>Status</td><td>Supplier Name</td></tr></table></div>';

$query1 = "SELECT * FROM $table WHERE user_id = '{$_SESSION['id']}' AND date > NOW() - INTERVAL 30 DAY ORDER BY date DESC";
$result1 = mysql_query($query1);
while ($row1 = mysql_fetch_array($result1)) {


    if(mysql_num_rows($result1)>0){

    $date = $row1['date'];
    require_once 'time_ago.php';

    if($row1['activity_type']==='Bank'){
        $activity_type = 'Bank Details Changed';
    }else{
        if($row1['activity_type']==='Username'){
            $activity_type = 'Username Changed';   
        } else {
            if($row1['activity_type']==='Password'){
                $activity_type = 'Password Changed';   
            } else {
                if($row1['activity_type']==='Contact'){
                    $activity_type = 'Contact Details Changed';   
                } else {
                    if($row1['activity_type']==='Invoice'){
                        $activity_type = 'Invoice Submitted';   
                    } else {
                        if($row1['activity_type']==='Trading'){
                            $activity_type = 'Trading Suspended';     
                        }
                    }
                }
            }
        }
    }




        echo '<table><tr><td><p>'.$activity_type.'</p></td><td><p>1234</p></td><td><p>'.$row1['status'].'</p></td><td><p>'; 
        echo pretty_relative_time($row1['date']);
        echo '</p></td></tr><tr class="separator"></tr></table>';

    }else{

        echo   '<div id="content">No Recent Activity</div>';   

    }
}

echo '</div>';
?>

</div>
Alex
  • 626
  • 7
  • 16
james
  • 153
  • 1
  • 17
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 03 '15 at 15:12
  • 1
    sidenote: `if/else if/else` and why the `===`? – Funk Forty Niner Jun 03 '15 at 15:12
  • `mysql_num_rows` should be outside the while loop, not inside. And at least switch to mysqli. Also this is minor, but it'd be cleaner to use a [switch case](http://php.net/manual/en/control-structures.switch.php) for a series of comparisons against the same variable . – Andy Hoffner Jun 03 '15 at 15:26

1 Answers1

0

You need to move the if statement out of the while loop as its not being run if theres nothing for the while loop to iterate over.

switch the if and while around

if(mysql_num_rows($result1)>0){

while ($row1 = mysql_fetch_array($result1)) {
Gareth Luckett
  • 896
  • 7
  • 13