pagination doesn't handle special characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mgreiner2883
Forum Newbie
Posts: 1
Joined: Wed Apr 15, 2015 8:25 am

pagination doesn't handle special characters

Post by mgreiner2883 »

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.

Code: Select all

<?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>
} 
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pagination doesn't handle special characters

Post by Celauran »

Post Reply