Long articles in php page through the implementation code

  • 2020-05-26 07:54:20
  • OfStack

Welcome to exchange! The implementation code is as follows:
 
<?php include('include/config.php'); ?> 
<?php 
/** 
*Author Bird: heart 
* Implement long article pagination code  
* Principle:  
* using 1 An array to record each article 1 Page (using the p0 , p1 , p2... Make a manual tag) of the starting number of bytes and then pass through php The paged articles () function manipulates the array to display the paged articles. Page display, pass ptag (with tag The value of the 1 Sample) value.  
* Using the php Function:  
*1 , strlen(" string ") - Returns the length of the given string. -  Returns the total number of bytes in a string.  
*2 , strpos(" string "," The matching characters ") - Returns the numeric position of the first occurrence of needle in the haystack string. -  Returns the first occurrence in a string 1 The byte ordinal number of the matched characters.  
*3 , substr(" string "," The starting position "," Termination of the position ") - substr() returns the portion of string specified by the start and length parameters. -  Returns the number of characters in a string that specify the starting and ending position.  
*/ 
$sql = "select * from article where id = 41";// define sql Statement, return id for 41 The content of the  
$result = mysql_query($sql);// perform sql Statement that returns the result set  
$row = mysql_fetch_array($result);// Returns from the recordset as an array  
$content = $row['content'];// Assign the article to a variable $content 
$articleCounts = strlen($content);// return $content The total number of bytes  
$isTrue = true;// Circulating markers  
$tag = 0;// Paging tags, array subscripts  
echo " Total number of bytes: ".$articleCounts."<br>";// The test information  
// Look for signs." ptag "And assign its position (the number of bytes it is in) to the array array[]------------------------------------------ 
while($isTrue){ 
$startAt = strpos($content,"p".$tag);// Get the corresponding ptag The byte ordinal number of  
if($startAt != false){ // If there is a tag (the return value is not false ), then the location is recorded  
$array[$tag++] = $startAt; 
}else{ // If there is no tag, the array will be array[0] The assignment '\0' 
$array[$tag] = '\0'; 
$isTrue = false; 
} 
} 
// The loop outputs the marked location ------------------------------------------------------------- The test information  
for($i = 0; $i < $tag; $i++){ 
echo $array[$i]."<br>"; 
} 
echo "------------------------------ <br>"; 
// output --------------------------------------------------------------------- 
if($array[0] == '\0'){ // Determine if there is a mark  
echo $content; // Unmarked case, single page display  
}else{ // Marked cases are displayed in pagination  
// Output paging content  
if( isset($_GET['ptag']) ){ // To determine if there is ptag A value is passed, and a value is displayed  ptag+1  Page, otherwise displays no 1 Page ( ptag=0 )  
$ptag = $_GET['ptag']; // the ptag Is assigned to a variable $ptag 
if($ptag < $tag){ // Determine whether the parameter is incorrect  
echo " Have the value transfer , According to the first ".($ptag+1)." page <br>"; // The test information  
echo " Value is: ".$ptag."<br>"; // The test information  
echo substr($content,$array[$ptag - 1] + 2,$array[$ptag] - $array[$ptag - 1] - 2);// According to ptag+1 The content of the page  
}else{echo " Parameter is wrong ";} 
} 
else{ // There is no ptag The case of value passing, shows the value 1 Page ( ptag=0 )  
echo " No value transfer , According to the first 1 page <br>"; // The test information  
echo substr($content,0,$array[0] - 1);// According to the first 1 The content of the page  
} 
} 
// Loop displays page links ------------------------------------------------------------- 
if($array[0] != '\0'){ // Display page links only if there is a manual tag  
for($i = 0;$i < $tag;$i++){ 
if($ptag == $i){ // If it is this page, it is shown in bold  
$pager .= " <a href='test.php?ptag=$i'><b>".($i+1)."</b></a> "; 
}else{ // Not on this page  
$pager .= " <a href='test.php?ptag=$i'>".($i+1)."</a> "; 
} 
} 
echo "<br> Jump to the first ".$pager." page "; // The output link  
} 
?> 

Related articles: