Hey,
so I have tried for about 5 hours to figure this out but to no avail.
My table can currently sort the data from my databae but I cannot use pagination. I have created a pagination code and it works perfectly but combining the two has proven impossible.
Here are both codes:
Pagination code:
<html>
</html>
<?php
//Connecting to Banner DB
include('config.php') ;
//Preparing variables
if(!isset( $_GET['p']) ){$_GET['p'] =0;}
$per_page= 5;
$sql= "SELECT url FROM `banner_clicks`" ;
$sql2= "SELECT url FROM `banner_clicks` ORDER BY url ASC LIMIT ".$_GET['p'].",". $per_page;
$query= mysql_query ($sql2) ;
$rows= mysql_num_rows ( mysql_query ($sql)) ;
$pages= ceil($rows / $per_page);
while ($fetch=mysql_fetch_assoc($query) ){
echo'<p>'. $fetch['url'] . '</p>';
}
for ( $i=0; $i<$pages; $i++){
echo ' <a href="pagination.php?p=' . ($i * $per_page) . '">'.($i +1). '</a>';
}
And here is the Sort Code(connection to DB is included but not in the part of the co:
<?php
if ( isset ( $_GET['order_var'] ) )
{
$order_var = $_GET['order_var'];
switch ( $order_var )
{
case 'fn_asc':
$order_var = "url ASC";
break;
case 'fn_dsc':
$order_var = "url DESC";
break;
case 'ln_asc':
$order_var = "provby ASC";
break;
case 'ln_dsc':
$order_var = "provby DESC";
break;
case 'id_asc':
$order_var = "Description ASC";
break;
case 'id_dsc':
$order_var = "Description DESC";
break;
}
}
else
{
$order_var = "provby ASC"; //default setting
}
$query = "SELECT * FROM `banner_clicks` ORDER BY $order_var" or die('error getting table');
print $query;
$result = mysqli_query($dbcon, $query) or die('error getting table');
//here start the links
$id_var = "id_asc";
$fn_var = "fn_asc";
$ln_var = "ln_asc";
if ( $order_var == "Description ASC" )
{
$id_var = "id_dsc";
}
else if ( $order_var == "Description DESC" )
{
$id_var = "id_asc";
}
if ( $order_var == "url ASC" )
{
$fn_var = "fn_dsc";
}
else if ( $order_var == "url DESC" )
{
$fn_var = "fn_asc";
}
if ( $order_var == "provby ASC" )
{
$ln_var = "ln_dsc";
}
else if ( $order_var == "provby DESC" )
{
$ln_var = "ln_asc";
}
//table starts
print '
<table>
<tr>
<td><a href="zine.php?order_var=' . $id_var . '">ID</a></td>
<td><a href="zine.php?order_var=' . $fn_var . '">Links</a></td>
<td><a href="zine.php?order_var=' . $ln_var . '">Provided By</a></td>
</tr>';
while ( $row = mysqli_fetch_assoc ( $result ) ) //clicks are included in the ahref tag and are matched with the url
{
print '
<tr>
<td bgcolor="gray"><a href=click.php?id='.$row['id'].'">' . $row['url'] . '</td>
<td>' . $row['provby'] . '</td>
<td>' . $row['Description'] . '</td>
</tr>';
}
print '</table>';
?>
As you can see the variable order_var is used to order the table. I tried to change the following line
$query = "SELECT * FROM `banner_clicks` ORDER BY $order_var" or die('error getting table');
to
$query = "SELECT * FROM `banner_clicks` ORDER BY $order_var LIMIT $pages".$_GET['p'].",". $per_page;" or die('error getting table');
and adding the necessary lines my table could be ordered and was paginated but displayed no data…
any help is greatly appreciated, going to bed now will answer as soon as I get up!