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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php newbie (search/result page) 1

Status
Not open for further replies.

mayamanako

Technical User
Aug 31, 2005
113
GB
hi folks, can you please point me to right directions as to where could i find a tutorial for search and results page for php? preferably using dreamweaver. i've been searching but i'm out of luck. tnx very much in advance.
 
i just would like to have a simple search page wherein this page will search if there's already a particular item in the database and on the results page i would like to notify the visitor if a particulat item is in the database already or not.

i mentioned dreamweaver because i am familiar with it already and hoping to make it a bit easier.

thnx in advance and hope you can help.
 
simply put you need to start with a few different code bits:

1. some html to generate a web form into which a user can type search requests. call the textbox element you want to use the same thing as the field in your database.

2. on the page that the form submits to examine the value of the incoming form variable and use php to build the query for your database. then build a table from the results.

say you have a database of first name and last name:

the form
Code:
<form method="get" action="page2.php">
First Name: <input type="text" name="first_name"/> <br/>
Last Name: <input type="text" name="last_name"/> <br/>
<input type="submit" name="submit" value="Search" />
</form>
then page2.php note i have not included the database connect and select statements (mysql_connect and mysql_select_db). you will need to add these yourself.

Code:
<?
$where = "";
if (isset($_GET['last_name']):
	$last_name = trim ($_GET['last_name']);
	if (!empty($last_name)) $where .= " last_name like '%".$last_name."%' ";
endif;
if (isset($_GET['first_name']):
	$first_name = trim ($_GET['first_name']);
	if (!empty($first_name)) $where .= " last_name like '%".$first_name."%' ";
endif;

$sql = "select first_name, last_name from people where $where ";
$result = mysql_query($sql);

if (mysql_num_rows($result) == 0):
	die("No results found");
else:
	echo "<table><tr><th>First Name</th><th>Last Name</th></tr>";
	while ($row = mysql_fetch_assoc($result)):
		echo "<tr><td>".$row['first_name']."</td><td>".$row['last_name']."</td></tr>";
	endwhile;

	echo "</table>";
endif;
 
You're welcom.

But: sorry - have noticed that there is an error in the second where bit. it should be

Code:
if (isset($_GET['first_name']):
    $first_name = trim ($_GET['first_name']);
    if (!empty($first_name)):
      if (!empty($where)):
        $where .= " and first_name like '%".$first_name."%' ";
      else:
        $where .= " first_name like '%".$first_name."%' ";
      endif;
endif;

needed to add the " and " into the query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top