在数组中循环两个

时间:2017-10-13 23:15:36

标签: php loops

我正在使用类似按钮创建此帖子系统并计算社交网站。

我的最终目标是将这两组结果循环在一起。因此,类似的计数与各个帖子一起循环。帖子和喜欢是按顺序排列的。一切都匹配,除了我得不到这些,同时获取结果循环在一起。

发布循环

<table class="postborder">                  
  <?php
  $query1 = "SELECT * FROM tbl_images ORDER BY id DESC"; 

  $result = mysqli_query($connect, $query1);  
  while($row = mysqli_fetch_array($result)) { 
  ?>
  <div id="newpost">  
    <tr>
      <td id="userpost"><?php echo $row['username']; ?> </td>
    </tr>
    <tr>
      <td>
      <hr id="hrline">
      <img id="newimgpost" src="data:image/jpeg;base64,<?php echo base64_encode($row['name']); ?>" height="500" width="500" class="img-thumnail" />  
      </td>
    <tr>
      <td id="textpost">
      <?php echo $row['textpost']; ?>   
      </td>
    </tr>
    <tr>
      <td id="likebutton">
      <?php }  ?>
    </div>
</table>  

像计数循环

<?php  
 //index.php  
 //session_start();  
 //SESSION['userid'] = (int)3;  
 $connect = mysqli_connect("localhost", "root", "", "snazzer");  
 $query2 = "  
      SELECT tbl_images.id, tbl_images.textpost,  
      COUNT(likes.id) as likes,  
      GROUP_CONCAT(users.name separator '|') as liked  
      FROM  
      tbl_images  
      LEFT JOIN likes  
      ON likes.postid = tbl_images.id  
      LEFT JOIN users  
      ON likes.userid = users.userid  
      GROUP BY tbl_images.id  
      ORDER BY id DESC
 ";  
 $result2 = mysqli_query($connect, $query2); 
 if (!$result2) {
    printf("Error: %s\n", mysqli_error($connect));
    exit();
} 
 while($row = mysqli_fetch_array($result2))  
 {  
      echo '<h3>'.$row["textpost"].'</h3>';  
      echo '  
      <a href="profile.php?type=postid&id='.$row["id"].'">LIKE</a>';
      echo '<p>'.$row["likes"].' People like this</p>';  
      if(count($row["liked"]))  
      {  
           $liked = explode("|", $row["liked"]);  
           echo '<ul>';  
           foreach($liked as $like)  
           {  
                echo '<li>'.$like.'</li>';  
           }  
           echo '</ul>';  
      }  
 }  
 if(isset($_GET["type"], $_GET["id"]))  
 {  
      $type = $_GET["type"];  
      $id = (int)$_GET["id"];  
      if($type == "postid")  
      {  
           $query = "  
           INSERT INTO likes (userid, postid)  
           SELECT {$_SESSION['userid']}, {$id} FROM tbl_images   
                WHERE EXISTS(  
                     SELECT id FROM tbl_images WHERE id = {$id}) AND  
                     NOT EXISTS(  
                          SELECT id FROM likes WHERE userid = {$_SESSION['userid']} AND postid = {$id})  
                          LIMIT 1  
           ";  
           mysqli_query($connect, $query);  
           header("location:profile.php");  
      }  
 }  
 ?>

1 个答案:

答案 0 :(得分:0)

我相信你可以一起运行两个查询。这很简单;用分号分隔每个查询:

$query = "SELECT * FROM tbl_images ORDER BY id DESC;
          SELECT tbl_images.id, tbl_images.textpost, COUNT(likes.id) as likes, GROUP_CONCAT(users.name separator '|') as liked FROM tbl_images LEFT JOIN likes ON likes.postid = tbl_images.id LEFT JOIN users ON likes.userid = users.userid GROUP BY tbl_images.id ORDER BY id DESC";

或者通过递增值来分隔变量,您可以阅读解释它的文档here

$query = "SELECT * FROM tbl_images ORDER BY id DESC";
$query .= "SELECT tbl_images.id, tbl_images.textpost, COUNT(likes.id) as likes, GROUP_CONCAT(users.name separator '|') as liked FROM tbl_images LEFT JOIN likes ON likes.postid = tbl_images.id LEFT JOIN users ON likes.userid = users.userid GROUP BY tbl_images.id ORDER BY id DESC"; 

然后像往常一样运行代码,除非您将mysqli_query更改为mysqli_multi_query

$result = mysqli_multi_query( $connect, $query );
while($row = mysqli_fetch_array( $result )) {
    //... code your table here ...//
}

你也可以使用面向对象的方法:

$result = $connect->multi_query( $query );

但当然两种方法都应该可行。请记住,您可能容易受到mysqli_multi_query的SQL注入攻击。这是PHP的官方警告:

  

安全注意事项

     

API函数mysqli_query()和mysqli_real_query()不设置激活服务器中多个查询所需的连接标志。额外的API调用用于多个语句,以减少意外SQL注入攻击的可能性。攻击者可能会尝试添加诸如的语句; DROP DATABASE mysql或;选择休眠(999)。如果攻击者成功将SQL添加到语句字符串但未使用mysqli_multi_query,则服务器将不会执行第二个注入的恶意SQL语句。

考虑

您可以随时在数组中保存while循环中的结果,并在foreach循环中运行它们,或者将它们单独输出为$array['key']。这对您的查询来说是一个简单的解决方法:

<?php 

// first query
$query1 = "SELECT * FROM tbl_images ORDER BY id DESC"; 

$result = mysqli_query($connect, $query1);  
while($row = mysqli_fetch_array($result)) { 
    $array1[] = $row;
}

// second query
$query2 = "  
  SELECT tbl_images.id, tbl_images.textpost,  
  COUNT(likes.id) as likes,  
  GROUP_CONCAT(users.name separator '|') as liked  
  FROM  
  tbl_images  
  LEFT JOIN likes  
  ON likes.postid = tbl_images.id  
  LEFT JOIN users  
  ON likes.userid = users.userid  
  GROUP BY tbl_images.id  
  ORDER BY id DESC
";  
$result2 = mysqli_query($connect, $query2); 
while($row = mysqli_fetch_array($result2)) { 
    $array2[] = $row;
}

设置好这些记录后,您现在可以使用$array1$array2来设置您的表格。

要获取他们的密钥和设置,您可以使用print_rvar_dump,然后您就可以看到key => value对:

print_r( $array1 );
echo '<br />';
print_r( $array2 );