Sorting MYSQL results Paging and column heads

Hi All,

I am working on a PHP page to display results from a MySQL db and I am havi=
ng a problem getting my results to page properly.

When the page loads, it looks fine, but hitting next/previous does not chan=
ge the data?

Any thoughts appreciated -

Edward
Ebrooathealthydirectionsdotcom



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Du=
tf-8" />
<title></title>
<link href=3D"demo.css" rel=3D"stylesheet" type=3D"text/css" />
</head>
<body>

<h1>
<?php
include_once 'config/constants.php';
include_once 'config/opendb.php';
// rows to return
$rowsPerPage =3D 20;
$pageNum =3D 1;
$yesterday =3D strftime ("%Y/%m/%d", strtotime("-2 day"));
$pretty =3D strftime ("%A %B %d");
$page =3D $_GET['page'];

print "<table style=3D'font-family: verdana; font-size: 8pt; color: 000066;=
' cellpadding=3D5 cellspacing=3D1 bgcolor=3DFFFFFF width=3D\"1009\" height=
=3D\"293\" border=3D\"0\">";
print " <tr>";
print "<td width=3D\"471\" height=3D\"287\"><h1><img src=3D\"http://microb=
e.healthydirections.com/images/hd_bnr_TLv2.gif\" width=3D\"471\" height=3D\=
"123\" /></h1>";
print "<font face=3Darial color=3D000066><h1>Bounce Report </font></h1>";
print "<font face=3Darial color=3D000066><a href=3D\"http://microbe.healthy=
directions.com/bounce\">Go to Adaptive Log</a>
";

print "
";
print "Showing results from $pretty
";
print "<br>";
print " <td width=3D\"522\">";
print "</td>";
print "</tr>";
print "</table>";
print "</h1>";

if(isset($_GET['page']))
{
$pageNum =3D $_GET['page'];
}

$offset =3D ($pageNum - 1) * $rowsPerPage;




db_connect_ecelerity(DBUSER, DBUSERPW);
$query1 =3D "select * FROM `bouncelog` ORDER BY `insert_date` DESC LIMIT $o=
ffset, $rowsPerPage";
$numresults=3Dmysql_query($query1);
$numrows=3Dmysql_num_rows($numresults);
$result =3D mysql_query($query1) or die('Error, lame query failed');

function makeHeaderLink($value, $key, $col, $dir) {
$out =3D "<a href=3D\"" . $_SERVER['SCRIPT_NAME'] .=
"?c=3D";
//set column query string value
switch($key) {
case "insert_date":
$out .=3D "1";
break;
case "message_id":
$out .=3D "2";
break;
case "log_type":
$out .=3D "3";
break;
case "to_local":
$out .=3D "4";
break;
case "to_domain":
$out .=3D "5";
break;
case "from_local":
$out .=3D "6";
break;
case "from_domain":
$out .=3D "7";
break;
case "binding_group":
$out .=3D "8";
break;
case "binding_name":
$out .=3D "9";
break;
case "bounce_phase":
$out .=3D "10";
break;
case "bounce_code":
$out .=3D "11";
break;
case "ip_address":
$out .=3D "12";
break;
case "bounce_message":
$out .=3D "13";
break;
default:
$out .=3D "1";
}

$out .=3D "&d=3D";

//reverse sort if the current column is clicked
if($key =3D=3D $col) {
switch($dir) {
case "ASC":
$out .=3D "1";
break;
default:
$out .=3D "0";
}
}
else {
//pass on current sort direction
switch($dir) {
case "ASC":
$out .=3D "0";
break;
default:
$out .=3D "1";
}
}

//complete link
$out .=3D "\">$value</a>";

return $out;
}

switch($_GET['c']) {
case "1":
$col =3D "insert_date";
break;
case "3":
$col =3D "message_id";
break;
case "3":
$col =3D "log_type";
break;
case "4":
$col =3D "to_local";
break;
case "5":
$col =3D "to_domain";
break;
case "6":
$col =3D "from_local";
break;
case "7":
$col =3D "from_domain";
break;
case "8":
$col =3D "binding_group";
break;
case "9":
$col =3D "binding_name";
break;
case "10":
$col =3D "binding_phase";
break;
case "11":
$col =3D "bounce_code";
break;
case "12":
$col =3D "ip_address";
break;
case "13":
$col =3D "bounce_message";
break;
default:
$col =3D "insert_date";
}

if($_GET['d'] =3D=3D "1") {
$dir =3D "DESC";
}
else {
$dir =3D "ASC";
}

