ORDER BY post popularity within DIV

2019-08-21 09:40发布

问题:

i am stuck up with some php code . so it is like i want to order post inside the probably order by most popular post of that particular user whom i follow/subscribe.

here is my code:

<?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);
?>

I have 3 tables :

=> |Table | 1 | username |

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

=> |Table | 2 | subscribers |

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

=> |Table | 3 | post |

  |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

THANKS IN ADVANCE !!