On a web or mobile app, how we fetch and echo out information play a major rule. We all have seen how social network sites like facebook, twitter and Instagram display their data randomly. How would you feel if you build a new web app with just few users and anytime you refresh your browser the newest post is always top of the old posts, about an hour if there is no new post yet that same post will still be on top. This is not really good most especially if it is news feed category. To avoid making the app look boring we need to display the information randomly.


Displaying individual private messages using ID DESC or ID ASC is good, but it wouldn’t go with regular post category. In this tutorial, we’re going to briefly discus the basic three ways to fetch and display data.

We’ll handle one after the other starting from:

  • asc
  • desc
  • rand

Order by Id asc method:
We’re all familiar with ORDER BY ID ASC, this very method enable us to display information starting from the lowest number to the highest number. The example below will fetch data from user_table and display them in ascending form.


<!DOCTYPE html>

<html>

<head>

<title>php information display method</title>

</head>
<body>
<h2>php information display</h2>
<?php
//Db connection
$mydb = new mysqli("localhost", "root", "", "storagedatabase");
//select all details from user_table
$select = "SELECT * from user_table ORDER by ID ASC LIMIT 3";
$query = mysqli_query($mydb,$select);
//If the above select statement execute then do store the values in row variable
if($query){
while($row = mysqli_fetch_array($query)){
echo $row['user_id'];
echo "<br /><br />";
echo $row['user_first_name'];
echo "<br /><br />";
echo $row['user_sex'];
echo "<hr />";
}
}
?>
</body>
</html>

The output of the above code will be:
User_id: 1
User_first_name: Jeremiah
User_sex: male

User_id: 2
User_first_name: Tortor
User_sex: male

User_id: 3
User_first_name: joy
User_sex: female

Order by id desc method:
Displaying data in the desc pattern is not new to us. This method simply echo out information starting from the last inserted record to the first one. The example code below shows that the asc order method for the first example has been changed to desc. 


<!DOCTYPE html>

<html>

<head>

<title>php information display method</title>

</head>
<body>
<h2>php information display</h2>
<?php
//Db connection
$mydb = new mysqli("localhost", "root", "", "storagedatabase");
//select all details from user_table
$select = "SELECT * from user_table ORDER by ID DESC LIMIT 3";
$query = mysqli_query($mydb,$select);
//If the above select statement execute then do store the values in row variable
if($query){
while($row = mysqli_fetch_array($query)){
echo $row['user_id'];
echo "<br /><br />";
echo $row['user_first_name'];
echo "<br /><br />";
echo $row['user_sex'];
echo "<hr />";
}
}
?>
</body>
</html>

The output of the above code will be:
User_id: 3
User_first_name: Joy
User_sex: female

User_id: 2
User_first_name: Tortor
User_sex: male


User_id: 1
User_first_name: Jeremiah
User_sex: male


Order by rand method:
Random record display is the last one we're going to discus, this method is the main reason for this tutorial. On daily basis we do see from websites like facebook, twitter and instagram display their news feed information randomly. What do I mean by randomly? Anytime you login your account, on the home page you always see that posts are display random in date and time. Sometimes you see old posts on top of the new ones, they fetch and echo the record using rand (0-10). Now we changed the desc order method to rand() order pattern. Check the three codes and their results.


<!DOCTYPE html>

<html>

<head>

<title>php information display method</title>

</head>
<body>
<h2>php information display</h2>
<?php
//Db connection
$mydb = new mysqli("localhost", "root", "", "storagedatabase");
//select all details from user_table
$select = "SELECT * from user_table ORDER by RAND() LIMIT 3";
$query = mysqli_query($mydb,$select);
//If the above select statement execute then do store the values in row variable
if($query){
while($row = mysqli_fetch_array($query)){
echo $row['user_id'];
echo "<br /><br />";
echo $row['user_first_name'];
echo "<br /><br />";
echo $row['user_sex'];
echo "<hr />";
}
}
?>
</body>
</html>

The output of the above code will be:

User_id: 2
User_first_name: Tortor
User_sex: male

User_id: 1
User_first_name: Jeremiah
User_sex: male

User_id: 3
User_first_name: Joy
User_sex: female

All the three are useful, it is up to you how you want to echo out records. In this tutorial, we assume that you already have basic knowledge on php mysqli.

comment any difficulty.