if(!$link =3D mysql_connect("localhost", "root", "psss")) {
echo "Cannot connect to db server";
}
elseif(!mysql_select_db("ecelerity")) {
echo "Cannot select database";
}
else {
if(!$rs =3D mysql_query("SELECT * FROM bouncelog ORDER BY $col =
$dir LIMIT 500")) {
echo "Cannot parse query";
}
elseif(mysql_num_rows($rs) =3D=3D 0) {
echo "No records found";
}
else {

echo "<table class=3D\"bordered\" cellspacing=3D\"0\">\n";

echo "<tr>";
echo "<th>" . makeHeaderLink("Date", "insert_date", $col, $=
dir) . "</th>";
echo "<th>" . makeHeaderLink("Message ID", "message_id", $c=
ol, $dir) . "</th>";
echo "<th>" . makeHeaderLink("Log Type", "log_type", $col, =
$dir) . "</th>";
echo "<th>" . makeHeaderLink("To Local", "to_local", $col, =
$dir) . "</th>";
echo "<th>" . makeHeaderLink("To Domain", "to_domain", $col=
, $dir) . "</th>";
echo "<th>" . makeHeaderLink("From Local", "from_local", $c=
ol, $dir) . "</th>";
echo "<th>" . makeHeaderLink("From Domain", "from_domain", =
$col, $dir) . "</th>";
echo "<th>" . makeHeaderLink("Binding Group", "binding_grou=
p", $col, $dir) . "</th>";
echo "<th>" . makeHeaderLink("Binding Name", "binding_name"=
, $col, $dir) . "</th>";
echo "<th>" . makeHeaderLink("Bounce Phase", "bounce_phase"=
, $col, $dir) . "</th>";
echo "<th>" . makeHeaderLink("Bounce Code", "bounce_code", =
$col, $dir) . "</th>";
echo "<th>" . makeHeaderLink("IP Address", "ip_address", $c=
ol, $dir) . "</th>";
echo "<th>" . makeHeaderLink("Bounce Message", "bounce_mess=
age", $col, $dir) . "</th>";
echo "</tr>\n";

for($i =3D 0; $i < $numrows; $i++)
{
$row =3D mysql_fetch_array($rs); //get a row from our resul=
t set
$date2 =3D $row['insert_date'];
$date3 =3D date("F-d-Y", $date2);

$ip2 =3D $row['ip_address'];
$host =3D long2ip($ip2);

if(($i % 2) =3D=3D 0) {
echo "<TR bgcolor=3D\"#0099FF\">\n";
} else {
echo "<TR bgcolor=3D\"white\">\n";
}


echo "<td>$date3</td><td>".$row['message_id']."</td><td>".$row['log_type'].=
"</td><td>".$row['to_local']."</td><td>".$row['to_domain']."</td><td>".$row=
['from_local']."</td><
td>".$row['from_domain']."</td><td>".$row['binding_group']."</td><td>".$row=
['binding_name']."</td><td>".$row['bounce_phase']."</td><td>".$row['bounce_=
code']."</td><td>".$ro
w['ip_address']."</td><td>".$row['bounce_message']."</td>";
echo "</tr>\n";
}
echo "<br>";
}
}

$query =3D "SELECT COUNT(insert_date) AS numrows FROM bouncelog";
$result =3D mysql_query($query) or die('Error, query lamefailed');
$row =3D mysql_fetch_array($result, MYSQL_ASSOC);
$numrows =3D $row['numrows'];

// how many pages we have when using paging?
$maxPage =3D ceil($numrows/$rowsPerPage);

$self =3D $_SERVER['PHP_SELF'];

if ($pageNum > 1)
{
$page =3D $pageNum - 1;
$prev =3D " <a href=3D\"$self?page=3D$page\">[Prev]</a> ";

$first =3D " <a href=3D\"$self?page=3D1\">[First Page]</a> ";
}
else
{
$prev =3D ' [Prev] '; // we're on page one, don't enable 'previo=
us' link
$first =3D ' [First Page] '; // nor 'first page' link
}

if ($pageNum < $maxPage)
{
$page =3D $pageNum + 1;
$next =3D " <a href=3D\"$self?page=3D$page\">[Next]</a> ";

$last =3D " <a href=3D\"$self?page=3D$maxPage\">[Last Page]</a> ";
}
else
{
$next =3D ' [Next] '; // we're on the last page, don't enable 'nex=
t' link
$last =3D ' [Last Page] '; // nor 'last page' link
}

echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$=
maxPage</strong> pages " . $next . $last;

echo "</table>";
?>
</body>
</html>




--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Edward Brookhouse [ Mo, 05 Oktober 2009 20:57 ] [ ID #2018092 ]

Re: Sorting MYSQL results Paging and column heads

Edward Brookhouse wrote:
> Hi All,
>
> I am working on a PHP page to display results from a MySQL db and I am having a problem getting my results to page properly.
>
> When the page loads, it looks fine, but hitting next/previous does not change the data?

<snip>

> db_connect_ecelerity(DBUSER, DBUSERPW);
> $query1 = "select * FROM `bouncelog` ORDER BY `insert_date` DESC LIMIT $offset, $rowsPerPage";

Is it running this query or the next one?

> if(!$rs = mysql_query("SELECT * FROM bouncelog ORDER BY $col $dir LIMIT 500")) {
> echo "Cannot parse query";

There's only a limit here, no offset attached.

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
dmagick [ Di, 06 Oktober 2009 00:11 ] [ ID #2018259 ]
PHP » gmane.comp.php.database » Sorting MYSQL results Paging and column heads

Vorheriges Thema: Need help in triggers
Nächstes Thema: Re: PDO and PostgreSQL - RAISE NOTICE