foreach和/或如果是数组语句

时间:2017-03-30 03:40:46

标签: php

我有一个显示列表中个人资料的个人资料。如下图所示。

enter image description here

用户

 id    |  email  |  full_name  |  job_title  | bio |  profile_photo  

图片

 image_id  | id  | artist_img

代码

<?php 

$db = dbconnect();
$stmt = $db->prepare('SELECT
 users.email,
 users.full_name,
 users.job_title,
 users.bio,
 users.profile_photo,
 images.id,
 images.artist_img
FROM users
INNER JOIN images ON users.id=images.id GROUP BY images.id');
$stmt->execute();
$result = $stmt->get_result();

while (($row = mysqli_fetch_assoc($result)) != false) {

        $id = $row['id'];        
        $full_name = $row['full_name'];    
        $email = $row['email'];   
        $job_title = $row['job_title'];
        $bio = $row['bio'];
        $ProfilePhoto = $row['profile_photo'];
        $artist_img = $row['artist_img'];    


        if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
            $image = "$ProfilePhoto";
        } else {
            $image = "avatar.jpg";
       }

   echo "<div class='container team-wrap'>
           <div class='row'>
             <div class='col-md-6'>
                <img class='img-responsive' src='artist/$image'>
               </div>
                 <div class=\"col-md-6\">
                    <strong>$full_name<br>$job_title</strong>
                      <br>
                      <p>$bio</p>
                      <a href='mailto:$email' class='btn btn-info'>Contact Me</a>
                </div>
             </div>
          </div>

          <div class=\"container space team-wrap\">
           <div class=\"row\">
              <div class=\"col-lg-12\">
                    <div id=\"gallery-slider\" class=\"slider responsive\">
                      <div>";




                          echo"
                        <img src=\"gallery/$artist_img\" alt=\"\"></a>";  




                      echo "</div>
                    </div>
                  <hr>
               </div>
            </div>
        </div>";
    }        
 ?>

问题区域

echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>"; 

我遇到的问题是,如果用户有5张图片,它会重复每张图片的个人资料,每个图片会添加5张个人资料。

并且根本不显示其他用户个人资料。显示它是如何显示是看图像,例如它在配置文件1下有4个图像,它显示有配置文件图片..好吧,它为每个图像重新编写所有信息我想要与用户显示具有相同ID的图片像下面的滑块一样..

并且它也拒绝显示其他用户的其他个人资料。

1 个答案:

答案 0 :(得分:1)

是的,因为它没有看到变量,因为你将它作为文本回显

echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>"; 

应该是

$r=0;
foreach ($images as $image.id){
    [$artist_img=$images[$r];
    echo "<img src=\"gallery/".$artist_img."\" alt=\"\"></a>"; 
    $r++;
    }

//

我不想回应很多HTML,因为很容易犯错,我更喜欢它是保留在html中,只是回应我的变量,但那只是我

<html>
   <body>
      <a><img src="gallery/<? php echo $artist_img; ?>" alt=""></a> 
   </body>
</html>

你在这里看到你将图片的变量作为单行

获取
$artist_img = $row['artist_img'];

你需要把它变成一个数组,因为它是一个你必须记住的数组,使用inner生成一些数据,也许对你来说最好运行2个查询第二个查询加载images.inner join对于有些人但很复杂,并没有真正给出任何好处,因为仍然会搜索整个表格两次以获得结果 这样的事可能对你有用

 $artist_img2=array();
//because there are more then one piece of data in it
$artist_img2 = $row['images']; 
//then you need to do another loop to put the data in variables or echo them out     
// in the loop
    //note the row refers to id and not image.id because in the inner array the key will be id 
     $artist_img3=row2['id'];
     echo "<img src=\"gallery/".$artist_img3."\" alt=\"\"></a>"; 
     //end loop