|
|
|
<?
// This is a script to walk through the results of a query to a postgresql
// database and display a set number of results per page rather than
// displaying them all on one page.
// Define the db connect variables, the queries and the results per page
// The example query is a table set up as:
//
// create table authors
// ( first text,
// second text);
//
// Also built in is a method for users to select how the results are sorted
// Written by Jim Tom Polk October 1999
// Comments, improvements, etc to jtpolk@camalott.com
// name the db connection variables
$host = "localhost";
$port = "5432";
$dbname = "predb";
// set up the queries, this could come from a form on another page
$sort_default = array(" SELECT * FROM authors ", "Natural Sort");
$sort_second = array(" SELECT * FROM authors ORDER BY second ", "Last Name Sort");
$sort_first = array(" SELECT * FROM authors ORDER BY first ", "First Name Sort");
// set up a default query if none is set
if (!IsSet($query) ) :
$query = $sort_default[0];
endif;
// allow users to select how the query is sorted
echo "<form action=\"$PHP_SELF\" method=\"POST\">\n";
echo "<table summary=\"Select Sort Table\">\n";
echo "<tr>\n";
echo "<td>\n";
echo "<select name=\"query\">\n";
// test values and put asterisk so users know the default sort
echo "<option value=\"$sort_default[0]\"";
if ($query == $sort_default[0])
{ echo " SELECTED > "; }
else
{ echo " >"; }
echo "$sort_default[1]</option>\n";
echo "<option value=\"$sort_second[0]\"";
if ($query == $sort_second[0])
{ echo " SELECTED > "; }
else
{ echo " >"; }
echo "$sort_second[1]</option>\n";
echo "<option value=\"$sort_first[0]\"";
if ($query == $sort_first[0])
{ echo " SELECTED > "; }
else
{ echo " >"; }
echo "$sort_first[1]</option>\n";
echo "</select>\n";
echo "</td>\n";
echo "<td>\n";
echo "<input type=\"SUBMIT\" value=\"GO\">\n";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</form>\n";
// set how many records will appear per page
$pagesize = 5;
// if first time in to run query, test and set start variable
if (!IsSet($start) ) :
$start = 0;
endif;
// connect to the database, variables defined at top
$conn = pg_Connect("$host", "$port", "", "", "$dbname");
if (!$conn)
{
echo "A Connect to DB Error Occured\n";
exit;
}
// run query on table in database
$result = pg_Exec ($conn, "$query ;");
if (!$result)
{
echo "An error occured.\n";
exit;
}
// find out which is less, query results or start plus pagesize
// assign that variable to last
$last = min($start + $pagesize, pg_NumRows($result));
// let people know where they are as they walk through table
echo "<table>\n";
echo "<caption>\n";
echo "$last of " . pg_NumRows($result);
echo "</caption>\n";
echo "<tr>\n";
echo "<td align=\"right\" width=\"100\">\n";
// if results of query less than the results to show per page
// then do not show back, forth and top buttons
if (pg_NumRows($result) <= $pagesize )
{ echo "\n"; }
else // begin show buttons
{
// if the value of last is not equal to pagesize then
// show backward button, one or the other will show
// along with the top button
if ($last != $pagesize)
{
echo "<form action=\"$PHP_SELF\" method=\"POST\">\n";
// set value for start to start minus the page size to show
$incback = ($start - $pagesize);
echo "<input type=\"HIDDEN\" name=\"start\" value=\"$incback\">\n";
echo "<input type=\"HIDDEN\" name=\"query\" value=\"$query\">\n";
echo "<input type=\"SUBMIT\" value=\"<< Backward\">\n";
echo "</form>\n";
}
echo "</td>\n";
// the top button will always show along with either
// the backward or forward button(s)
echo "<td align=\"center\">\n";
echo "<form action=\"$PHP_SELF\" method=\"POST\">\n";
echo "<input type=\"HIDDEN\" name=\"query\" value=\"$query\">\n";
echo "<input type=\"SUBMIT\" value=\"TOP\">\n";
echo "</form>\n";
echo "</td>\n";
echo "<td align=\"left\" width=\"100\">\n";
// if value of start plus page size is
// NOT greater than or equal to rows returned
// show forward button
if (!( ($start + $pagesize) >= pg_NumRows($result) ) )
{
echo "<form action=\"$PHP_SELF\" method=\"POST\">\n";
// set value for start to start plus the page size to show
// no errors will occur if the start is greater than the
// results available in showing results
$incfor = ($start + $pagesize);
echo "<input type=\"HIDDEN\" name=\"start\" value=\"$incfor\">\n";
echo "<input type=\"HIDDEN\" name=\"query\" value=\"$query\">\n";
echo "<input type=\"SUBMIT\" value=\"Forward >>\">\n";
echo "</form>\n";
}
} // end show buttons
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
// now show loop through results from query for items
while ($start < $last)
{
echo "<table border=\"1\">\n";
echo "<tr>\n";
echo "<td width=\"100\">\n";
echo pg_Result($result, $start, "first");
echo "</td><td width=\"100\">\n";
echo pg_Result($result, $start, "second");
echo "</td>\n";
// show number in human format i.e. zero is one
$startpos = $start + 1;
echo "<td align=\"right\" width=\"36\">\n$startpos</td></tr>\n";
echo "</table>\n";
$start++;
} // end display loop of items from query
// free results and then close connection to database
pg_FreeResult($result);
pg_Close($conn);
?> |
|
| PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL | | | Monthly and Daily Upcoming Events calendar. Categories : Date Time, PostgreSQL, PHP, Calendar, Databases | | | Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0. Categories : Databases, PHP, mSQL, Databases | | | AUTH (.htaccess style) - a login system that uses PostgreSQL. Categories : PHP, Authentication, Databases, PostgreSQL | | | DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser! Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | Save and restore files into postgresql database (PHP SCRIPT) PHP CLASS Categories : PHP, Databases, PostgreSQL, Filesystem | | | Is there any way to test that the $result has null values or not without reading the field values in the results in postgre?
Categories : PostgreSQL, PHP, Databases | | | This is a database wrapper for PostgreSQL, but can be simply modified for any other database type. Categories : Databases, PostgreSQL, PHP | | | Postgresql Database Backup And Restore PHP script Categories : PHP, Databases, PostgreSQL | | | This is a function to display a table for Postgres results. This this code in an include file, require it, and call ShowResults($result) whenever you need to see what your queries result in. Categories : Databases, PHP, PostgreSQL | | | This is Yet Another Sql Abstraction Library. Include it in your script and you can use the most important SQL functions without worrying about the SQL backend. Categories : Databases, PHP, ODBC, MySQL, PostgreSQL | | | A very simple postgreSQL guestbook script. Categories : PostgreSQL, Databases, PHP | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | |
|
|
|