I'm currently having an issue with the pagination script that I have in place. When a search is made for a particular item containing an "apostrophe", the results are rendered correctly. However, the pagination is not corresponding correctly. An error is produced when attempting to move on to pages extending past "page 1".

example error:

Sorry, there are no matching result for Arnie\. (should be rendered as "Arnie's")

So, obviously, the pagination that I currently have in place isn't handling special characters correctly. Any suggestions on how to correct this would be amazing. Thank you!

<?php

$button = $_GET ['submit'];
$search = $_GET ['search']; 

if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("");
mysql_select_db("");


$search = htmlspecialchars($search); 


$search = mysql_real_escape_string($search);



$search_exploded = explode (" ", $search);

$x = "";
$construct = "";  

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="Name LIKE '%$search_each%'";
else
$construct .="AND Name LIKE '%$search_each%'";

}


$constructs ="SELECT * FROM broadway WHERE $construct";
$run = mysql_query($constructs);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{ 

echo "$foundnum results found !<p>";

$per_page = 25;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0; 
$getquery = mysql_query("SELECT * FROM broadway WHERE $construct LIMIT $start, $per_page");

    echo "<table data-toggle='table' data-sort-name='name' data-sort-order='desc'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th data-sortable='true'>Date</th><th data-field='Name' data-align='left' data-sortable='true'>Name</th><th>City</th><th>Description</th>";
    echo "</tr>";
    echo "</thead>";

    while($runrows = mysql_fetch_assoc($getquery)) {
        echo "<tr>";
        echo "<td>{$runrows['Name']}</td>";
        echo "<td>{$runrows['PCITY']}</td>";
        echo "<td>{$runrows['TYPE']}</td>";
        echo '<td><a href="results.php?nameID=' .$runrows['ID'].'">' .$runrows['TOTAL_VIOLATIONS'].'</a></td>';
        echo "</tr>";

    }

    echo "</table>";


//Pagination Starts


$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $max_pages - 1;

if($max_pages > 1)
{   
//previous button
if (!($start<=0)) 
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    

//pages 
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;   
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;                 
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))        
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
} 
$i = $i + $per_page;                                       
}

}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                 
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}

}
//close to end; only hide early pages
else
{
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='pagination' href='search2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
} 
$i = $i + $per_page;              
}
}
}

//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='search2.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
}  



} 
} 
?>

</body>
</html>
} 
?>

I've also aware that mysql extension has been deprecated, so converting to PDO might be best option here?

Member Avatar for diafol

PDO may prevent all of that horrible deprecated code you're using. Yuck. I think this has been mentioned before.
Example of what you could do...

$searchword = $_GET['searchword']; //or from POST - but should be GET

$rowsPerPage = 20; //or whatever

//get count of records first and sort out $pagenum from URL before the following
//example SQL: SELECT COUNT(*) FROM table WHERE word = ?

$start = ($pagenum-1) * $rowsPerPage;

//create PDO object first before this
$stmt = $db->prepare("SELECT ... FROM table WHERE word = ? LIMIT ?,?);

$stmt->execute(array($searchword,$start,$rowsPerPage));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

ah, gotcha! I grabbed the code from a source and had a feeling I would run into some obstacles. I appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.