简体   繁体   中英

How to check an if else statement inside a while loop in PHP?

I have called a function that retrieves data from my table in db & I want to add an if else statement to check the table to see if some specified fields are empty or not. So in order to do that I wrote this as my code:

    <?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>
        ";
       }        
     }
   }
            ?>

But the problem is when the table is empty it does not show the error message which is "Please adjust the settings first". I think because I didn't insert the conditional statement correctly inside this while loop. So please if you know how to solve this problem help me. Thanks ...

If your table is empty, the lines inside while loop will not be executed. Because there is nothing to iterate and that is the reason you are not seeing the message you have given. The solution is to have an if condition before your while loop. Something like,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM