Jump to content

Archived

This topic is now archived and is closed to further replies.

Billbonic

PHP Sorting

Recommended Posts

Hi guys,

I've only recently starting learning php and I've been trying to understand the following code from a tutorial(which unforutnatly doesn't decribe this section of code) due to my born ignorance on the subject I would really appreciate some help on demystifying the source below: i really haven't a clue how it works:

Thanks in advance for any replies - much appreciated.

-------------------------------------------------------
<?php
include_once("include/db_connection.php");

$initStartLimit = 0;
$limitPerPage = 10;

$startLimit = $_REQUEST['startLimit'];
$numberOfRows = $_REQUEST['rows'];
$sortBy = $_REQUEST['sortBy'];
$sortOrder = $_REQUEST['sortOrder'];

if ($startLimit=="")
{
$startLimit = $initStartLimit;
}

if ($numberOfRows=="")
{
$numberOfRows = $limitPerPage;
}

if ($sortOrder=="")
{
$sortOrder = "DESC";
}
if ($sortOrder == "DESC") { $newSortOrder = "ASC"; } else { $newSortOrder = "DESC"; }
$limitQuery = " LIMIT ".$startLimit.",".$numberOfRows;
$nextStartLimit = $startLimit + $limitPerPage;
$previousStartLimit = $startLimit - $limitPerPage;

if ($sortBy!="")
{
$orderByQuery = " ORDER BY ".$sortBy." ".$sortOrder;
}


$sql = "SELECT * FROM customers".$orderByQuery.$limitQuery;
echo $sql."<br>";

$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);


if ($numberOfRows==0) {
?>

Sorry. No records found !!

<?
}
else if ($numberOfRows>0) {

$i=0;
?>


<br>
<?
if ($_REQUEST['startLimit'] != "")
{
?>

<a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $previousStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Previous <? echo $limitPerPage; ?> Results</a>....
<? } ?>
<?
if ($numberOfRows == $limitPerPage)
{
?>
<a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $nextStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Next <? echo $limitPerPage; ?> Results</a>
<? } ?>

<br><br>
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="0" WIDTH="100%">
<TR><TD>
<a href="<? echo $PHP_SELF; ?>?sortBy=customer_id&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
<B>Customer_id</B>
</a>
</TD><TD>
<a href="<? echo $PHP_SELF; ?>?sortBy=fname&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
<B>Fname</B>
</a>
</TD><TD>
<a href="<? echo $PHP_SELF; ?>?sortBy=mname&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
<B>Mname</B>
</a>
</TD><TD>
<a href="<? echo $PHP_SELF; ?>?sortBy=lname&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
<B>Lname</B>
</a>
</TD><TD>
<a href="<? echo $PHP_SELF; ?>?sortBy=company&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
<B>Company</B>
</a>
</TD>
etc...... for the rest of the column names like
Address, City, State, Country etc...
-------------------------------------------------------

Share this post


Link to post
Share on other sites
welcome to the forum! i hope you find lots of help here to aide you on your quest for PHP knowledge. i'll take a stab at the code you provided, and i hope it will help you understand. now, i'm just going to add comments to the different sections and see if it helps:
[code]
<?php
// include your database connection first
include_once("include/db_connection.php");

// set the page controls to limit the number of records to show
$initStartLimit = 0;
$limitPerPage = 10;

// see if the user has specified a different limit than the default
$startLimit = $_REQUEST['startLimit'];
$numberOfRows = $_REQUEST['rows'];
$sortBy = $_REQUEST['sortBy'];
$sortOrder = $_REQUEST['sortOrder'];

if ($startLimit=="")
  $startLimit = $initStartLimit;

if ($numberOfRows=="")
  $numberOfRows = $limitPerPage;

if ($sortOrder=="")
  $sortOrder = "DESC";

// set your alternating ascending/descending links
if ($sortOrder == "DESC") { $newSortOrder = "ASC"; } else { $newSortOrder = "DESC"; }

// set your limit clause for your query based on values set above
// and set your limits for your prev and next links
$limitQuery = " LIMIT ".$startLimit.",".$numberOfRows;
$nextStartLimit = $startLimit + $limitPerPage;
$previousStartLimit = $startLimit - $limitPerPage;

// add order by clause if there is one selected
if ($sortBy!="")
  $orderByQuery = " ORDER BY ".$sortBy." ".$sortOrder;

// finalize your query itself and echo it out
$sql = "SELECT * FROM customers".$orderByQuery.$limitQuery;
echo $sql."<br>";

// run the query and see how many records are returned
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);

// the rest of the code is simply showing results
// based on how many records were returned
[/code]

now, i've got to say, this is pretty sloppy code in some places. it's not the easiest to try to learn from. if you're wanting to learn about pagination (which is the gist of this code), i'd recommend you read [a href=\"http://www.phpfreaks.com/tutorials/73/0.php\" target=\"_blank\"]this tutorial[/a] right here on PHPFreaks. it's much cleaner, well documented, and it's a WHOLE lot easier to understand.

hope this helpS!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.