Forum Moderators: coopster

Message Too Old, No Replies

if and/or statements

         

princeofvegas

8:20 am on Jul 7, 2010 (gmt 0)

10+ Year Member



I am having a heck of a time getting this bit of code working.

$searchaddress = $_GET['searchaddress'];
$searchcity = $_GET['searchcity'];
$searchstate = $_GET['searchstate'];
$searchzip = $_GET['searchzip'];

if((empty($searchaddress)) || (empty($searchcity)) || (empty($searchstate))){
$fromaddress = "";
}elseif((isset($searchaddress)) && (isset($searchcity)) && (isset($searchstate))){
$fromaddress = "$searchaddress $searchcity $searchstate";
}elseif(isset($searchzip)){
$fromaddress = "$searchzip";
}
echo $fromaddress;


The if and the first elseif statement is working great! However if just a zip is available in from the $_GET then it doesnt echo the zip code like it is supposed to :( If anyone can help me solve this is would be much appreciated.

Thank you in advance.

Matthew1980

9:28 am on Jul 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there princeofvegas,

Shouldn't the last part of that chain be 'else'?

Have you checked to see if there is any value held in the array before trying to echo?

echo "<pre>";
print_r($_GET);
echo "</pre>";

Use that to see exactly whats set & whats not. Also, you really need to perform a check before assigning the $_GET to vars - ie sanitise the data, especially if you intend to use in sql query etc.

Also: echo $fromaddress = $searchzip;// no need for quotes ;)

Hope that helps,

Cheers,
MRb

penders

10:41 am on Jul 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



However if just a zip is available in from the $_GET then it doesnt echo the zip code like it is supposed to...


If just $searchzip is set then the other vars are therefore empty, so your first if() is going to evaluate to true....
if((empty($searchaddress)) || (empty($searchcity)) || (empty($searchstate))){ 
$fromaddress = "";


You need to check your $searchzip first.

But your logic does look a bit strange, are you sure this is what you want to do...
- If any one of the first 3 fields are empty then the whole address is empty.
- Otherwise if ALL 3 fields are set then the address is a combination of ALL 3 fields.
No halfway measure, all or nothing. These 2 statements cover every eventuality so your code is never going to reach any additional else/elseif clauses.

Actually, you might be OK if you 'AND' your first clause... if ALL 3 fields are empty ie.
if((empty($searchaddress)) && (empty($searchcity)) && (empty($searchstate))){ 
$fromaddress = "";

rocknbil

5:47 pm on Jul 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this all you're doing? echoing these? What if the values aren't set in $_GET? What about a basic cleansing of input?


$input = Array(
'searchaddress' => 'a-z\s\d\'\.\,',
'searchcity' => , 'a-z\s',
'searchstate' => , 'a-z',
'searchzip' => '\d\-'
);
//
$fromaddress = null;
//
foreach ($input as field => $regex) {
if (isset($_GET[$field])) {
$_GET[$field] = preg_replace("/[^$regex]+/i", '', $_GET[$field]);
if (! empty($GET[$field])) { $fromaddress .= $_GET[$field] . ' ' ; }
}
}
echo "<p>$fromaddress</p>";

princeofvegas

5:54 pm on Jul 7, 2010 (gmt 0)

10+ Year Member



Hello All. Thank you very much for your help guys. I am still new and learning PHP so it is great to have people like you help out. Rocknbil.. that worked perfectly and did exactly what I wanted it to do. Last night before I went to sleep I did get the script to do what I wanted by using this:
if((empty($searchaddress)) || (empty($searchcity)) || (empty($searchstate)) && (isset($searchzip))){
$fromaddress = $searchzip;
}elseif((isset($searchaddress)) && (isset($searchcity)) && (isset($searchstate))){
$fromaddress = "$searchaddress $searchcity $searchstate";
}else{
$fromaddress = "";
}


I know it is messy however it did work, although it is great to know the proper way to do it now.

rocknbil

12:56 am on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, my only real point was that if you do this,

$var = $_GET['some-field'];

and some-field is not set, you will get

undefined index 'some-field' at line 1234

and $var will be empty no matter what you do. Untrapped conditions are a great way to build a security hole, sooner or later.

if (isset($_GET[$field])) {
// NOW do something . . .
}

No harm in a little optimizing and cleansing along the way. :-)