PHP在数组中查找值并回显不同的东西

时间:2016-05-14 09:27:42

标签: php

我有以下代码:

$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';
    }
    ?>

我一直在寻找解决这个问题,我希望有人可以帮助我。我要感谢所有让efford事先帮助我的人。

1 个答案:

答案 0 :(得分:0)

问题在于这条线,

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

in_array()函数的第二个参数,即$photos['path']是错误的,因为$photos数组没有名为path的索引。

if 块的正确条件是

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