<?php

/* This sample program searches and pages through a MySQL
database from a PHP page. Initial results show all entries,
but then the user may choose to restrict entries based on
the content of the title field

This example is used to look through and search the titles
of my blog, "The Horse's Mouth", but it has far wider
application too */

mysql_connect("localhost","wellho","wawawawa");
mysql_select_db("wellho");

$perpage = 10;
$html = "";
$startat = $_REQUEST[page] * $perpage;
$limlim = "%".$_REQUEST[look4]."%";

$q = mysql_query("select count(entry_id) from mt_entry where entry_title like '$limlim'");
$row = mysql_fetch_array($q);
$havesome = $row[0];
$pages = floor(($row[0]-1)  / $perpage) +1 ;

$q = mysql_query("select * from mt_entry where entry_title like '$limlim' order by entry_id desc limit $startat,$perpage");

while ($row = mysql_fetch_assoc($q)) {
   $text = strip_tags($row[entry_text]);
   $text = substr($text,0,300);
   $html .= "<dt>$row[entry_id] - <a href=/mouth/$row[entry_id]_.html target=pix>$row[entry_title]</a></dt>";
   $html .= "<dd>$text ....<br><br></dd>";
   };

$lynx = "Please choose the next page you want to view:";
for ($k=0; $k<$pages; $k++) {
   if ($k != $_REQUEST[page]) {
    $lynx .= " <a href=$PHP_SELF"."?page=$k&look4=".urlencode(stripslashes($_REQUEST[look4])).">".($k+1)."</a>";
   } else {
    $lynx .= " <b>--".($k+1)."--</b>";
   }
}
if ($pages < 2) {
   $lynx = "All results shown on this page";
}
if ($havesome == 0) {
   $lynx = "Sorry - no titles matched. Please change your search string";
   }
?>
<html><head>
<title>Showing blog entries</title>
<body>
<h2>Search titles on "The Horse's Mouth"</h2>
<form>Search only for titles including ... <input name=look4
value="<?= htmlspecialchars(stripslashes($_REQUEST[look4])) ?>">
(Please leave box empty to select all titles)<br>
<input type=submit></form><br>
<h2>Here are the entries you selected - page <?= $_REQUEST[page]+1 ?>:</h2><br>
<?= $html ?>
<?= $lynx ?>
</body>

-- PeterJones - 07 Nov 2006
Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2006-11-07 - PeterJones
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback