Help on search function

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
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Help on search function

Post by thcc2 »

hi all,
I had get a search function from a reference book, but it doesn't work and display all the selected results when the page is loaded. I want it to only display the result when the search button is clicked. the commented code is the original code from the book.

Code: Select all

//if($submit)

//{
	//add
	 print ("<form action=\"./search3.php\" method=\"get\">\n");
    print ("<table width=\"525\" cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"#123456\">\n");
    print ("<tr>\n");
    print ("<td colspan=\"2\" bgcolor=\"#DFFFDF\"><b>Search your address book...</b></td>\n");
    print ("</tr>\n");
    print ("<tr>\n");
    print ("<td colspan=\"2\" bgcolor=\"#ffffff\">\n");
    print ("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n");
    print ("<tr>\n");
    print ("<td>&nbsp;First Name</td>\n");
    print ("<td>Last Name</td>\n");
    print ("</tr>\n");
    print ("<tr>\n");
    print ("<td>&nbsp;<input type=\"text\" name=\"display_name\" size=\"20\" maxsize=\"20\"></td>\n");
    print ("<td><input type=\"text\" name=\"lastName\" size=\"30\" maxsize=\"30\"></td>\n");
    print ("</tr>\n");
    print ("</table>\n");
    print ("</td>");
    print ("</tr>");
    print ("<tr>\n");
    print ("<td colspan=\"2\" align=\"right\" bgcolor=\"#DFFFDF\">\n");
    print ("<input type=\"hidden\" name=\"tab\" value=\"?\">");
    print ("<input type=\"submit\" name=\"submit\" value=\"Search\">&nbsp;");
    print ("</td>\n");
    print ("</tr>\n");
    print ("</table>\n");
    print ("</form>\n");
	//add

   $query = "select display_name, mobile_numb, level, class from address_book " .
   		"where display_name like '{$display_name}%'";

  

   $query .= " order by display_name";

   $pg_result = pg_query($dbconn, $query);
   echo($pg_query);

   print ("<table width=\"525\" cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"#123456\">");

   print ("<tr>");

   print ("<td bgcolor=\"#dfdfdf\"><b>Name</b></td>");

   print ("<td bgcolor=\"#dfdfdf\"><b>Phone</b></td>");

   print ("<td bgcolor=\"#dfdfdf\"><b>E-Mail</b></td>");

   print ("</tr>");

   while($row = pg_fetch_row($pg_result))

   {
   	echo "ha";

	  if(($row[4] == 1) or ($row[4] == ""))

	  {

	     $primaryPhone = $row[10];

	     $primaryEmail = $row[13];

	  }

	  if($row[4] == 2)

	  {

	     $primaryPhone = $row[20];

	     $primaryEmail = $row[23];

	  }

      print("<tr>\n");

      printf("<td bgcolor=\"#ffffff\">&nbsp;<a href=\"./search3.php?contactID=%s\">%s, %s</a></td>\n", $rowcolor, $row[0], $row[1], $row[2]);

      printf("<td bgcolor=\"#ffffff\">&nbsp;<a href=\"./search3.php?contactID=%s\">%s</a></td>\n", $rowcolor, $row[0], $primaryPhone);

      printf("<td bgcolor=\"#ffffff\">&nbsp;<a href=\"./search3.php?contactID=%s\">%s</a></td>\n", $rowcolor, $row[0], $primaryEmail);

      print("</tr>\n");

   }

   print ("<tr>");

   print ("<th bgcolor=\"#dfdfdf\">&nbsp;</th>");

   print ("<th bgcolor=\"#dfdfdf\">&nbsp;</th>");

   print ("<th bgcolor=\"#dfdfdf\">&nbsp;</th>");

   print ("</tr>");

   print ("<tr>\n");

   print ("<td colspan=\"3\" align=\"right\" bgcolor=\"#DFFFDF\">\n");

   print ("<a href=\"./search3.php?tab=?\">Search Again</a>.");

   print ("</td>\n");

   print ("</tr>\n");

   print ("</table>");

//}

//else

//{
	//echo "aa";

   //print ("<form action=\"./search3.php\" method=\"get\">\n");

   //print ("<table width=\"525\" cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"#123456\">\n");

   //print ("<tr>\n");

   //print ("<td colspan=\"2\" bgcolor=\"#DFFFDF\"><b>Search your address book...</b></td>\n");

   //print ("</tr>\n");

   //print ("<tr>\n");

   //print ("<td colspan=\"2\" bgcolor=\"#ffffff\">\n");

   //print ("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n");

   //print ("<tr>\n");

   //print ("<td>&nbsp;First Name</td>\n");

   //print ("<td>Last Name</td>\n");

   //print ("</tr>\n");

   //print ("<tr>\n");

   //print ("<td>&nbsp;<input type=\"text\" name=\"display_name\" size=\"20\" maxsize=\"20\"></td>\n");

   //print ("<td><input type=\"text\" name=\"lastName\" size=\"30\" maxsize=\"30\"></td>\n");

   //print ("</tr>\n");

   //print ("</table>\n");

   //print ("</td>");

   //print ("</tr>");

   //print ("<tr>\n");

   //print ("<td colspan=\"2\" align=\"right\" bgcolor=\"#DFFFDF\">\n");

   //print ("<input type=\"hidden\" name=\"tab\" value=\"?\">");

   //print ("<input type=\"submit\" name=\"submit\" value=\"Search\">&nbsp;");

   //print ("</td>\n");

   //print ("</tr>\n");

   //print ("</table>\n");

   //print ("</form>\n");

//}

?>
hope someone can help me on this, thks in advance
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Why did you comment out some of the code? From a quick glance at the code it seems the code you commented is what is needed to show first the input field (when not submitted), and show the results when POST values are submitted.

Code: Select all

if(isset($_GET['submit'])
{   
// form is submitted, show form + process + results
// ....

}
else
{    
// form is not yet submitted, show form
// ....
}
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

matthijs wrote:Why did you comment out some of the code? From a quick glance at the code it seems the code you commented is what is needed to show first the input field (when not submitted), and show the results when POST values are submitted.

Code: Select all

if(isset($_GET['submit'])
{   
// form is submitted, show form + process + results
// ....

}
else
{    
// form is not yet submitted, show form
// ....
}
thks for your reply
i dont know where does the $submit come from?
if i not commment the

Code: Select all

if($submit)
it will go into the else block when the page is loaded
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

The $submit is coming from the form that is shown with the else block. That is what is supposed to happen.

Code: Select all

//print ("<input type=\"submit\" name=\"submit\" value=\"Search\">&nbsp;");
The logic is like this:
if { form is submitted, process search and show results ... }
else { form is not yet submitted, show form to fill in a search item ... }

Does this help?
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

matthijs wrote:The $submit is coming from the form that is shown with the else block. That is what is supposed to happen.

Code: Select all

//print ("<input type="submit" name="submit" value="Search">&nbsp;");
The logic is like this:
if { form is submitted, process search and show results ... }
else { form is not yet submitted, show form to fill in a search item ... }

Does this help?
thks for your help,
i have uncomment my code, but it still go to the else block when the submit button is clicked. Pls look at my code thks. is it because my button is located inside the else block, so it is not able to detect when the submit button has been clicked?
here is my code after uncomment, still doesn't work.

Code: Select all

if($submit)

	{
		echo "aa";

   	$query = "select display_name, mobile_numb, level, class from address_book " .
   		"where display_name like '{$display_name}%'";

  

   	$query .= " order by display_name";

   	$pg_result = pg_query($dbconn, $query);
   	echo($pg_query);

   	print ("<table width="525" cellspacing="1" cellpadding="3" border="0" bgcolor="#123456">");

   	print ("<tr>");

   	print ("<td bgcolor="#dfdfdf"><b>Name</b></td>");

   	print ("<td bgcolor="#dfdfdf"><b>Phone</b></td>");

   	print ("<td bgcolor="#dfdfdf"><b>E-Mail</b></td>");

   	print ("</tr>");

   	while($row = pg_fetch_row($pg_result))

   	{
   	//echo "ha";

	  		if(($row[4] == 1) or ($row[4] == ""))

	  		{

	     		$primaryPhone = $row[10];

	     		$primaryEmail = $row[13];

	  		}

	  		if($row[4] == 2)

	  		{

	     		$primaryPhone = $row[20];

	     		$primaryEmail = $row[23];

	  		}

      	print("<tr>\n");

      	printf("<td bgcolor="#ffffff">&nbsp;<a href="./search3.php?contactID=%s">%s, %s</a></td>\n", $rowcolor, $row[0], $row[1], $row[2]);

      	printf("<td bgcolor="#ffffff">&nbsp;<a href="./search3.php?contactID=%s">%s</a></td>\n", $rowcolor, $row[0], $primaryPhone);

      	printf("<td bgcolor="#ffffff">&nbsp;<a href="./search3.php?contactID=%s">%s</a></td>\n", $rowcolor, $row[0], $primaryEmail);

      	print("</tr>\n");

   }

   print ("<tr>");

   print ("<th bgcolor="#dfdfdf">&nbsp;</th>");

   print ("<th bgcolor="#dfdfdf">&nbsp;</th>");

   print ("<th bgcolor="#dfdfdf">&nbsp;</th>");

   print ("</tr>");

   print ("<tr>\n");

   print ("<td colspan="3" align="right" bgcolor="#DFFFDF">\n");

   print ("<a href="./search3.php?tab=?">Search Again</a>.");

   print ("</td>\n");

   print ("</tr>\n");

   print ("</table>");

}

else

{
	echo "here";

   print ("<form action="./search3.php" method="get">\n");

   print ("<table width="525" cellspacing="1" cellpadding="3" border="0" bgcolor="#123456">\n");

   print ("<tr>\n");

   print ("<td colspan="2" bgcolor="#DFFFDF"><b>Search your address book...</b></td>\n");

   print ("</tr>\n");

   print ("<tr>\n");

   print ("<td colspan="2" bgcolor="#ffffff">\n");

   print ("<table width="100%" cellspacing="0" cellpadding="0" border="0">\n");

   print ("<tr>\n");

   print ("<td>&nbsp;First Name</td>\n");

   print ("<td>Last Name</td>\n");

   print ("</tr>\n");

   print ("<tr>\n");

   print ("<td>&nbsp;<input type="text" name="display_name" size="20" maxsize="20"></td>\n");

   print ("<td><input type="text" name="lastName" size="30" maxsize="30"></td>\n");

   print ("</tr>\n");

   print ("</table>\n");

   print ("</td>");

   print ("</tr>");

   print ("<tr>\n");

   print ("<td colspan="2" align="right" bgcolor="#DFFFDF">\n");

   print ("<input type="hidden" name="tab" value="?">");

   print ("<input type="submit" name="submit" value="Search">&nbsp;");

   print ("</td>\n");

   print ("</tr>\n");

   print ("</table>\n");

   print ("</form>\n");

}

?>
thks in advance
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

matthijs wrote:The $submit is coming from the form that is shown with the else block. That is what is supposed to happen.

Code: Select all

//print ("<input type="submit" name="submit" value="Search">&nbsp;");
The logic is like this:
if { form is submitted, process search and show results ... }
else { form is not yet submitted, show form to fill in a search item ... }

Does this help?
forgot to tell you that when the page is loaded it goes to the else block, but when i press the "Search button" it also go into the else block.
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

should i need to set something to

Code: Select all

$search
variable?
what i have to do with this variable before doing anything?
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

i had replace the

Code: Select all

if($search)
with

Code: Select all

if($_POST['submit'])
if still odesn't work for me :?:
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Try changing $search to $_GET['search']

Since php 4.1.0 using superglobals is the preferred way of retreiving variables (see http://nl2.php.net/reserved.variables)

So change the code to:

Code: Select all

<?php
if (isset($_GET['search']) 
{


}
Because the action of the form is:
method=\"get\">

You'll need to access the GET superglobal instead of the POST.
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

matthijs wrote:Try changing $search to $_GET['search']

Since php 4.1.0 using superglobals is the preferred way of retreiving variables (see http://nl2.php.net/reserved.variables)

So change the code to:

Code: Select all

<?php
if (isset($_GET['search']) 
{


}
Because the action of the form is:
method="get">

You'll need to access the GET superglobal instead of the POST.
thks a lot, but i have another problem which is due with the select statement i think, no matter how, it will display all the records in the database.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

it's probably the same issue. In the form there's the rule:

Code: Select all

<input type=\"text\" name=\"display_name\" size=\"20\" maxsize=\"20\">
The variable display_name should be recieved in the same way as the search, like:

Code: Select all

$display_name = $_GET['display_name'];
and then you can use $display_name in the query.

As a sidenote: do you do any input filtering for the variables you recieve and output escaping when you use them in the query?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Don't forget to sanitise your input!
thcc2
Forum Commoner
Posts: 33
Joined: Thu Jan 05, 2006 9:37 pm
Location: Singapore

Post by thcc2 »

matthijs wrote:it's probably the same issue. In the form there's the rule:

Code: Select all

<input type="text" name="display_name" size="20" maxsize="20">
The variable display_name should be recieved in the same way as the search, like:

Code: Select all

$display_name = $_GET['display_name'];
and then you can use $display_name in the query.

As a sidenote: do you do any input filtering for the variables you recieve and output escaping when you use them in the query?
now it got no result appear, only with the table header .
when i try to

Code: Select all

echo($query);
it gives me the following result

select display_name, mobile_numb, level, class from address_book where display_name like 'terence%'

Code: Select all

if($_GET['submit'])

	{
		$display_name = $_GET['display_name']; 
		//echo $display_name.'<br>'; 

   	$query = "select display_name, mobile_numb, level, class from address_book where " .
   	 "display_name like '$display_name%'"; 

  

   	//$query .= " order by display_name";
   	//echo $query.'<br>';

   	$pg_result = pg_query($dbconn, $query);
   	echo($query);

   	print ("<table width="525" cellspacing="1" cellpadding="3" border="0" bgcolor="#123456">");

   	print ("<tr>");

   	print ("<td bgcolor="#dfdfdf"><b>Name</b></td>");

   	print ("<td bgcolor="#dfdfdf"><b>Phone</b></td>");

   	print ("<td bgcolor="#dfdfdf"><b>E-Mail</b></td>");

   	print ("</tr>");
   	//$row = pg_fetch_row($pg_result)

   	while($row = pg_fetch_row($pg_result))

   	{
   	//echo "ha";

	  		if(($row[4] == 1) or ($row[4] == ""))

	  		{

	     		$primaryPhone = $row[10];

	     		$primaryEmail = $row[13];

	  		}

	  		if($row[4] == 2)

	  		{

	     		$primaryPhone = $row[20];

	     		$primaryEmail = $row[23];

	  		}

      	print("<tr>\n");

      	printf("<td bgcolor="#ffffff">&nbsp;<a href="./search3.php?contactID=%s">%s, %s</a></td>\n", $rowcolor, $row[0], $row[1], $row[2]);

      	printf("<td bgcolor="#ffffff">&nbsp;<a href="./search3.php?contactID=%s">%s</a></td>\n", $rowcolor, $row[0], $primaryPhone);

      	printf("<td bgcolor="#ffffff">&nbsp;<a href="./search3.php?contactID=%s">%s</a></td>\n", $rowcolor, $row[0], $primaryEmail);

      	print("</tr>\n");

   }

   print ("<tr>");

   print ("<th bgcolor="#dfdfdf">&nbsp;</th>");

   print ("<th bgcolor="#dfdfdf">&nbsp;</th>");

   print ("<th bgcolor="#dfdfdf">&nbsp;</th>");

   print ("</tr>");

   print ("<tr>\n");

   print ("<td colspan="3" align="right" bgcolor="#DFFFDF">\n");

   print ("<a href="./search3.php?tab=?">Search Again</a>.");

   print ("</td>\n");

   print ("</tr>\n");

   print ("</table>");

}
[/b]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

So everything is working fine then?

You may also want to add a second wildcard

Code: Select all

$query = "select display_name, mobile_numb, level, class from address_book where " .
        "display_name like '%".$display_name."%'";
Post Reply