dpfln86244 2015-06-03 15:11
浏览 44
已采纳

mysql如果num行超过0显示结果,则echo echo no recent activity - not working?

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>
  • 写回答

1条回答 默认 最新

  • dongru3726 2015-06-03 15:13
    关注

    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)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?