Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing variable values

Status
Not open for further replies.

bill1one

Programmer
Sep 29, 2004
93
0
0
US
I have text boxes on my form for users to enter a range of numbers that they would like to see in the database. I would like to query to ignore the range if the user does not enter anything in the box.

To do this, I basically said if the variables are empty to reassign them with the minimum and maximum number possible.

Here is the code I have for that:

Code:
$min = $_POST['min'];
$max = $_POST['max'];

if ($min == "") {
$minNew = 0;
}
else {
$minNew = $min;
}

if ($max == "") {
$maxNew = 2000;
}
else {
$maxNew = $max;
}

It works fine, but seems clunky. Is there a more effecient way to do this?

Any help would be appreciated. I am using MySQL 5.0 and PHP 5.1.4
 
Hi

It could be written as :
Code:
$min = $_POST['min'];
$max = $_POST['max'];

$minNew = $min == "" ? 0 : $min;
$maxNew = $max == "" ? 2000 : $max;
But what if the value of [tt]$_POST['max'][/tt] is "Intelligent Work Forums for Computer Professionals" ? You will accept it just because it is not equal with "" ?

Feherke.
 
Code:
foreach(array('min','max') as $var){
 ${$var}New = !empty($_POST[$var]) ? $_POST[$var] : (($var === 'min') ? 0 : 2000);
}

i think that would work.
 
Both worked fine. Thanks a million. In response to
But what if the value of $_POST['max'] is "Intelligent Work Forums for Computer Professionals" ? You will accept it just because it is not equal with "" ?
I have a javascript to handle that. Thanks for pointing it out.
 
as a partial fix you could do this
Code:
foreach(array('min','max') as $var){
 ${$var}New = !empty($_POST[$var]) ? float_val($_POST[$var]) : (($var === 'min') ? 0 : 2000);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top