[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

AJAX based search in PHP

Asked by PranjalShah in PHP Scripting Language, Miscellaneous Web Development

Tags: web development, php, mysql, AJAX

i want to do something like

http://www.petefreitag.com/item/605.cfm

it is a AJAX based suggestion search...what will be the suggest.cfm file look like in PHP...


Here is my code for the basic search and results....
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
<?php
//error handling
//ini_set('error_reporting',E_ALL);
//ini_set('display_errors',1);
include_once('../inludes/db.php');
?><!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" />
<link rel="stylesheet" type="text/css" media="screen" href="../builder/chassis/css/table.css" />
<script src="js/sorttable.js"></script>
<title>Search</title>
</head>
<body>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>
 
<?php
  // Get the search variable from URL
 
  $var = (!empty($_GET['q'])) ?  mysql_real_escape_string(trim($_GET['q'])) : '';
 
// rows to return
$limit=25; 
 
// check for an empty string and display a message.
if (empty($var))
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }
 
// Build SQL Query  
$query = "SELECT ID,name,description,vendor,IFNULL(price,'N/A')price,IFNULL(cost,'N/A')cost,IFNULL(category,'N/A')category,IFNULL(subcategory,'N/A')subcategory FROM prices WHERE name LIKE '%$var%' OR description LIKE '%$var%' OR vendor LIKE '%$var%' OR category LIKE '%$var%' OR subcategory LIKE '%$var%'";
//$query = "SELECT * FROM prices WHERE name LIKE '%$var%' OR description LIKE '%$var%' OR vendor LIKE '%$var%' OR category LIKE '%$var%' OR subcategory LIKE '%$var%'"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
// If we have no results, offer a google search as an alternative
 
if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $var . "&quot; returned zero results</p>";
  }
 
// next determine if s has been passed to script, if not use 0
 $s = (!empty($_GET['s'])) ?  intval($_GET['s']) : 0;
 
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>Your search results for <strong> &quot;" . $var . "&quot;</strong> are as below..</p>";
 
// begin to show results set
//echo "<h4>Results</h4>";
$count = 1 + $s ;
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$_SERVER[PHP_SELF]?s=$prevs&q=$var\">&lt;&lt; 
  Prev $limit</a>&nbsp ";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo " <a href=\"$_SERVER[PHP_SELF]?s=$news&q=$var\">Next $limit &gt;&gt;</a>";
  }
  
// now you can display the results returned
  /*while ($row= mysql_fetch_array($result)) {
  $title = $row["name"];
 
  echo "$count.) $title<br>" ;
  $count++ ;
  }
 */
 if(mysql_num_rows($result) )
{
  echo "<table width=50% border=1 class=sortable>";
  echo "<tr><th> </th><th>No</th><th>Name</th><th>Description</th><th>Vendor</th><th>List Price</th><th>Street Price</th><th>Category</th><th>Sub Category</th></tr>";
// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
	if($row['cost']=="N/A") {$dol1="";} else {
	
	$dol1 = "$";  }
	$dol = "$";
  echo "<tr><td><input type='checkbox' name='price[]' value='{$row['ID']}' /></td><td>{$count}</td><td>{$row['name']}</td><td>{$row['description']}</td><td>{$row['vendor']}</td><td>$dol{$row['price']}</td><td>$dol1{$row['cost']}</td><td>{$row['category']}</td><td>{$row['subcategory']}</td></tr>";
  $count++ ;
  }
  echo "</table>";
}
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$_SERVER[PHP_SELF]?s=$prevs&q=$var\">&lt;&lt; 
  Prev $limit</a>&nbsp ";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo " <a href=\"$_SERVER[PHP_SELF]?s=$news&q=$var\">Next $limit &gt;&gt;</a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?></body>
</html>
[+][-]10/28/09 06:07 PM, ID: 25689810Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 11:14 PM, ID: 25691043Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625