Searches, and search engines example from a Well House Consultants training course
More on Searches, and search engines
[link]
This example is described in the following article(s): • Static variables and ampersands in PHP - [link] • How to show a large result set page by page in PHP - [link] |
If you're searching for a page where you can try this code,
select here |
Source code: page_it.php Module: H308
<?php
/* This is a demonstrattion which allows you to page through a result set
(lines matching a word from within a data file). They are shown 12 per
page, and you can move forward and backward pages if they won't all fit
on a single page. If you get no matches at all, then the system will
suggest a handful of close alternatives. An example for our PHP
programming course! */
$pagesize = 12;
// Function to add items into a table only of they're on the right page.
/* Incoming parameters:
An array of the columns for display in the table (current row)
Number of matches so far (incremented within the function)
Number of matches SHOWN so far (also incremented)
The page number your are displaying
The number of items to show per page */
function show_item($bits,&$matched,&$shown,$pageno,$pagesize) {
# First call ONLY - work out the range of matches to show
static $call = 0;
static $showfrom, $showto;
if ($call++ == 0) {
$showfrom = $pageno * $pagesize;
$showto = $showfrom + $pagesize - 1; }
# If the current matched item is in the range to show, add it to
# the return string
if ($matched >= $showfrom and $matched <= $showto) {
$showrow .= "<tr><td>$matched: $bits[0]</td><td>$bits[1].</td></tr>";
$shown++; }
$matched++;
return $showrow;
}
// Function to generate a page chooser line . Always offer the first and last
// pages, and the 2 pages immediately before and after the current page too.
// Add in extra ... sequences to indicate where page numbers are missing from
// the menu, and highlight (rather than provide a link) on the current page
// so that your user isn't going to attempt to link back to himself.
/* Parameters are
Number of matches
Page size
Current page number being displayed
Any other parameters to be passed with links */
function page_chooser($matched, $pagesize, $pageno, $prestring = "") {
$pagestring = "";
$pages = floor(($matched + $pagesize - 1) / $pagesize);
if ($pages > 1) {
# loop through all possible page numbers to offer
for ($k=0; $k<$pages; $k++) {
# Offer first, last, and within 3 of current
$wantit = 0;
if ($k == 0) $wantit = 1;
if ($k == $pages-1) $wantit = 1;
if (abs($k - $pageno) < 3) $wantit = 1;
# Make it either a link or a plain bold piece of text
if ($wantit) {
if ($pageno != $k) {
$pagestring .= "<a href=\"?$prestring&pageno=$k\">".($k+1)."</a> ";
} else {
$pagestring .= "<b>".($k+1)."</b> ";
}
# If we are NOT putting the number out, do we need to output "..."
$real = 1;
} else {
if ($real == 1) $pagestring .= " ... ";
$real = 0;
}
}
$pagestring .= "<br />";
}
return $pagestring;
}
# ------------------------- Main code
# Collect incoming parameters
$pageno = 0;
if ($_REQUEST[pageno] != "") $pageno = $_REQUEST[pageno];
$rq = stripslashes($_REQUEST[wot]);
$rqw = htmlspecialchars($rq);
if ($_REQUEST[wot] == "") {
// ---------- NO DATA SUMITTED
$answer = "Your answer will appear here";
} else {
if (preg_match("/^[A-Z0-9 ]+$/i",$rq)) {
// ---------- VALID DATA SUBMITTED
$places = file("$_SERVER[DOCUMENT_ROOT]/data/also.xyz");
$answer = "<br><b>Lines matching $rq</b><br>";
$answer .= "<table border=1 cellpadding=3>";
$shown = $total = $matched = 0;
$try = array();
for ($k=0; $k<count($places); $k++) {
$total++;
$bits = preg_split('/\s+/',trim($places[$k]),2);
if (preg_match("/$rq/i",$bits[1])) {
$answer .= show_item($bits,$matched,$shown,$pageno,$pagesize);
}
// in case everything fails to match, gather up nearby alternatives
$diff = levenshtein($bits[1],$rq);
$try[$bits[1]] = $diff;
}
$answer .= "<tr><td colspan=2>Matched: $matched of $total ($shown shown)</td></tr>";
$answer .= "</table><br>";
$answer .= page_chooser($matched,$pagesize,$pageno, "wot=$rqw");
// If there were no matches, offer the user the nearest alternatives
if ($shown == 0) {
asort($try);
$answer .= "Was it ... ";
$k = 0;
foreach (array_keys($try) as $quiteclose) {
$answer .= " $quiteclose {$try[$quiteclose]}; ";
if ($k++ > 3) break;
}
}
// ---------- INVALID DATA SUBMITTED
} else {
$answer = "There are some nasty characters around";
}
}
// ----------------------------------------------------
?>
<html>
<body>
<h1>Looking up places!</h1>
<b>This sample program matches against places and their postal zones</b><br><br>
A demonstration by Well House Consultants, which shows you how you can present
results page by page, and offer a page selector "strip" to page through them.
The demonstration also includes the code to offer you the closest alternatives
if nothing matches at all<br><br>
<?= $answer ?>
<hr>
<form>Search for
<input name=wot size=10 value="<?= $rqw ?>"> and
<input type=submit></form<br>
Copyright, etc
</body>
</html>
Learn about this subject
This module and example are covered on our public
PHP Techniques course. If you have a group of
three or more trainees who need to learn the subject, we can also arrange a
private or
on site course for you.
Books covering this topic
Yes. We have over 700 books in our library.
Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Searches, and search engines" training module. You'll find a description of the topic and some
other closely related examples on the
"Searches, and search engines" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from
our download centre under an
Open Training Notes License.
Other resources
• Our
Solutions centre provides a number of longer technical articles.
• Our
Opentalk forum archive provides a question and answer centre.
•
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the
resources centre.
• All of these resources can be searched through through our
search engine
• And there's a global index
here.
Web site author
This web site is written and maintained by
Well House Consultants.
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job.
Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are
forbidden from using it for running your own training courses
without our prior written permission. See
our
page on courseware provision for more details.
Any of our images within this code may NOT be reused on a public URL without our
prior permission. For Bona Fide personal use, we will often grant you permission provided
that you provide a link back. Commercial use on a website will incur a license fee for
each image used - details on request.