Page 1 of 1
Search....
Posted: Mon Mar 07, 2011 9:50 am
by tattooartist
Ok,
Please bare with me because I am new to php. I have a page located here.
http://www.pennstateind.com/library.php. which I was able to build the php to return the results of the instructions with some guidance and have people download the pdf files. Now I need to apply a search just above all the displayed files on the page. My thought was to have a search box and when the client entered his product code it would bring him to that specific download or maybe just jump to that part of the page and highlight the product. Can I just search my already displayed reults or do I need to hit the database again? If so nay help would be great. Thanks in advance! I can supply my code for the existing display if needed
Lou
Re: Search....
Posted: Mon Mar 07, 2011 10:17 am
by social_experiment
tattooartist wrote:Now I need to apply a search just above all the displayed files on the page.
Do you mean you want to search the displayed files (results)? I don't understand what you have in mind but it would be a more user-friendly option to have any search done against the whole database, unless you specify it otherwise, like "search the result" or something to that effect.
Re: Search....
Posted: Mon Mar 07, 2011 2:51 pm
by tattooartist
Yes I want to search against the whole data base. This is what I have come up with but can not get it to work. I am using the code from my existing php for generating the display but can seem to get it to work. This code will generate a result but the file and description is not showing up. Thanks for the relpy and any input! The code below is running on this page and you can see what is happening. It seems to be returning the amount of files that there are but nothing is showing. Thanks. You can use this PK-PEN when you search. It is a valid product code. also another is PK50CAL to see the different results.
http://www.penstateind.com/library_ins_search.php
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="product_code">Product Code</option>
<Option VALUE="product_name">Product Name</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("psi") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM category c
inner join library l on c.category_id = l.category_id
left join files f on l.file_id = f.file_id WHERE upper($field) LIKE'%$find%'");
//And we display the results
while ($result = mysql_fetch_array( $data ))
{
echo "<b><a href='http://www.pennstateind.com/library/" . $filename . "'>$productcode</b>: $productname</a><br>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: Search....
Posted: Mon Mar 07, 2011 6:38 pm
by califdon
In your line:
Code: Select all
echo "<b><a href='http://www.pennstateind.com/library/" . $filename . "'>$productcode</b>: $productname</a><br>";
those 3 variables have not been set to anything. The easiest way is to use mysql_fetch_assoc() instead of mysql_fetch_array(), which at least lets you retrieve the values using the field names as indexes, like this:
Code: Select all
while ($result = mysql_fetch_assoc( $data ))
{
echo "<b><a href='http://www.pennstateind.com/library/" . $result['filename'] . " '>" . $result['productcode'] . "</b>: " . $result['productname'] . "</a><br>";
}
Re: Search....
Posted: Tue Mar 08, 2011 10:14 am
by social_experiment
Code: Select all
action="<?=$PHP_SELF?>">
// change to
action=""
You can also remove
$PHP_SELF (which is actually $_SERVER['PHP_SELF'] btw

).
Re: Search....
Posted: Fri Mar 11, 2011 9:44 am
by tattooartist
Thank you