Page 1 of 1

Create page break or Read More... Button in your page Rate Topic: -----

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 31 October 2009 - 04:27 AM

Create page break or readmore tag with php

Hi guys,
Sometimes you need to show a few lines in your index page from one or more than one pages like About Us, Vision & Mission and etc… and create a More… button for clicking and showing all data.
In this tutorial I am going to show, how to create page break or read more button in your pages. It is very simple all you have to do is just create a tag </break> where you want to create read more buttons. And It’s Standard ASCII Code is </break> and we are going to use these codes in our PHP script.
Ok let’s start our tutorials
First we need to create our tables
pages table
CREATE TABLE `aba_db`.`pages` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Title` VARCHAR( 100 ) NOT NULL ,
`Data` LONGTEXT NOT NULL ,
`Status` INT NOT NULL 
) ENGINE = MYISAM ;

After that insert some data in table, and try to use </break> in you code example:

Fernando was taken unconscious to a hospital, where he died four days later. Pathologists said he had 30 bruises and brain hemorrhaging, signs of a fatal beating. Olivas did not testify at his trial but denied guilt and continues to maintain his innocence, defense lawyer Peter Gold said Friday.</break>
In a 3-0 ruling Thursday, the First District Court of Appeal rejected defense claims that the prosecutor had discriminatory motives in removing four Latinos from the jury panel.
The court cited the trial judge's findings that the prosecutor had offered acceptable race-neutral reasons. For example, the court said, one juror was 20 years old, another indicated a willingness to question medical testimony, and a third had a daughter who was a social worker, which might lead the juror to have liberal views on crime.


Full code:
<?PHP
// include connection page
include("config.php");
// select data from pages table in database
$str = "SELECT * FROM pages";
if(isset($_GET['id'])){
	$id = $_GET['id'];
	$str.=" WHERE ID=$id";
}
$pages_query = $mysqli->query($str)or die(mysqli_error($mysqli));

// fetch all data in an array
$pages_fetch = $pages_query->fetch_assoc();

// assign page data to $data variable
$data = $pages_fetch['Data'];
$separator = "</break>";
if(!isset($_GET['id'])){
	$data = explode($separator,$data);
	echo $data[0];
	echo "<br /><a href='pages.php?id=".$pages_fetch['ID']."'>More...</a>";
}else{
echo str_replace($separator,"",$data);
}
?>
And then print data from table:
<?PHP
// include connection page
include("config.php");
// select data from pages table in database
$pages_query = $mysqli->query("SELECT * FROM pages")or die(mysqli_error($mysqli));
// fetch all data in an array
$pages_fetch = $pages_query->fetch_assoc();
// assign page data to $data variable
$data = $pages_fetch['Data'];
Split page contents with </break> separator
$data = explode("</break>",$data);
// print data before </break> tag
echo $data[0];

// print readmore… link in page
echo <a href='pages.php?id=".$pages_fetch['ID']."'>More...</a>";
?>


Code Explanations:
include("config.php"); config.php contain database information.
$str = "SELECT * FROM pages"; String query for getting all data from table
if(isset($_GET['id'])){
$id = $_GET['id'];
$str.=" WHERE ID=$id";
}
This piece of code is use when someone clicked on read more button, and its filtering query with id of page and Concatenate some data with $str variable.
$pages_query = $mysqli->query($str)or die(mysqli_error($mysqli)); execute query $str variable.
$pages_fetch = $pages_query->fetch_assoc(); fetching query into $pages_fetch variable or getting all data from query and assigning it in $pages_fetch variable.
$data = $pages_fetch['Data']; assign page data into $data variable.
$separator = "</break>"; ASCII code of</break> and it is for separating the paragraph.
if(!isset($_GET['id'])){
$data = explode($separator,$data);
echo $data[0];
echo "<br /><a href='pages.php?id=".$pages_fetch['ID']."'>More...</a>";
}else{
when page load for the first time or no one clicked on More… button then this code will execute, $data = explode($separator,$data); split or separate paragraph into array
echo $data[0]; science it is an array and we are showing data from
echo str_replace($separator,"",$data); remove </break> when showing full data.
Thanks for reading :)

Attached File(s)

  • Attached File  Docs.pdf (18.44K)
    Number of downloads: 710


Is This A Good Question/Topic? 1
  • +

Replies To: Create page break or Read More... Button in your page

#2 LeVampirE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-January 10

Posted 17 January 2010 - 09:32 PM

thakns a lot man, great tutorial i like your tutorials, :^: :^: nothing to say just thanks & great job, keep posting ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1