first commit
This commit is contained in:
90
tutorial.php
Normal file
90
tutorial.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
require_once('database.php'); //connect to your database in this file
|
||||
define("NUMBER_PER_PAGE", 5); //number of records per page of the search results
|
||||
|
||||
/****
|
||||
* Purpose: paginate a result set
|
||||
* Precondition: current page, total records, extra variables to pass in the page string
|
||||
* Postcondition: pagination is displayed
|
||||
****/
|
||||
function pagination($current_page_number, $total_records_found, $query_string = null)
|
||||
{
|
||||
$page = 1;
|
||||
|
||||
echo "Page: ";
|
||||
|
||||
for ($total_pages = ($total_records_found/NUMBER_PER_PAGE); $total_pages > 0; $total_pages--)
|
||||
{
|
||||
if ($page != $current_page_number)
|
||||
echo "<a href=\"?page=$page" . (($query_string) ? "&$query_string" : "") . "\">";
|
||||
|
||||
echo "$page ";
|
||||
|
||||
if ($page != $current_page_number)
|
||||
echo "</a>";
|
||||
|
||||
$page++;
|
||||
}
|
||||
}
|
||||
|
||||
echo "<h2>Search Members</h2>
|
||||
<form action='{$_SERVER['PHP_SELF']}' method='post'>
|
||||
<p>ID: <input type='text' name='id' /></p>
|
||||
<p>Username: <input type='text' name='username' /></p>
|
||||
<p>Email: <input type='text' name='email' /></p>
|
||||
<p align='center'>
|
||||
<input type='submit' name='submit' value='Search' />
|
||||
</p>
|
||||
</form>";
|
||||
|
||||
/**
|
||||
* Display Search Results Below Here
|
||||
**/
|
||||
|
||||
//load the current paginated page number
|
||||
$page = ($_GET['page']) ? $_GET['page'] : 1;
|
||||
$start = ($page-1) * NUMBER_PER_PAGE;
|
||||
|
||||
/**
|
||||
* if we used the search form use those variables, otherwise look for
|
||||
* variables passed in the URL because someone clicked on a page number
|
||||
**/
|
||||
$id = ($_POST['id']) ? $_POST['id'] : $_GET['id'];
|
||||
$username = ($_POST['username']) ? $_POST['username'] : $_GET['username'];
|
||||
$email = ($_POST['email']) ? $_POST['email'] : $_GET['email'];
|
||||
|
||||
$sql = "SELECT * FROM members WHERE 1=1";
|
||||
|
||||
if ($id)
|
||||
$sql .= " AND id='" . mysql_real_escape_string($id) . "'";
|
||||
|
||||
if ($username)
|
||||
$sql .= " AND username='" . mysql_real_escape_string($username) . "'";
|
||||
|
||||
if ($email)
|
||||
$sql .= " AND email='" . mysql_real_escape_string($email) . "'";
|
||||
|
||||
//this return the total number of records returned by our query
|
||||
$total_records = mysql_num_rows(mysql_query($sql));
|
||||
|
||||
//now we limit our query to the number of results we want per page
|
||||
$sql .= " LIMIT $start, " . NUMBER_PER_PAGE;
|
||||
|
||||
/**
|
||||
* Next we display our pagination at the top of our search results
|
||||
* and we include the search words filled into our form so we can pass
|
||||
* this information to the page numbers. That way as they click from page
|
||||
* to page the query will pull up the correct results
|
||||
**/
|
||||
pagination($page, $total_records, "id=$id&username=$username&email=$email");
|
||||
|
||||
$loop = mysql_query($sql)
|
||||
or die ('cannot run the query because: ' . mysql_error());
|
||||
|
||||
while ($record = mysql_fetch_assoc($loop))
|
||||
echo "<br/>{$record['id']}) " . stripslashes($record['username']) . " - {$record['email']}";
|
||||
|
||||
echo "<center>" . number_format($total_records) . " search results found</center>";
|
||||
|
||||
pagination($page, $total_records, "id=$id&username=$username&email=$email");
|
||||
?>
|
Reference in New Issue
Block a user