Jump to content

Array in function need help


afaaro

Recommended Posts

I need help querying array in function its not working

function getCatid($params=false){
	if(is_array($params)){
		foreach ($params as $key => $value) {
			$inputdata = $value ? "$key" : $key;
		}
	}else{
		$inputdata = "0";
	}
    if($result = GetList("SELECT * FROM ".DB_PREFIX."mediaPost WHERE $inputdata ORDER BY p.post_created DESC LIMIT 20")){
        	foreach ($result as $data) {
        		echo $data['post_name']."<br>";
        	}
    }	
}
	
getCatid(array("post_enabled"=>"1", "post_featured"=>"0"));
Link to comment
https://forums.phpfreaks.com/topic/287116-array-in-function-need-help/
Share on other sites

Are you using the array to construct the where clause, eg  array("post_enabled"=>"1", "post_featured"=>"0")

 

should yield the following query to be performed by your function

SELECT * FROM ".DB_PREFIX."mediaPost WHERE post_enabled = 1 AND post_featured = 0 ORDER BY p.post_created DESC LIMIT 20

You can accomplish this using

function getCatid($params=false) {

    // start to define the query
    $sql = 'SELECT * FROM '.DB_PREFIX.'mediaPost ';

    // generated the where clause
    if(is_array($params)) {
        $sql .= 'WHERE ';
        foreach ($params as $field => $value) {
            $sql .= "$field = $value AND "; // add the field, value pair to the query
        }
        $sql = substr($sql, 0, -4); // remove 'AND ' from the end of generated query
    }

    // append the rest of the query
    $sql .= 'ORDER BY p.post_created DESC LIMIT 20';

    // pass the generated query to GetList()
    if($result = GetList($sql)){
            foreach ($result as $data) {
                echo $data['post_name']."<br>";
            }
    }   
}

That will generate this query

SELECT * FROM DB_PREFIXmediaPost WHERE post_enabled = 1 AND... post_created = <=UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 DAY)) ... LIMIT 20

Notice the = <= bit. This is invalid SQL syntax. This caused due to my code hard-coding the comparison operator to a = and the fact you have included a different operator as part of the field value.
 
The function does not have the logic to substitute the = with <=.  I think rather than passing in an array of field, value pairs instead just pass in array of combined field, value pairs eg
 
getCatid(array('post_enabled = 1', 'post_featured = 0', 'post_created <= UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 DAY))'));
 
So change

    // generated the where clause
    if(is_array($params)) {
        $sql .= 'WHERE ';
        foreach ($params as $field_value) {
            $sql .= "$field_value AND ";
        }
        $sql = substr($sql, 0, -4); // remove 'AND ' from the end of generated query
    }

To just

if(is_array($params)) {
    $sql .= 'WHERE ' . implode(' AND ', $params);
}

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.