Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT HELP with Simple single page search function

Status
Not open for further replies.

hoppy2

Programmer
Joined
Sep 14, 2003
Messages
8
Location
AU
Hi Everyone,

I'm a PHP newbie, or was that dumbie....

I have a .php page which lists all the categories in my MySQL category database file.

I need a search function that will appear on the category.php page and will take the person to the category they are looking for.

It only needs to work on that one .php page full of categories.

Any help gratefully accepted.

Regards
hoppy2
 
I just realised what a dumb questioned I asked.

PHP is server side code so I really need java script to solve this problem. Once the category.php page is being viewed by the person viewing the page I need a browser side code...


Derrr...told you I was dumb
 
hello, this works fine and can be tweaked to many usages
hope it'll work for you
////first is the include named dbinfo.inc.php
//////
<?
$username=&quot;aaaaa&quot;;
$password=&quot;aaaaaaa&quot;;
$database=&quot;aaaaaaaa&quot;;
$host=&quot;aaaaaaa&quot;;
?>

///// then is the engine
///allows for number of pages etc....

<?

include(&quot;dbinfo.inc.php&quot;);
?>
<?

$connect = @mysql_connect(&quot;$host&quot;, &quot;$username&quot;, &quot;$password&quot;);

if (!($connect)) // If no connect, error and exit().
{
echo(&quot;<p>Unable to connect to the database server.</p>&quot;);
exit();
}

if (!(@mysql_select_db($database))) // If can't connect to database, error and exit().
{
echo(&quot;<p>Unable to locate the $database database.</p>&quot;);
exit();
}

if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = mysql_query(&quot;SELECT business_name, newsletter_name, newsletter_text FROM stories where category like '%ag%&quot;. $query .&quot;%' &quot;);


// the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo(&quot;No results found matching your query - $query&quot;);
exit();}

$pages = intval($numrows/$limit); // Number of results pages.

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

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

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
?>
<html>
<head>
<title>Search Results for <?=$query?></title>
</head>
<body>

<table width=&quot;100%&quot; border=&quot;2&quot; bgcolor=&quot;#ffffcc&quot;>
<tr>
<td align=&quot;left&quot; colspan=&quot;4&quot;>
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>

Page <b><?=$current?></b> of <b><?=$total?></b>

Results per-page: <a href=&quot;<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5&quot;><b>5</b></a> | <a href=&quot;<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10&quot;><b>10</b></a>
</td>
</tr>

<?
//Go back into PHP mode.

// Now we can display results.

$results = mysql_query(&quot;SELECT business_name, newsletter_name, newsletter_text FROM stories where category like '%ag%&quot;. $query .&quot;%' ORDER BY business_name ASC LIMIT $page, $limit&quot;);
while ($data = mysql_fetch_array($results))
{
?>

<tr>
<td width=&quot;100% align=&quot;top&quot; valign=&quot;left&quot;><font color=&quot;#cccccc&quot; size=&quot;4&quot;><I>AGRICULTURE & FOOD</i></font>
</td></tr>
<tr>
<td width=&quot;100%&quot; align=&quot;left&quot; valign=&quot;top&quot;><font color=&quot;#990000&quot; size=&quot;3&quot;>Business Name</font>: <?=$data[&quot;business_name&quot;]?></font></td>
</tr>
<tr>
<td width=&quot;100%&quot; align=&quot;left&quot; valign=&quot;top&quot;><font color=&quot;#990000&quot; size=&quot;3&quot;>Newsletter Name</font>: <?=$data[&quot;newsletter_name&quot;]?></font></td>
</tr>
<tr>
<td width=&quot;33%&quot; align=&quot;left&quot; valign=&quot;top&quot;><font color=&quot;#990000&quot; size=&quot;3&quot;>Newsletter</font>: <br>
<?=$data[&quot;newsletter_text&quot;]?></font></td>
</tr>
<tr>
<td width=&quot;100%&quot; colspan=&quot;1&quot;>
<?
{ // If last page don't give next link.
$next_page = $page + $limit;
echo(&quot;<a href=\&quot;$PHP_SELF?query=$query&page=$next_page&limit=$limit\&quot;><align=left><font size=3>Next: Click here to jump to next page</a></font></hr>\n&quot;);}

?>
</tr>
</td>

<?
}
?>
<align=&quot;left&quot;>
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo(&quot;<a href=\&quot;$PHP_SELF?query=$query&page=$back_page&limit=$limit\&quot;>back</a> \n&quot;);}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo(&quot;<b><hr size=5 align=left width=400>$i</b></hr> \n&quot;);} // If current page don't give link, just text.
else{
echo(&quot;<font size=4><a href=\&quot;$PHP_SELF?query=$query&page=$ppage&limit=$limit\&quot;>$i</a></font> \n&quot;);}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(&quot;<a href=\&quot;$PHP_SELF?query=$query&page=$next_page&limit=$limit\&quot;><hr size=5 align=left width=400><font size=5>next</a></font></hr>\n&quot;);}
?>
</table>
</body>
</html>
 
Just a note:
Context switching between PHP and HTML makes the code harde to read and difficult to debug. If you can produce the HTML with your code or use external HTML templates.
 
Agreed on
usually this the way I proceed
HTML templates and includes

however since the HTML is so basic
and the code I hope well commented :)
 
Thanks Cabernet,

I'll give it atry today.

much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top