如何在PHP中检查while循环中的if else语句?

时间:2016-03-12 13:27:02

标签: php

我调用了一个函数,该函数从db&amp ;;中的表中检索数据。我想添加一个if else语句来检查表以查看某些指定的字段是否为空。所以为了做到这一点,我把它写成我的代码:

    <?php 
function post_pages(){
global $con;
$get_settings = "select * from site_settings;";
$run_settings = mysqli_query($con,$get_settings);
while($row_settings = mysqli_fetch_array($run_settings)){
    $addpost_id = $row_settings['id'];
    $addpost_name = $row_settings['sitename'];
    $addpost_url = $row_settings['siteurl'];
    $addpost_tags = $row_settings['sitetags'];
    $addpost_pp1= $row_settings['postpageone'];
    $addpost_pp2= $row_settings['postpagetwo'];
    $addpost_pp3= $row_settings['postpagethree'];
    $addpost_imgG= $row_settings['imagegalleryone'];
    $addpost_imgG= $row_settings['imagegallerytwo'];
    $addpost_vG= $row_settings['videogallery'];

if (empty($addpost_pp1) || (empty($addpost_pp2)) || (empty($addpost_pp3))){
    echo "<h4 class='widgettitle'>Please adjust your site settings first!</h4>";
}else{
        echo "
            <form action='' method='POST'>
                <select name='pages'>
                    <option value='home'>home page</option>
                    <option value='blogs'>blogs page</option>
                    <option value='aboutus'>aboutus page</option>
                </select></br>
                <input type='submit' name='submit' value='Submit'/>
            </form>
        ";
       }        
     }
   }
            ?>

但问题是当表格为空时它不会显示错误信息,即&#34;请先调整设置&#34;。我想因为我没有在这个while循环中正确插入条件语句。所以,如果你知道如何解决这个问题,请帮助我。谢谢......

1 个答案:

答案 0 :(得分:1)

如果您的表为空,则不会执行while循环中的行。因为没有什么可以迭代的,这就是你没有看到你给出的信息的原因。解决方案是在while循环之前使用if条件。像,

if (mysqli_num_rows($run_settings) == 0) {
    echo "No records found";
} else {
    // Your while logic
}