PHP - Simple Display Random Content

Submitted by: 
Language: 
Visitors have accessed this post 198 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Simple Display Random Content</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-2"></div>
  20. <div class="col-md-8">
  21. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add content</button>
  22. <br /><br />
  23. <div style="border:1px solid #000; padding:20px;"><?php include'random.php'?></div>
  24. </div>
  25. </div>
  26. <div class="modal fade" aria-hidden="true" id="form_modal">
  27. <div class="modal-dialog">
  28. <div class="modal-content">
  29. <form method="POST" action="add_content.php">
  30. <div class="modal-header">
  31. <h3 class="modal-title">Add Content</h3>
  32. </div>
  33. <div class="modal-body">
  34. <div class="col-md-2"></div>
  35. <div class="col-md-8">
  36. <div class="form-group">
  37. <label>Content</label>
  38. <textarea class="form-control" name="content" style="resize:none; height:300px;"></textarea>
  39. </div>
  40. </div>
  41. </div>
  42. <br style="clear:both;"/>
  43. <div class="modal-footer">
  44. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  45. <button name="save" class="btn btn-primary" ><span class="glyphicon glyphicon-save"></span> Save</button>
  46. </div>
  47. </form>
  48. </div>
  49. </div>
  50. </div>
  51. <script src="js/jquery-3.2.1.min.js"></script>
  52. <script src="js/bootstrap.js"></script>
  53. </body>
  54. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.