Results 1 to 3 of 3

Thread: paging a search variable

  1. #1

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    paging a search variable

    hi,
    This is my 2nd post in this category

    I have paging script it is working fine so far, but dont know how do I use it togather with serch form to post a search variable so it can still paging the variable up to it should be in page $i, next and prev.

    attachment is a sql database, config.php search.php paging.php scripts

    Any help is great and very much appreciate

    ps. I will come back within 25 mins

    Rgds,
    Attached Files Attached Files

  2. #2

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: paging a search variable

    I have modify this script, but it return with search variable is empty:

    vb Code:
    1. <?php
    2. include 'config.php';
    3. $pg = $_GET[pg];
    4.  
    5. /*  page default 1 */
    6. if(!isset($_GET['pg'])){
    7.     $page = 1;
    8. } else {
    9.     $page = $_GET['pg'];
    10. }
    11.  
    12. /* item per page */
    13. $max_results = 5;
    14.  
    15. /* calc: 1 x 5 = 5  ,  5 - 5 = 0   , record start from 0*/
    16. $from = (($page * $max_results) - $max_results);  
    17.  
    18.  
    19.  
    20.  
    21.         $search=$_POST["search"];
    22.  
    23.         if ($search != "") {
    24.  
    25.  
    26. /* populate databse, LIMIT  0 to 5 */
    27. $sql = mysql_query("SELECT * FROM content ORDER BY id WHERE header LIKE '%$search%' LIMIT $from, $max_results ");
    28. //while($row = mysql_fetch_array($sql)){
    29.  
    30. /* display result,  */
    31.               echo "<table>";
    32.               while ($r=@mysql_fetch_array($sql)){
    33.                 echo ("
    34.                 <tr><td>Id: <b>$r[id]</b></td></tr>
    35.                 <tr><td>Word: $r[header]</td></tr>
    36.                 <tr><td>Meaning: $r[content]<hr></td></tr>");
    37.               }                
    38.               echo "</table>";
    39.              
    40.                
    41. ?>
    42.  
    43. <hr>
    44. <?php
    45.  
    46. $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM content"),0);
    47.  
    48. $total_pages = ceil($total_results / $max_results);
    49.  
    50. /*   hiperlink pages*/
    51. echo "<center>Select a Page<br />";
    52.  
    53. /*  Previous link */
    54. if($pg > 1){
    55.     $prev = ($page - 1);
    56.     echo "<a href=$_SERVER[PHP_SELF]?pg=$prev> <-Previous </a> ";
    57. }
    58.  
    59. for($i = 1; $i <= $total_pages; $i++){
    60.     if(($pg) == $i){
    61.         echo "$i ";
    62.         } else {
    63.             echo "<a href=$_SERVER[PHP_SELF]?pg=$i>$i</a> ";
    64.     }
    65. }
    66.  
    67. /*  Next link */
    68. if($pg < $total_pages){
    69.     $next = ($page + 1);
    70.     echo "<a href=$_SERVER[PHP_SELF]?pg=$next>Next-></a>";
    71. }
    72. echo "</center>";
    73.  
    74.  
    75.  
    76.  
    77. } else {
    78. echo "Search is empty, Key-in a word first!!";
    79. }
    80.  
    81.  
    82. ?>

    Regards,

  3. #3

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: paging a search variable

    the script above is a bit complicated, now this is a nother search script use content database, to search header by letter and display its content,;
    Code:
    <?php
    		$host="localhost";
    		$username="root";
    		$password="1234";
    		$db_name="test";
    		$tbl_name="content";
    
    		mysql_connect("$host","$username", "$password")
    		or die("Cannot connect");
    		mysql_select_db("$db_name")
    		or die("Cannot select database");
    
    		$search=$_POST["search"];
    
    		if ($search != "") {
    			$result = mysql_query("SELECT * FROM $tbl_name 
    					WHERE header LIKE '%$search%' ");
    			if (mysql_num_rows($result)>0) {
    			
    			 $numrows=mysql_num_rows($result);
    			  echo "<p>" .$numrows . " results found for " . stripslashes("<b>$search</b>") . "</p>";
    			  
    			  
    			  
    			  
    			  
    			  echo "<table>";
    			  while ($r = mysql_fetch_array($result)){
    				echo ("
    				<tr><td>Id: <b>$r[id]</b></td></tr>
    				<tr><td>header: $r[header]</td></tr>
    				<tr><td>content: $r[content]<hr></td></tr>");
    			  }						
    			  echo "</table>";
    			} else {
    			  echo "No result found on <b>$search</b> ";
    					
    			} 
    		}
    
    		if(!$search){
    			echo "Key-in a word for searching";
    		} 
    	
    	?>
    now Is there possible to do mysql_query that is searched by $result and requery into this script in order to make paging:
    Code:
    $pg = $_GET[pg];
    
    /*  page default 1 */
    if(!isset($_GET['pg'])){ 
        $page = 1; 
    } else { 
        $page = $_GET['pg']; 
    } 
    
    /* item per page is 5 */
    $max_results = 5; 			  
    /* calc: 1 x 5 = 5  ,  5 - 5 = 0   , record start from 0*/
    $from = (($page * $max_results) - $max_results);  
    
    /* populate databse, LIMIT  0 to 5 */
    $sql = mysql_query("SELECT * FROM $result ORDER BY id LIMIT $from, $max_results ");
    and change this line to:
    while ($r = mysql_fetch_array($sql)){

    any advice ?

    Regards,

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