Jump to content

Can someone please tell me why this code is not working?


maxzyfel

Recommended Posts

Hello, I am Max and I'm new to PHP. I'm trying to generate a query "$fquery" on the fly which will include only the fields which are provided. But for some reason it is not concatenating very well as i want. can some one please look at the code an tell me what is wrong.

 

function get_post($field) {
        if(isset($_POST[$field])) {
            return mysql_real_escape_string($_POST[$field]);
        } else {
            return null;
        }
    }
    

    
    function display_fillters($field1 = 10, $field2, $field3 = 'Greater Accra', $field4, $field5, $field6, $field7, $field8, $field9, $field10){
     
        $limit = get_post($field1);
        $city = get_post($field2);
        $region = get_post($field3);
        $size = get_post($field4);
        $vendor = get_post($field5);
        $faces = get_post($field6);
        $status = get_post($field7);
        $artwork = get_post($field8);
        $recommendation = get_post($field9);
        $illumination = get_post($field10);        


        $fquery = "SELECT * FROM billboards";
        if(isset($city) || isset($region) || isset($size) || isset($vendor) || isset($faces) || isset($status) || isset($artwork) || isset($recommendation) || isset($illumination)) {
            
            $fquery .= " WHERE";
            
            if(isset($region)){
                $fquery .= " Region = '{$region}'";
            }
            if(isset($city)){
                $fquery .= " AND City = '$city'";
            }
            if(isset($size)){
                $fquery .= " AND Size = '$size'";
            }
            if(isset($vendor)){
                $fquery .= " AND Vendor = '$vendor'";
            }
            if(isset($faces)){
                $fquery .= " AND Faces = $faces";
            }
            if(isset($status)){
                $fquery .= " AND Status = '$status'";
            }
            if(isset($artwork)){
                $fquery .= " AND Aartwork = '$artwork'";
            }
            if(isset($recommendation)){
                $fquery .= " AND Recommendation = '$recommendation'";
        	}
            if(isset($illumination)){
                $fquery .= " AND Illumination = '$illumination'";
        	}
    	}    
        $fquery .= " ORDER BY Board_ID LIMIT 0, $limit";
         
        print $fquery;
    }
    
     display_fillters('20', 'Accra', 'Greater Accra', '5m x 10m', 'DDP', '1', 'Done', 'Not Found',  'Recommended', 'Not Lighted');

 

[attachment deleted by admin]

Hi Max could you tell me a bit more? What is it doing or not doing? Are you getting any errors? If so what are they? Nothing is jumping off the page at me as wrong but I am not an expert myself more intermediate beginner if I had to say. But if you give me some more info it might give me a better starting spot.

Couple of things.

 

1. isset() will return true IF the variable is set, and it's value is NOT null.

  So, if any of the 8 fields that  doesn't have a default value, is not assigned a value, you will get a fatal error. Which means all of your arguments will be set.

  To correct this behavior, you could. A: set a default value of each argument as NULL. (Thus allowing the isset() function to do it's job.)

  Or, B: Change the isset() function to empty() function.  This will test if the argument ' ' was passed to it.

 

2. You need to rethink how you are doing this.  If $region is NOT set, then your query will not be built right.  I would suggest putting all of the variables into an array, then imploding.

 

if(isset($city) || isset($region) || isset($size) || isset($vendor) || isset($faces) || isset($status) || isset($artwork) || isset($recommendation) || isset($illumination)) {
            
            $fquery .= " WHERE";
            if(!empty($region)){
                $query[] =  " Region = '{$region}'";
            }
            if(!empty($city)){
                $query[] =  " City = '$city'";
            }
            if(!empty($size)){
                $query[] =  " Size = '$size'";
            }
            if(!empty($vendor)){
                $query[] =  " Vendor = '$vendor'";
            }
            if(!empty($faces)){
                $query[] =  " Faces = $faces";
            }
            if(!empty($status)){
                $query[] =  " Status = '$status'";
            }
            if(!empty($artwork)){
                $query[] =  " Aartwork = '$artwork'";
            }
            if(!empty($recommendation)){
                $query[] =  " Recommendation = '$recommendation'";
		}
            if(!empty($illumination)){
                $query[] =  " Illumination = '$illumination'";
		}
    
$setQuery = implode('AND',$query);


}
$fquery .= " $setQuery  ORDER BY Board_ID LIMIT 0, $limit";

echo $fquery;
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.