Jump to content

Array in function need help


afaaro
Go to solution Solved by 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"));
Edited by afaaro
Link to comment
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>";
            }
    }   
}
Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.