loops - php pagination and search -


i have while loop pagination have idea how make search don't know how insert here's code pagination , output commands working don't know how put code search , it's messing head.

//count total number of row in table*/ $count_query   = mysql_query("select count(personid) numrows persons"); $row     = mysql_fetch_array($count_query); $numrows = $row['numrows']; $total_pages = ceil($numrows/$per_page); $reload = 'index.php'; //main query fetch data  $query = mysql_query("select * persons order rand() limit $offset,$per_page"); //loop through fetched data while($result = mysql_fetch_array($query)){ $id = $result['personid']; echo "<div class= content > "; echo"<img height=100 width=100 src='upload/". $result['image'] ."'/>"; echo "<font color='black'>". $result['firstname']. "</font></br>"; echo "</div>"; 

so trial , error think part got error row here's whole code

> <?php include_once('includes/dbconnect.php'); >  >  > ?> >  > <?php >  > // can display results returned. first display > search form on top of page >  > $searchtext = $_post["q"]; >  >  >  >  >  > $action = (isset($_request['action'])&& $_request['action'] > !=null)?$_request['action']:''; >  > if($action == 'ajax'){ >  >   include 'pagination.php'; //include pagination file >  >  >   //pagination variables  $page = (isset($_request['page']) && > !empty($_request['page']))?$_request['page']:1;   $per_page = 5; //how > records want show     $adjacents  = 5; //gap between pages > after number of adjacents     $offset = ($page - 1) * $per_page; >  >   //count total number of row in table*/     $count_query   = > mysql_query("select count(personid) numrows persons");    $row   > = mysql_fetch_array($count_query);    $numrows = $row['numrows'];     $total_pages = ceil($numrows/$per_page);    $reload = 'index.php'; >  >                   //search >         // basic sql-injection protection >  >         $searchtext = $_request["q"];     //main query fetch data >         // query simple search criteria $query = mysql_query("select * persons firstname '%" >            . $searchtext . "%' order rand() limit $offset,$per_page"); >  >   //loop through fetched data >  >  >         while($result = mysql_fetch_array($query)){ >         $id = $result['personid']; >  >  >                                       echo "<div class= content > "; >  >                                       echo"<img height=100 width=100 src='upload/". $result['image'] ."'/>"; >                                       echo "<font color='black'>". $result['firstname']. "</font></br>"; >  >  >  >                                       echo "</div>"; >  >  > } echo paginate($reload, $page, $total_pages, $adjacents); } else{ ?> >  >  > <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>simple ajax > pagination php , mysql</title> <script type="text/javascript" > src="jquery-1.5.2.min.js"></script> <link media="screen" > href="style.css" type="text/css" rel="stylesheet"> <script > type="text/javascript">   $(document).ready(function(){       load(1);    }); >  >   function load(page){        $("#loader").fadein('slow');        $.ajax({ >           url:'index.php?action=ajax&page='+page,             success:function(data){ >               $(".outer_div").html(data).fadein('slow'); >               $("#loader").fadeout('slow');           }       }) >  >         } >  > </script> >  > </head> <body> >   >  >  > <div id="loader"><img src="loader.gif"></div> >  >  >  > <div class="outer_div"></div> >  >     <div class="content"><form action='' method='post'> <input type='text' name='q' /> <input type="button" onclick="history.go(0)" > value="refresh"/> </p> </form></div> </body> </html> <?php >  > }?> 

this output want
1: http://i.stack.imgur.com/l8mma.png
2: http://i.stack.imgur.com/p47ui.png

in case can add where clause pattern condition , receive desired effect quickly.

// basic sql-injection protection $searchtext = htmlspecialchars ($_post['searchtext']);  // query simple search criteria $query = mysql_query("select * persons firstname '%"             . $searchtext . "%' order rand() limit $offset,$per_page"); 

but approache have several disadvatages:

  1. your request slow (you see more data in db) because use order rand() construction sorts randomly all entries in db table , returns small amount specified in limit clause;
  2. it neseccary reload page every time want search something. if want implement dynamic update of search results list should use ajax queries javascript.

p.s.: try not use deprecated mysql_ functions, use pdo or mysqli indstead (they provide built-in sql-injection protection trough prepared statments).

update:

ok, using ajax.

so don't need form @ all. use 2 elements: text input , button.

html:

<input id="q" type='text' name='q' /> <input type="button" onclick="load(1)" value="refresh"/> 

javascript:

function load(page){     $("#loader").fadein('slow');     var searchtext = $('#q').val();     $.ajax({            url:     'index.php?action=ajax&page='+page+'&q='+searchtext,             success:  function(data){                $(".outer_div").html(data).fadein('slow');                $("#loader").fadeout('slow');                       }     }); } 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -