Aurora Aurora - 6 months ago 7
PHP Question

PHP Find value in array and echo something different

I have the following code:

$media = $db->query("SELECT * FROM uploaded_photos WHERE user_id='".$profile->id."' LIMIT 20");
$picheck = $db->query("SELECT * FROM users WHERE id='".$profile->id."'");
$picheck = $picheck->fetch_object();

if($media->num_rows == 0) {
$uploaded_photos = 0;
} else {
$uploaded_photos = array();
while($photo = $media->fetch_object()) {
$photos[] = array('type'=>'uploaded','id' => $photo->id, 'path' => $photo->path);
}
}
//IN A RELATED PHP FILE:
<?php
$c=0;
if(!empty($photos)) {
for($i = 0; $i < count($photos); $i++) {
if($photos[$i]['type'] == 'instagram') {

} else {
$c++;
echo '<div class="col-sm-3 col-md-3 col-lg-3 thumbnail m-5">';
echo '<div class="image-container">';
echo '<img src="'.$system->getDomain().'/uploads/'.$photos[$i]['path'].'" class="img-responsive">';
echo '
<div class="caption">
<h4>';
//HERE IS MY PROBLEM, IF THE PATH OF THE USERS CURRENT PROFILE PHOTO IS THE SAME AS THE PATH OF THE PHOTO DISPLAYED, IT SHOULD ECHO SOMETHING DIFFERENT.
if(in_array($picheck->profile_photo, $photos['path'], true)){ echo '<a href="#" class="no-underline pull-left"> <i class="fa fa-exclamation" data-toggle="tooltip" data-placement="right" data-title="Your Profilepicture" placeholder="" data-original-title="" title=""></i> </a>';
}
else { echo '<a href="#" onclick="deletePhoto('.$photos[$i]['id'].')" class="no-underline pull-left"> <i class="fa fa-trash" data-toggle="tooltip" data-placement="right" data-title="'.$lang['Delete_Photo'].'" placeholder="" data-original-title="" title=""></i> </a>';
}
echo '
<a href="#" onclick="setAsProfilePhoto('.$photos[$i]['id'].')" class="no-underline pull-right"> <i class="fa fa-user" data-toggle="tooltip" data-placement="left" data-title="'.$lang['Profile_Photo'].'" placeholder="" data-original-title="" title=""></i> </a>
</h4>
</div>
';
echo '</div>';
echo '</div>';
}
}
}
if($c==0) {
echo 'YOU DONT HAVE ANY PICTURES';
}
?>


I have been searching high and low to solve this, I hope someone can help me out. I would like to thank all who put efford in helping me out on this beforehand.

Answer

The problem is because of this line,

if(in_array($picheck->profile_photo, $photos['path'], true)){ ...
                                     ^^^^^^^^^^^^^^^

The second argument of in_array() function i.e $photos['path'] is wrong because $photos array doesn't have an index named path.

So the correct condition for if block would be,

if(in_array($picheck->profile_photo, $photos[$i], true)){ ...
Comments