Forum Moderators: coopster

Message Too Old, No Replies

Question about setting options

         

harryhermit

8:37 pm on Feb 29, 2004 (gmt 0)

10+ Year Member



Hi All,

I've been trying to get this to work for a while now, but I can't figure it out. Tryed out different suggestions, but can't get it to work the way I would like. Anyhow, I have a list of results I set in a table and at the top I have an option for entering min and max price and/or the option for sorting the table my name, price, or brand. Works great if I enter in a min and max value, but it returns an error if I don't enter in any values or enter in a non numerical value. Well, what I would rather have it do is not return an error and take me to the page using the options it does recognize and ignore the options it doesn't recognize or if it is empty. Here is my code:

if (!isset ($_GET['min'])) {
$min = $default_min;
} else {
$min = $_GET['min'];
}
$default_max = '1000';
if (!isset ($_GET['max'])) {
$max = $default_max;
} else {
$max = $_GET['max'];
}
$default_sort = 'name';
if (!isset ($_GET['sort']) ) {
$sort = $default_sort;
} else {
$sort = $_GET['sort'];
}
echo "
<form method=get action=/$word/filter>
<table cellspacing=0 border=0 cellpadding=0 width=100%>
<tr>
<td align=left bgcolor=EEEEEE>
<input type=hidden name=category value=\"$fetch[category]\">
<table border=0 cellspacing=0 cellpadding=2 width=485>
<tr>
<td><font color=#666666>Min Price</font> $
<input class=filter type=text size=\"5\" name=\"min\" value=\"\"></td>
<td><font color=#666666>Max Price</font> $
<input class=filter type=text size=\"5\" name=\"max\" value=\"\"></td>
<td align=left>and/or Sort by</td>
<td align=left><select name=sort class=select size=1>
<option value=name>Name</option>
<option value=our_price>Price</option>
<option value=brand>Brand</option>
</select> </td>
<td colspan=2 align=left><input type=submit value=Apply name=update style=\"color:#333333; background-color:#E0E0D0; font-size:11px;\"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>

My query is like so:
$result = mysql_query("select * from evitamins_products WHERE category='$category' and our_price BETWEEN $min and $max ORDER by $sort ASC") or die (mysql_error());

jatar_k

9:11 pm on Feb 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just add a bit more error checking to each

if I don't enter in any values or enter in a non numerical value

if (!isset($_GET['min']) ¦¦ $_GET['min'] == "" ¦¦!is_numeric [ca3.php.net]($_GET['min'])) $min = $default_min;
else $min = $_GET['min'];

I threw them all in the first if but you could handle non numeric by returning a "please enter a numeric value" in an else if as well.

WebmasterWorld breaks pipes, replace all ¦ with real pipe chars

harryhermit

10:06 pm on Feb 29, 2004 (gmt 0)

10+ Year Member



Wow, that was easy enough. This is in the "so simple, why I didn't think of that" category. I don't know if its me getting frustated that I can find answers for myself fast enough or my php newbiness. Prolly both. Anyhow, thanks Jatar, worked like a charm:)

jatar_k

10:16 pm on Feb 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sometimes you just need a nudge in the right direction from a fresh pair of eyes. ;)