Forum Moderators: coopster

Message Too Old, No Replies

Search DB using data from rows in place of text input

         

macspeed

12:38 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



Hello,
I'm new to PHP and managed to get a simple search script working but can't find how to replace the user text input with a drop down selection populated from my database rows.
<html> 
<head>
<basefont face="Arial">
</head>
<body>

<?php

error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Search <input type="text" name="search"><br>
<select size="1" name="dropdown">
<option value="" selected>Search By...</option>
<option value="Manufacturer">Manufacturer</option>
<option value="Model">Model</option>
<option value="Socket">Socket</option>
<option value="Hyperthread">Hyperthread</option>
<option value="Cores">Cores</option>
</select>
<input type="Submit" value="Submit" name="Submit">
</form>

<?php
}

else {

// form submitted
// set server access variables
$host = "#*$!X";
$user = "#*$!X";
$pass = "#*$!X";
$db = "#*$!X";

$search = empty($_POST['search'])? die ("ERROR: Enter Search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from Dropdown") : mysql_escape_string($_POST['dropdown']);

// Open Connection

$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");

//Select Database

mysql_select_db($db) or die ("Unable to connect to database");

//Create Query

$query = "SELECT * FROM CPU WHERE $dropdown='$search'" or die (mysql_error());

$result = mysql_query($query) or die (mysql_error());

$num=mysql_numrows($result);

mysql_close($connect);

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$Manufacturer=mysql_result($result,$i,"Manufacturer");
$Model=mysql_result($result,$i,"Model");
$Socket=mysql_result($result,$i,"Socket");
$Hyperthread=mysql_result($result,$i,"Hyperthread");
$Cores=mysql_result($result,$i,"Cores");

echo "<br><b>$Manufacturer</b></br>$Model</b></br>Socket: $Socket</br>Hyperthread: $Hyperthread</br>Cores: $Cores<br>";

$i++;

}
}
?>

</body>
</html>


I want my users to be able to select variables of the CPU configuration which are currently stored in my database under rows with the same name as those currently in the search dropdown (Manufacturer, Model, Cores, etc).

example: User wants to retrieve all CPUs with "4" cores that fit a "1156 socket".

I'd like for the return to be the entire record for the matching CPU.

If anyone has a working example of this I'd greatly appreciate it.

macspeed

2:19 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



I hate to ask but is there a way to build a table within the php for the search page drop downs similar to the one on the results page?

There's a query embedded in the table code that has me confused as to how to re-use the code.

Readie

2:46 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup, quite simple:

$filter = '<table>
<tr>
<td>' . select_menu('Manufacturer', 'CPU') . '</td>
<td>' . select_menu('Cores', 'CPU') . '</td>
<td>' . select_menu('Socket', 'CPU') . '</td>
<td>' . select_menu('x64', 'CPU') . '</td>
<td>' . select_menu('Price', 'CPU') . '</td>
</tr>
</table>';
echo $filter;

Also - which query has you confused? I'll try and clarify it for you.

macspeed

3:31 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



That's what I thought the solution would be, break the select_menu function into more lines with table elements in between.

What confused me was the table in the return function has this query at the top:

$sql .= ' ORDER BY `Model` ASC';

$result = mysql_query($sql);
$rows = mysql_num_rows($result);

echo '<table cellspacing="2">


From what I can see it looks like the results are being parsed into rows according to the number of results coming back from search.

I wasn't sure how to accomplish the same thing at the "start" of the coding. Your example clears things up though (as usual). :)

Readie

3:37 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From what I can see it looks like the results are being parsed into rows according to the number of results coming back from search.

Pretty much.

If you find any (non custom) functions that you don't know, you can look their meaning up really easily using the search box on PHP.net [php.net]

macspeed

3:48 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



OK here's what I went with:


<?php
$filter = '<table>
<tr>
<td><b>Manufacturer</b></td>
<td><b>Cores</b></td>
<td><b>Socket</b></td>
<td><b>64 Bit CPU</b></td>
<td><b>Average Price (USD)</b></td>
</tr>
<tr>
<td>' . select_menu('Manufacturer', 'CPU') . '</td>
<td>' . select_menu('Cores', 'CPU') . '</td>
<td>' . select_menu('Socket', 'CPU') . '</td>
<td>' . select_menu('x64', 'CPU') . '</td>
<td>' . select_menu('Price', 'CPU') . '</td>
</tr>
</table>';
echo $filter;
?>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>



I just need to pad a bit here and there for spacing and add the bg color elements back in but otherwise it looks great and actually works.

Thank You :)

Readie

4:06 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo $filter;

I just showed it that way because it's the way I normally do my code: Develop everything into a variable, and echo it all out at the very end of the PHP document.

With the way you are doing things, you may aswell just delete what I've quoted above and replace
[b]$filter = '[/b]
with
[b]echo '[/b]
- it'll make your script a bit more efficient as you're not consuming any additional memory.

Anyways, you're welcome, and I wish you luck with the rest of your project :)

macspeed

5:10 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



Here's one I forgot-
What statement would perform:

"if $Rows = 0 display "no objects matched your search" "?

I'll search around and see if I can find it

IF $Rows = '0' print "No results matched your query" ?

Readie

5:31 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if($rows = mysql_num_rows($result)) {
// Results found
} else {
// No results
}

Will get that functionality. It's actually setting $rows there, so you don't need that line further up.
This 38 message thread spans 2 pages: 38