This tutorial enables you to display two records when user clicks on read more link using object-oriented funtionality.

In my example, I have made a folder named read-more-php and placed these pages index.php, config.php, connection.php and style.css.

Further set $get_limit variable to travel in the url through the read more link which increments by 2 each time when user clicks on the link and store its value in $limit variable which is passed to sql query to limits the records while displaying it on page.


 My SQL Code Segment:

To create database and table, execute following codes in your MySQL .



CREATE DATABASE store;
CREATE TABLE link(
id int(10) NOT NULL AUTO_INCREMENT,
comments varchar(255) NOT NULL,
PRIMARY KEY (id)
)


Set Connection Variables in “config.php” File:



<?php
// Set Connection Variable //
$SERVER_NAME='localhost';
$SERVER_USERNAME='root';
$SERVER_PASSWORD='';
$DATABASE_NAME='store';

?>


To Set Limit Variable To Display Only 2 Records:


$get_limit= $_GET['get_limit'];
$limit=$get_limit+2;


 Class “connection.php” Having Connection String And Query:



<?php

class Connection{

//connection start
public function __construct(){
require('config.php');
$this->conn=new mysqli($SERVER_NAME,$SERVER_USERNAME,$SERVER_PASSWORD,$DATABASE_NAME);
}

// Query To Fetch Records From Link Table On The Basis Of $limit Value //
public function select_query($limit){
$sql = mysqli_query($this->conn,"SELECT * FROM link LIMIT ".$limit);
return $sql;
}

//connection close
public function __destruct(){

mysqli_close($this->conn);
}
}
$obj= new Connection();
?>


                Watch out live demo or download the given codes to use it.

read-more-php


PHP File: index.php

Given below code creates an HTML page with 2 records initially as $limit value is 2 and read more link.

My connection is set in “config.php” file and query is placed in “connection.php” .



<?php
// To Hide Notice Error //
error_reporting(~E_NOTICE);

// Include Connection File //
include('connection.php');
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<div id="login">
<table>
<tr>
<th><h2>Click To Read More..</h2><hr/><th>
</tr>
<?php
// Set $limit Variable To Display Only 2 Records //
$get_limit = $_GET['get_limit'];
$limit = $get_limit + 2;

//Select Query Call //
$a = $obj->select_query($limit) or die($a . "<br/><br/>" . mysqli_error());
// Display Records //
while ($row = mysqli_fetch_array($a)) {
?>
<tr>
<td><?php echo $row['comments']; ?></td>
</tr>
<?php
}
?>
</table>
<br/>
<a href="/read-more-php/index.php?get_limit=<?php echo $limit; ?> ">Read More..</a>
</div>
</div>
</body>
</html>


 CSS File: style.css

Styling HTML elements.



@import http://fonts.googleapis.com/css?family=Raleway;
/*----------------------------------------------
CSS Settings For HTML Div ExactCenter
------------------------------------------------*/
body{
background-color:#D8C092;
margin:100px,50px;
padding:100px,50px}
#main {
width:960px;
margin:100px auto;
margin-left:600px;
font-family:raleway;
background-color:#FEFFED
}
h2 {
background-color:#FEFFED;
text-align:center;
border-radius:10px 10px 0 0;
margin:-10px -40px;
padding:15px
}
hr {
border:0;
border-bottom:1px solid #ccc;
margin:10px -47px;
margin-bottom:30px
}
#login {
width:259px;
float:left;
border-radius:10px;
font-family:raleway;
border:2px solid #ccc;
padding:10px 40px 25px;
margin-top:70px;
background-color:#FEFFED
}
a {
text-decoration:none;
color:#6495ed
}
#read{
background-color: #FFBC00;
}


Conclusion:

It becomes easy to use read more functionality in page using PHP by avoiding complex coding with javascript and ajax. Hope you liked it, keep reading our blogs.