0
<?php 
    $student_info = getAllRows('student');
    $counter = 1;
    foreach ($student_info as $key => $student) {
?>
<tr>
    <td><?php echo $student['email']; ?></td>
    <td>
    <?php 
        if (isset($student['image']) && !empty($student['image']) && file_exists(UPLOAD_DIR.'student/'.$student['image'])) {
            echo "<img src='upload/student/".$student_info['image']."' class='img img-responsive img-thumbnail' width='300px'> ";
        }else{
            echo "<img src='upload/logo.png' class='img img-responsive img-thumbnail' width='300px'> ";
        }
    ?>
    </td>
    <td><a href="addaccount.php?id=<?php echo $student['id'] ?>&amp;act=edit">edit</a>/<a href="account.php?id=<?php echo $student['id'] ?>&amp;act=delete" onclick="return confirm('Are You sure you want to delete this student Account?');">delete</a>/<a href="change_password.php?id=<?php echo $student['id'] ?>&amp;act=change_password">Change Password</a></td>
</tr>

I have use this code... but showing some error

error:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\school\admin\view_student_profile.php on line 58

I want to get data from db... and then show all the photo... there is more than one data in db... and also there is file in the source folder... my problem is that i wanna show related photo if present else a website logo

1 Answers1

1

Why not just wrap your foreach($student_info) with a !empty($student_info) checking before doing any further looping with $student_infoarray ?

if(!empty(student_info)){
    foreach ($student_info as $key => $student) {
     // your other code goes here 
    }
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • actually i just tested and `empty` returns true if a var is set to `false`, so checking if empty is good. but it would be defined so the isset isn't needed. was almost thinking that since it was set and would contain iether records or a false empty would be false... not enough coffee yet i guess – ivanivan Sep 15 '18 at 16:01
  • There's no need to check both isset and empty at the same time. https://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty/4560099 – Qirel Sep 15 '18 at 16:23
  • @Qirel Yes, you're right :), I've edited my answer. THanKs – A l w a y s S u n n y Sep 15 '18 at 16:31
  • a new empty array can be set so it's better to check both isset() an empty() boolean at thesame time – Niranjan2054 Sep 17 '18 at 02:10