Forum Moderators: coopster

Message Too Old, No Replies

PHP Search database by selecting checkbox

PHP Search database by selecting checkbox

         

greensleeves

1:19 am on Oct 25, 2007 (gmt 0)

10+ Year Member



Hi, Im a newbie to PHP and I'm wondering if someone could help me out with this
Tryng to search out products from a database by clicking a checkbox option

for instance if description contains the word "bluetooth" and "MP3"

<input type="checkbox" value="bt" />Bluetooth <br />
<input type="checkbox" value="cs" />Card slot <br />
<input type="checkbox" value="mp3" />MP3 player <br />

here is my database
$query = "SELECT id, manufacturer, item_number, description FROM $table_name
$result = @mysql_query ($query)

Here is my table layout

echo '<table>
<tr>
<td class="layout"><b><a href="' . $link1 . '">Manufacturer</a></b></td>
<td class="layout"><b><a href="' . $link2 . '">Item Number</a></b></td>
<td class="layout"><b><a href="' . $link3 . '">Description</a></b></td>
</tr>
';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee'? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td class="layout_nowwrap">' . $row['manufacturer'] . '</td>
<td class="layout">' . $row['item_number'] . '</td>
<td class="layout">' . $row['description'] . '</td>
</tr>
'; }

echo '</table>';

just in case your wondering I also set cases for sorting through colums

$link1 = "{$_SERVER['PHP_SELF']}?sort=mfa"; // manufacturer
$link2 = "{$_SERVER['PHP_SELF']}?sort=ina"; // item_number
and so on....

// manufacturer case
case 'mfa':
$order_by = 'manufacturer DESC, item_number DESC';
$link1 = "{$_SERVER['PHP_SELF']}?sort=mfd";
break;
case 'mfd':
$order_by = 'manufacturer ASC, item_number ASC';
$link1 = "{$_SERVER['PHP_SELF']}?sort=mfa";
break;

hope someone can help me with this... thanks

Habtom

4:25 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmasterworld, greensleeves

> Give your input boxes names:

<input type="checkbox" name="bluetooth" value="bt" />Bluetooth <br />
<input type="checkbox" name="card_slot" value="cs" />Card slot <br />
<input type="checkbox" name="mp3_player" value="mp3" />MP3 player <br />

[b] > Modify your query to accommodate the new criterias

$query = "SELECT id, manufacturer, item_number, description FROM $table_name WHERE description = '%". $_REQUEST['bluetooth'] ."%' OR description = '%". $_REQUEST['card_slot'] ."%' OR description = '%". $_REQUEST['mp3_player'] ."%'";
$result = @mysql_query ($query)

> You might need to check the form inputs for validation and security.

Hope this helps.

Habtom

greensleeves

7:22 am on Oct 25, 2007 (gmt 0)

10+ Year Member



Thanks this really pointed me in the right direction but im not sure how to check the form inputs for validation any help im a real rookie at this. What id like to do is filter out key words from a database on checking the input box

<?
include ('/header.html');

$link2 = "{$_SERVER['PHP_SELF']}?sort=mfa"; // manufacturer
$link3 = "{$_SERVER['PHP_SELF']}?sort=ina"; // item_number
$link7 = "{$_SERVER['PHP_SELF']}?sort=dna"; // description

// Determine the sorting order.
if (isset($_GET['sort'])) {

switch ($_GET['sort']) {
case 'mfa':
$order_by = 'manufacturer DESC, item_number DESC, color DESC';
$link2 = "{$_SERVER['PHP_SELF']}?sort=mfd";
break;
case 'mfd':
$order_by = 'manufacturer ASC, item_number ASC, color ASC';
$link2 = "{$_SERVER['PHP_SELF']}?sort=mfa";
break;
// cut out code for space
default:
$order_by = 'manufacturer ASC, item_number ASC';
break;
}
$sort = $_GET['sort'];
} else { // default sorting order.
$order_by = 'manufacturer ASC, item_number ASC';
$sort = 'mfd';
}
// query.
$query = "SELECT id, manufacturer, item_number, description FROM $table_name WHERE description = '%". $_REQUEST['bluetooth'] ."%' OR description = '%". $_REQUEST['card_slot'] ."%' OR description = '%". $_REQUEST['mp3_player'] ."%'";
$result = @mysql_query ($query)

// selection box
echo '<form method="post">
<input type="checkbox" name="bluetooth" value="bt" />Bluetooth <br />
<input type="checkbox" name="card_slot" value="cs" />Card slot <br />
<input type="checkbox" name="mp3_player" value="mp3" />MP3 player <br />
</form>
';
// Table header.
echo '<table>
<tr>
<td><b><a href="' . $link2 . '">Manufacturer</a></b></td>
<td><b><a href="' . $link3 . '">Item Number</a></b></td>
<td><b><a href="' . $link7 . '">Description</a></b></td>
</tr>
';
// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee'? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td>' . $row['manufacturer'] . '</td>
<td>' . $row['item_number'] . '</td>
<td>' . $row['description'] . '</td>
</tr> ';
}
echo '</table>';

mysql_free_result ($result);
mysql_close();
}
include ('/footer.html');
?>

Habtom

7:36 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The query needs to be improved to accommodate what you wanted:

if ($_REQUEST['bluetooth'] == "bt") {
$WHERE .= "description = 'keyword here for bt' OR";
}

if ($_REQUEST['bluetooth'] == "cs") {
$WHERE .= " description = 'keyword here for cs' OR";
}
if ($_REQUEST['bluetooth'] == "mp3") {
$WHERE .= " description = 'keyword here for mp3' OR";
}

$WHERE = rtrim($WHERE, "OR");

$query = "SELECT id, manufacturer, item_number, description FROM $table_name WHERE ". $WHERE;

You might expand this to get the right results.