PHP - Simple Display Random Content

In this tutorial we will create a Simple Display Random Content using PHP. This code can display a random content in the webpage with the use of MySQLi query. The code use MySQLi Select query to display a random content by adding a RAND() parameter in the ORDER BY statement. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_content, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn=mysqli_connect("localhost", "root", "", "db_content");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Simple Display Random Content</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-2"></div>
  19.                 <div class="col-md-8">
  20.                         <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add content</button>
  21.                         <br /><br />
  22.                         <div style="border:1px solid #000; padding:20px;"><?php include'random.php'?></div>
  23.                 </div>
  24.         </div>
  25. <div class="modal fade" aria-hidden="true" id="form_modal">
  26.         <div class="modal-dialog">
  27.                 <div class="modal-content">
  28.                         <form method="POST" action="add_content.php">
  29.                                 <div class="modal-header">
  30.                                         <h3 class="modal-title">Add Content</h3>
  31.                                 </div>
  32.                                 <div class="modal-body">
  33.                                         <div class="col-md-2"></div>
  34.                                         <div class="col-md-8">
  35.                                                 <div class="form-group">
  36.                                                         <label>Content</label>
  37.                                                         <textarea class="form-control" name="content" style="resize:none; height:300px;"></textarea>
  38.                                                 </div>
  39.                                         </div>
  40.                                 </div>
  41.                                 <br style="clear:both;"/>
  42.                                 <div class="modal-footer">
  43.                                         <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  44.                                         <button name="save" class="btn btn-primary" ><span class="glyphicon glyphicon-save"></span> Save</button>
  45.                                 </div>
  46.                         </form>
  47.                 </div>
  48.         </div>
  49. </div> 
  50. <script src="js/jquery-3.2.1.min.js"></script> 
  51. <script src="js/bootstrap.js"></script>
  52. </body>
  53. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the html content to the database server. To do that just copy and write this block of codes inside the text editor, then save it as add_content.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $content=addslashes($_POST['content']);
  6.                
  7.                 mysqli_query($conn, "INSERT INTO `content` VALUES('', '$content')") or die(mysqli_error());
  8.                 header("location: index.php");
  9.         }
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will display a random content when the web application has launch. To make this just copy and write these block of codes below inside the text editor, then save it as random.php
  1. <?php
  2.         require'conn.php';
  3.        
  4.         $query=mysqli_query($conn, "SELECT * FROM `content` ORDER BY rand()") or die(mysqli_error());
  5.         $fetch=mysqli_fetch_array($query);
  6.         echo $fetch['content'];
  7. ?>
There you have it we successfully created Simple Display Random Content using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment