Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Php Search & pagination

    I am creating a search feature in my downloads section, and I was wanting to create a search script.The search works, and I can now look through the database for files, but I was wanting to add pagination as well.It appears to work, but then as soon as I click "page 2", it says it can't find any results.It also does not seperate the results, still leaving the total amount on the first page.Could someone please tell me what I'm doing wrong?


    NOTE: I know this is deprecated, I'm just actually trying to get it ALL fully functional before I try to make it more secure.

    Here is the full code:


    Code:
    <center>
    <?php
    require_once('submit/db/db_connection.php');
    $search = (isset($_POST['search'])) ? ($_POST['search']) : '' ;
    define('PUN_ROOT', dirname(__FILE__).'/../');
    include PUN_ROOT.'include/common.php';
    require_once('submit/db/db_connection.php');
    $search = $_POST['search']; //This grabs everything your user has put into the search field (which has been labelled as "search" earlier from the .HTML page).
    $conn = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
     $selected = mysql_select_db ("sh2_files") 
    or die ("Could not select database"); 
    // PHP Search Script
    $sql = "SELECT * FROM downloads WHERE name LIKE '".$search."'";
     $result = mysql_query($sql,$conn)or die (mysql_error()); 
    $num_rows = mysql_num_rows($result);
    ?>
    <?php 
    $page=1;//Default page
     $limit=5;//Records per page
     $start=0;//starts displaying records from 0
     if(isset($_GET['page']) && $_GET['page']!=''){
     $page=$_GET['page'];
     }
     $start=($page-1)*$limit;
     ?> 
    <?php 
    function pagination($per_page = 10, $page = 1, $url = '', $total){ 
    
    $adjacents = "2";
     
    $page = ($page == 0 ? 1 : $page); 
    $start = ($page - 1) * $per_page; 
    
    $prev = $page - 1; 
    $next = $page + 1;
     $lastpage = ceil($total/$per_page);
     $lpm1 = $lastpage - 1;
     
    $pagination = "";
     if($lastpage > 1)
     { 
    $pagination .= "<ul class='pagination'>";
     $pagination .= "<li class='details'>Page $page of $lastpage</li>";
     if ($lastpage < 7 + ($adjacents * 2))
     { 
    for ($counter = 1; $counter <= $lastpage; $counter++)
     {
     if ($counter == $page)
     $pagination.= "<li><a class='current'>$counter</a></li>";
     else
     $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
    }
     }
     elseif($lastpage > 5 + ($adjacents * 2))
     {
     if($page < 1 + ($adjacents * 2)) 
    {
     for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
     {
     if ($counter == $page)
     $pagination.= "<li><a class='current'>$counter</a></li>";
     else
     $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
    }
     $pagination.= "<li class='dot'>...</li>";
     $pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
     $pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>"; 
    }
     elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
     {
     $pagination.= "<li><a href='{$url}1'>1</a></li>";
     $pagination.= "<li><a href='{$url}2'>2</a></li>";
     $pagination.= "<li class='dot'>...</li>";
     for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
     {
     if ($counter == $page)
     $pagination.= "<li><a class='current'>$counter</a></li>";
     else
     $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
    }
     $pagination.= "<li class='dot'>..</li>";
     $pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
     $pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>"; 
    }
     else
     {
     $pagination.= "<li><a href='{$url}1'>1</a></li>";
     $pagination.= "<li><a href='{$url}2'>2</a></li>";
     $pagination.= "<li class='dot'>..</li>";
     for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
     {
     if ($counter == $page)
     $pagination.= "<li><a class='current'>$counter</a></li>";
     else
     $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
    }
     }
     }
     
    if ($page < $counter - 1){
     $pagination.= "<li><a href='{$url}$next'>Next</a></li>";
     // $pagination.= "<li><a href='{$url}$lastpage'>Last</a></li>";
     }else{
     //$pagination.= "<li><a class='current'>Next</a></li>";
     // $pagination.= "<li><a class='current'>Last</a></li>";
     }
     $pagination.= "</ul>\n"; 
    } 
    return $pagination;
     } 
    
    ?> 
    <style type="text/css">
     ul.pagination { font-family: "Arial", "Helvetica", sans-serif;
     font-size: 13px;
     height: 100%;
     list-style-type: none;
     margin: 20px 0;
     overflow: hidden;
     padding: 0; }
     ul.pagination li.details { background-color: white;
     border-color: #C8D5E0;
     border-image: none;
     border-style: solid;
     border-width: 1px 1px 2px;
     color: #1E598E;
     font-weight: bold;
     padding: 8px 10px;
     text-decoration: none; }
     ul.pagination li.dot { padding: 3px 0; }
     ul.pagination li { float: left;
     list-style-type: none;
     margin: 0 3px 0 0; }
     ul.pagination li:first-child { margin-left: 0; }
     ul.pagination li a { color: black;
     display: block;
     padding: 7px 10px;
     text-decoration: none; }
     ul.pagination li a img { border: medium none; }
     ul.pagination li a.current { background-color: white;
     border-radius: 0 0 0 0;
     color: #333333; }
     ul.pagination li a.current:hover { background-color: white; }
     ul.pagination li a:hover { background-color: #C8D5E0; }
     ul.pagination li a { background-color: #F6F6F6;
     border-color: #C8D5E0;
     border-image: none;
     border-style: solid;
     border-width: 1px 1px 2px;
     color: #1E598E;
     display: block;
     font-weight: bold;
     padding: 8px 10px;
     text-decoration: none; }
     </style> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
    <title>Download Central :: Stronghold 2 Nation</title>
    <link href="stylesheet.css" rel="stylesheet" type="text/css" />
    <link href="gb_styles.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    
    <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
    <div id="wrapper">
    
      <a href="http://stronghold2.strongholdnation.co.uk"><div id="header">
       <div id="topmenu">
        <ul>
    
        </ul>
       </div>
      </div>
      </a>
    	<div id="content">
    
    		<div id="posts">
    			<div class="post">
    				<p>
    				</p>
    				<br /><br />
    
    
    <h2 class="title">Search Results</h2><br />
    <?php if (mysql_num_rows($result)==0){ 
    echo "No files have been found matching the query <strong><b>".$search."</b></strong>.";
    }else{
    echo "Your search query has generated <b><strong>".$num_rows."</b></strong> results for the query <b>".$search."</b>.<br /><br />These results are shown below.To search for additional features as well, please click <a href='ad_search.php'>here</a>.<br /><br />"; 
    while ($row = mysql_fetch_array($result)){
    
    			         
      echo     '<div class="file_box"><br />';
      echo       '<div class="file_info">';
               echo "• <span class='text_bold'>File</span>: " .$row['name']."<br /><br />";
    		   echo "• <span class='text_bold'>Category</span>: " .$row['category']."<br /><br />";
               echo "• <span class='text_bold'>Author</span>: " .$row['username']."<br /><br />";
               echo "• <span class='text_bold'>Size</span>:  " .$row['size']." MB<br /><br /> ";        
             echo "</div>
           </div>";
          echo ' <div class="options">';
             echo "
           </div><br /><br />
             <div id='page_numbers'>
             <a href='viewfile.php?fileid=" .$row['id']."'><img class='button_image' src='images/buttons/view.gif' title='View File' alt='View This File' /></a><p><a href='download.php?fileid=" .$row['file']."'><img class='button_image' src='images/buttons/download.gif' title='Download' alt='Download' /></a></p>
           </div>"; ?>
     <?php } } 
    
    mysql_close();?>      
           
           
             <br /><br />
             <div id="page_numbers">
             <p><?php if (mysql_num_rows($result)==0){ 
    		 echo "0";
    		 }
    		 ?></p>
    		 <?php
     echo pagination($limit,$page,'search.php?page=',$num_rows); //call function to show pagination
     ?>
           </div>
    
    
    					
           
    				<p>&nbsp;</p>
    			</div>
    		</div>
    
    		    <div id="form_area" class="boxed">
    			<h2 class="heading">Quick Search</h2>
    			<div class="content">
    				<form method="post" action="search.php">
    					<div>
    			<label>Filename:<br /></label>
                <input type="text" class="textfield" name="search" value="" size="18" /><br /><br />
    						<input class="button" type="submit" value="Search" title="Search" /><br /><br />
    						&bull; <a href="ad_search.php">Advanced Search</a>
    					</div>
    				</form>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,995
    Thanks
    15
    Thanked 233 Times in 233 Posts
    Put stuff in $_SESSION if you want to keep it during a site visit. You can change it at any time, simply by re-declaring it.
    PHP Code:
    $_SESSION['search_results']=array();
    for 
    loop here.
    {
    $_SESSION['search_results'][$i]=$result[$i];

    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    $search $_POST['search']; //This grabs everything your user has put into the search field (which has been labelled as "search" earlier from the .HTML page).
    //. . .
    $sql "SELECT * FROM downloads WHERE name LIKE '".$search."'";
    //. . .
     
    echo pagination($limit,$page,'search.php?page=',$num_rows); //call function to show pagination 
    $search is POST. pagination() will work via GET to pass an anchor. You cannot find a GET within a POST.
    Change your POST to GET and update the search form to use the GET.

    As mentioned, sessions are an alternative as well. You don't need anything in the loop; a simple session for the SEARCH criteria would be fine, and simply getting the page is suitable. The pagination function looks pretty bulky as well, you can probably cut that down by a lot by simply evaluating where you sit relative to everything else around it versus in chunks like you have currently. The signature is also incorrect; you have specified up to three arguments as optional, but the fourth is required. There is no (conventional) way to provide nothing to the first three (and allowing it to retain the default value), so you'll need to either make them all optional, all required, or rearrange it so that all required parameters are before the optional ones.

    You'll also want to look into SQL injection or converting this to using prepared statements as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So how would I get the search info from the previous page into the URL for the $_GET then?


    And would sessions be just as good as, or are they deprecated as well?

    I will transfer it to PDO, but I want to make sure that it works before I do that.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Sessions would ultimately be easier to modify to. Since all you'd have to do is run a check on the $_POST['search'], and if set you populate a $_SESSION['search']. You then check in an independent branch for if $_SESSION['search'] is set, and use that as your search criteria.
    Personally I prefer to limit the amount of space used by connected clients on my hdd, so I would simply add the search to the GET in the pagination links, and use the GET to pass it around from both the form and the links.

    Also, if you plan on paginating, you probably make use of a LIMIT in your SQL. Since there is no standard for LIMIT in SQL, using PDO would require you to write a method to deal with the LIMIT at an abstract level that is swappable based on the PDO driver type. Without it, you effectively defeat the purpose of the PDO being an abstraction layer, and you may as well use the native drivers in the mysqli library instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,044
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Another good reason to use $_GET for a search term is so that someone can bookmark a link and return to the same result using that link.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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