CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2015
    Posts
    1

    pagination doesn't handle special characters

    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.

    HTML Code:
    <?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>
    } 
    ?>

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: pagination doesn't handle special characters

    Sounds like you have magic quotes turned on in your PHP config. Basically, posted forms need to escape out apostrophes in case of SQL injection into databases. Magic quotes handles it automatically instead of relying on the user for proper coding.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured