Hi Marion,
> I have a database table with like 2000 items in it.
Only? My latest project was automating the callcenter of a cellphone
(prepaid) company callcenter (reseller). Only the clients database is
already several tens of thousands of records. Not to mention the table in
which track is being kept of the protocols (ticket numbers).
But a nice exercise in making joins and doing that at last a bit efficient
as otherwise performance disappears...
Anyway,
> What I want to do is have like 25 per page showing with next previous etc.
Assuming you use mysql you will want to use " LIMIT $Start, $Showing" in the
end ("LIMIT $Showing OFFSET $Start" in PG).
What I do is create a session variable object, say $Data, containing
properties Start, Showing, Total, Query, with for example the following
content:
$Data->Query = "select * from yourtable";
$Data->Total = 13357;
$Data->Start = 150;
$Data->Showing = 50;
$Data->Data = array(contains Showing or less rows, being the data shown on
the actual page);
Actually still not using objects but arrays (see code example below, but
heck, what's the difference... don't mess with good code that alredy works
in several places ;)
Each next or previous page you just modify Start*, $Query =
$Data->Query."LIMIT ".$Data->Start.", ".$Data->Showing, unset and fill the
$Data->Data array again and display from this last array.
*of course you verify it doesn't go below 0 or over $Data->Total.
Now write some procedures to display the navigation and ready you are.
The first time a page is called you will need to give some parameters for
the query to be constructed, at every new page request you will just need to
give the page number and from there you calculate the new start and off you
go.
some functions I use:
function define_datacollection($Query, $DataCollection="Data", $Rows=0)
{
global $dbType;
if ((isset($_GET["P"]))&&(isset($_SESSION[$DataCollection]))) {
$_SESSION[$DataCollection]["Start"] =
abs(round($_GET["P"]))*$_SESSION[$DataCollection]["Rows"];
switch (strtolower($dbType)) { // extendable to other
DB-types
case 'postgresql':
$Query =
$_SESSION[$DataCollection]["Query"]." LIMIT
".$_SESSION[$DataCollection]["Rows"]." OFFSET
".$_SESSION[$DataCollection]["Start"];
break;
case 'mysql':
$Query =
$_SESSION[$DataCollection]["Query"]." LIMIT
".$_SESSION[$DataCollection]["Start"].",
".$_SESSION[$DataCollection]["Rows"];
break;
}
$_SESSION[$DataCollection]["Data"] = get_data($Query);
} else {
set_collectiondata($Query, $DataCollection, $Rows);
}
}
function set_collectiondata($Query, $DataCollection="Data", $Rows=0) {
global $MaxSearchResults, $dbType;
$_SESSION[$DataCollection]["Query"] = $Query;
$_SESSION[$DataCollection]["Results"] = get_resultcount($Query);
$_SESSION[$DataCollection]["Start"] = 0;
$_SESSION[$DataCollection]["Rows"] =
(($Rows==0)?$MaxSearchResults:$Rows);
switch (strtolower($dbType)) { // extendable to other DB-types
case 'postgresql':
$Query = $_SESSION[$DataCollection]["Query"]."
LIMIT
".$_SESSION[$DataCollection]["Rows"]." OFFSET
".$_SESSION[$DataCollection]["Start"];
break;
case 'mysql':
$Query = $_SESSION[$DataCollection]["Query"]."
LIMIT
".$_SESSION[$DataCollection]["Start"].",
".$_SESSION[$DataCollection]["Rows"];
break;
}
$_SESSION[$DataCollection]["Data"] = get_data($Query);
}
there are some more functions like get_data and get_resultcount but their
purpose should be pretty obvious.
PS: extra free tip: notice I use $MaxSearchResults instead of '25'.
Experience learns the customer always wants to change nagging little things
just after you finish coding. For not having to search your code for all
those values to change I just make a big config file and put all that stuff
in there. They want a change? Pronto, ten minutes later it's done. Sitewide.
Marc
Community email addresses:
Post message: [email protected]
Subscribe: [EMAIL PROTECTED]
Unsubscribe: [EMAIL PROTECTED]
List owner: [EMAIL PROTECTED]
Shortcut URL to this page:
http://groups.yahoo.com/group/php-list
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/php-list/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/php-list/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/