I'm still having problems trying to get a search script working the way I want it. Basically, I have a form where users enter the search type, enter the text to be searched for, and this in turn is passed to the php script, which should return the results, but also have hyperlinks at the top of selected fields, whereby if you click on them it will re-order the results (say alphabetical order of surname).
At the moment, I am able to do a search with no problems, but cannot work out how to allow for wildcards in the search text (say, entering "A%" in the type "Surname" will return all results where the surname begins with A). Nor can I get the re-order links to work - if I click on them I get a "No results".
I'm fairly new to php, so am really struggling at the moment. Heres the form (searchdb.htm) and the php script (test.php):
Form.htm
Test.php
Thanks in advance.
Pha3dr0n
At the moment, I am able to do a search with no problems, but cannot work out how to allow for wildcards in the search text (say, entering "A%" in the type "Surname" will return all results where the surname begins with A). Nor can I get the re-order links to work - if I click on them I get a "No results".
I'm fairly new to php, so am really struggling at the moment. Heres the form (searchdb.htm) and the php script (test.php):
Form.htm
PHP Code:
<html>
<head>
<title>Database search</title>
</head>
<body>
<h1><font face="Arial, Helvetica, sans-serif" color="#333333">Database Search</font></h1>
<form action="test.php" method="post">
<font face="Arial, Helvetica, sans-serif" color="#333333" size="2">Choose
Search Type:</font><br>
<select name="searchtype">
<option value="forename">Forename
<option value="surname">Surname
<option value="category">Category
<option value="title">Title
<option value="shows">Shows
<select>
<br>
<font face="Arial, Helvetica, sans-serif" size="2" color="#333333">Enter
Search Term:</font><br>
<input name="searchterm" type=text>
<br>
<input type=submit value="Search">
</form>
</body>
</html>
Test.php
PHP Code:
<?php
$default_sort = 'surname';
$allowed_order = array ('sex', 'surname','forename');
if($_GET['order']) $order = $_GET['order'];
else $order = $default_sort;
mysql_connect ('xxxx','xxxx','xxxx');
mysql_select_db ('contacts');
$column = $_POST['searchtype'];
$term = $_POST['searchterm'];
$query = "SELECT * FROM admin_contacts WHERE $column LIKE '%$term%' ORDER BY $order";
$result = mysql_query ($query);
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
?>
Pha3dr0n
Comment