在DIV中按职位人气排序

时间:2018-07-07 12:02:24

标签: php mysql sql mysqli

我陷入了一些PHP代码。因此,就像我要在我关注/订阅的特定用户的最受欢迎帖子中订购帖子一样。

这是我的代码:

<?php
$link = mysqli_connect("localhost", "root", "", "DB");
if($link === false){
    die("ERROR: Could not connect. "
                . mysqli_connect_error());
}

$sql = "SELECT * FROM subscribers
                 INNER JOIN post ON subscribers.subscribe_to = post.username
                 ORDER BY post.views DESC;";

if($res = mysqli_query($link, $sql)){
    if(mysqli_num_rows($res) > 0){
        $lastusername = "";
        while($row = mysqli_fetch_array($res)){

            if($lastusername != $row["username"]) {
                if($lastusername != '') {
                    //  Close previous div
                    echo "</div>\n";
                }
                echo "<center><div  class='container'>\n";
                $lastusername = $row["username"];
            }
                 echo '<div class="innercontainer">'.
                          '<img src="'.$row["image"].'" class="img_post">'.
                      '</div>';
        }

        echo "</div>\n";
        mysqli_free_result($res);
    } else{
        echo "No Matching records are found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " 
                                . mysqli_error($link);
}

mysqli_close($link);
?>

我有3张桌子:

=> |表| 1 |用户名|

 |ID    | USERNAME | SUBSCRIBERS |
 |------|----------|-------------|
 |1     | USER1    | 5000     
 |2.    | USER2    | 10000   |

=> |表| 2 |订阅者|

 |ID   | subscriber_from | subscriber_to  |
 |------|-----------------------------|---------------------|
 |1     | User_example     | USER1           |
 |2.    | User_example    | USER2          |

=> |表| 3 |发布|

  |ID   | POST_CONTENT | USERNAME | VIEWS
  |------|-----------------------------|---------------------|
  |1     | This is post no.1   | USER1             |  500
  |2.    | This is post no.2   | USER2          |600
  |3     | This is post no.3   | USER2          |200
  |4     | This is post no.4   | USER1          |800

谢谢!!

0 个答案:

没有答案