Help : Search Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
falieson
Forum Newbie
Posts: 2
Joined: Mon Apr 12, 2004 5:24 pm

Help : Search Page

Post by falieson »

I am trying to create a search page able to search by category or the name of the person. Yet, I haven't yet gotten it to work how I want it. I don't think much explanation is needed once the code is looked at. Thanks for any help!! :-D

search.php

Code: Select all

<html>
<body>
<form action="dir_results.php" method="post">
Choose to Search by 
<br />Catagory: 
<select name="category">
<option value="%">All</option>
<option>Accountants</option>
<option>Attorneys</option>
<option>Financial Planners</option>
<option>Insurance Agents</option>
<option>Trust Officers</option>
<option>Associates</option>
</select>
<br />Name:
<input type="text" name="name" maxlength=60 size=30 />
<br />
<input type="submit" value="Search" />
</form>
</body>
</html>

dir_results.php

Code: Select all

<html>
<body>

<?php 
// Connecting
include("includes/dblogin.php");
mysql_connect($host,$user,$pass) or die("Couldn't connect to database.");
mysql_select_db($db) or die("Couldn't select database");

$table = directory_members;

$query = "SELECT * FROM $table WHERE (name IS NOT NULL AND name LIKE '".$name."') or (category LIKE '".$category."') ORDER BY category, name ASC";  // DESC for decending

$result = mysql_query($query);
$num_results = mysql_num_rows($result);

while ($field = mysql_fetch_array($result)) &#123;
$type = $field&#1111;"category"];
$name = $field&#1111;"name"];
$portrait = $field&#1111;"portrait"];
$firm = $field&#1111;"firm"];
$position = $field&#1111;"position"];
$street1 = $field&#1111;"street1"];
if ($field&#1111;"street2"] = "") &#123;
	$street2 = "";
	&#125; else &#123;
	$street2 = "<br /> ".$field&#1111;"street2"];
	&#125;
$city = $field&#1111;"city"];
$state = $field&#1111;"state"];
$zip = $field&#1111;"zip"];
$email = $field&#1111;"email"];
$phone = $field&#1111;"phone"];
$fax = $field&#1111;"fax"];
$website = $field&#1111;"website"];

$output = <<<EOF
<table width="500px" height="300px" bgcolor="">
    <tr><td>
    <table width="40%" height="100%" bgcolor="" ALIGN="left"><TR>
    <td><br /><img src=$portrait width="200px" height="200px" /></td>
    </TR></table>
    <table width="58%" height="100%" bgcolor="" ALIGN="right"><TR><td>
    <b>$type</b>
	<br />$name
    <br />$firm
    <br />$street1
    $street2
    <br />$city, $state  $zip
    <br /> Phone: $phone
    <br /> Fax: $fax
    <br /> Email: <a href="mailto: $email">$email</a>
    <br /> Website: <a href="$website" >$website</a>
    </td></TR></table>
       </td></tr>
    </table>
EOF;

echo $output;
&#125;


echo "<p>Number of results found: ".$num_results."</p>";
?> 

</body>
</html>
[/i]
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You forgot to mention the error messages.

I do however want to inform you of this thread: viewtopic.php?t=511

I blatantly assume that it is the register globals issue you are having problems with ( $field vs. $_POST['field'] ). If not, abit more information, as in eventual error messages, would like help us more...
falieson
Forum Newbie
Posts: 2
Joined: Mon Apr 12, 2004 5:24 pm

Post by falieson »

I dont get any error messages, but it doesnt work correctly. The search through the name string doesn't work correctly.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Add the %'s around the $name part in the LIKE operator sql.

(name IS NOT NULL AND name LIKE '%$name%')
Post Reply