Saturday, March 5, 2011

how to get php to find and display data from mysql database ?

PHP, MySQL and a Web Server capable of parsing PHP pages, like Apache or IIS. This search script does not spider all your pages by crawling the links, so the content you want to be searchable must be within the database. Knowledge of PHP and MySQL is also necessary because this script is just the bones of a working solution. Numerous edits must take place upon this script before it is a usable solution.
Lets get started. If you would rather not copy the code into a text editor
Just after the body tag of your page, place the following HTML. This is for the form which will contain the textfield to enter our search string in.
form name="form" action="search.php" method="get"
input type="text" name="q"
input type="submit" name="Submit" value="Search" form
Now, enter the following PHP. Follow the PHP comments for what the script is doing, if you get stuck,
=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <<
Prev 10
  ";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo " a href=\"$PHP_SELF?s=$news&q=$var\" Next 10 >> a";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo " p Showing results $b to $a of $numrows p";

?>
Three major areas are covered in this script, the first is selecting data from the database which matches your entered keyword, the second is displaying the results on the web page and the last is generating the paging, which displays results in chunks of 10 with next/previous links where they are necessary.
Important: This script requires numerous edits before it can be adapted for use. The SQL query in this example is only selecting 1 field from a hypothetical database which doesn't pre-exist, nor is included in the downloadable files. Further down the script, the value of that field is being displayed on the page. The major amendments that need to be performed are 1.) The SQL statement, and 2.) The PHP which displays the result

No comments:

Post a Comment