Page 2 of 2

Re: How to sort search results in PHP or JS

Posted: Tue Oct 12, 2010 4:00 pm
by hm9
are you saying that if I have 200 records in the database and set the page size to show 20 per page, and when the page is displayed first time from the the database query, it will only show the first 20 on the page and then I ll be able to to use the options to disaply other records?

I know the page size bit works afterwards, but i m referring to the first time the page is displayed from the database query?

Re: How to sort search results in PHP or JS

Posted: Tue Oct 12, 2010 6:04 pm
by mikosiko
if you implement the functionality as per the documentation... yes... you should see the first 20 records and the buttons to paginate below that... is the way that works for me

Re: How to sort search results in PHP or JS

Posted: Wed Oct 13, 2010 9:45 am
by hm9
I am sorry but i am not sure if it works with a table derived from the database. It only works with static table as in the example shown. Is yours actually live records from a live database? do you use a form to submit a query and get the results on the table?

The documents do not show this.

this is the structure of the code:

Code: Select all

<head>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"> </script>
<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script>



<script type="text/javascript">
	$(function() {
		$("table")
			.tablesorter({widthFixed: true, widgets: ['zebra']})
			.tablesorterPager({container: $("#pager")});
	});
	</script> 
</head>
SQL Query that retrieves the records goes here

Code: Select all

<table class='tablesorter' cellspacing='0' cellpadding='0' border='1'> 

<thead> 
<th> row title</th>
</thead>
 <tbody>
</tbody>

<div id="pager" class="pager">
	<form>
		<img src="../addons/pager/icons/first.png" class="first"/>
		<img src="../addons/pager/icons/prev.png" class="prev"/>
		<input type="text" class="pagedisplay"/>
		<img src="../addons/pager/icons/next.png" class="next"/>
		<img src="../addons/pager/icons/last.png" class="last"/>
		<select class="pagesize">
			<option selected="selected"  value="3">3</option>
			<option value="20">20</option>
			<option value="30">30</option>
			<option  value="40">40</option>
		</select>
	</form>
</div> 

can you spot what is wrong?

Re: How to sort search results in PHP or JS

Posted: Wed Oct 13, 2010 1:56 pm
by John Cartwright
PHP execution is completed way before Javascript onload() event is triggered, therefore it doesn't matter if you render the table from live records, or have a static table.

Re: How to sort search results in PHP or JS

Posted: Wed Oct 13, 2010 2:22 pm
by mikosiko
Is yours actually live records from a live database?
Yes it is.... here is one of the codes that I'm using... you can use it as an example (modify the relevant data):

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
  <title>Tablesorter</title>
  <script type="text/javascript" src="path/to/jquery-latest.js"></script> 
  <script type="text/javascript" src="path/to/jquery.tablesorter.js"></script>
  <script type="text/javascript" src="path/to/addons/pager/jquery.tablesorter.pager.js"></script>

  <link rel="stylesheet" href="path/to/themes/blue/style.css" type="text/css"> 

  <script>
     $(function() { 
  	$("#myTable")
            .tablesorter({sortList:[[0,0]], widgets: ['zebra']})
            .tablesorterPager({container: $("#pager")}); 
	 }); 
  </script>
  
</head>
<body>
  <table id="myTable" class="tablesorter" border=1 width=50%> 
    <thead> 
        <tr>
           <th>LastName</th>
           <th>FirstName</th>
           <th>Sid</th>
           <th>LastName</th>
           <th>FirstName</th>
           <th>Dob</th>
        </tr> 
    </thead>

    <tbody>
        <?php
           // Define how to display/report errors
           ini_set("display_errors", "1");
           error_reporting(E_ALL);
                   
           // Define Constants for DB Access
           define('DB_HOST', 'yourhost');
           define('DB_USER', 'youruser');
           define('DB_PASSWORD', 'yourpassword');
           define('DB_DATABASE', 'yourdb');
                   
           // Create a new mysqli connection object
           $link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
        
           /* check connection */ 
           if(!mysqli_connect_errno()) {
               /*** if we are successful ***/
        
               /*** our SELECT query ***/
               $sql = "SELECT f.xlastname,f.xfirstname, t.tsid, t.tlastname, t.tfirstname,t.tdob
                         FROM tbl1 f JOIN tbl2 t ON (f.xlastname LIKE t.tlastname) LIMIT 400";
         
               /* Select queries return a resultset */
               if ($result = $link->query($sql)) {
        
        	        while($row = $result->fetch_assoc()) {
                           echo '<tr>';
        	              echo '<td>&nbsp;' . $row['xlastname'] . '</td>';
        	              echo '<td>&nbsp;' . $row['xfirstname'] . '</td>';
        	              echo '<td>&nbsp;' . $row['tsid'] . '</td>';
        	              echo '<td>&nbsp;' . $row['tlastname'] . '</td>';
        	              echo '<td>&nbsp;' . $row['tfirstname'] . '</td>';
        		      echo '<td>&nbsp;' . $row['tdob'] . '</td>';
        		   echo '</tr>'; 
        	         }
        	   
                  /* free result set */
                  $result->close();
               }
               $link->close();
           } else {
                /*** if we are unable to connect ***/
                echo 'Unable to connect';
                exit();
           }
        ?>
    </tbody>
  </table>
<!-- Pagination... -->
  <div id="pager" class="pager">
    	<form>
    		<img src="path/to/addons/pager/icons/first.png" class="first"/>
    		<img src="path/to/addons/pager/icons/prev.png" class="prev"/>
    		<input type="text" class="pagedisplay"/>
    		<img src="path/to/addons/pager/icons/next.png" class="next"/>
    		<img src="path/to/addons/pager/icons/last.png" class="last"/>
    		<select class="pagesize">
    			<option selected="selected"  value="10">10</option>
    			<option value="20">20</option>
    			<option value="30">30</option>
    			<option  value="40">40</option>
    		</select>
    	</form>
   </div>
  
</body>
</html>
hope this help

Re: How to sort search results in PHP or JS

Posted: Fri Oct 15, 2010 3:12 am
by hm9
Ok that has worked thanks. I got the order wrong initially

one thing I noticed is that when the page is loaded first time, it displays 10 records maximum despite setting the selected option to 5 only: <option selected="selected" value="5">5</option>
Does this occur in your case?

is that value 10 set as default elsewhere in the script?if so where?