afaaro Posted March 20, 2014 Share Posted March 20, 2014 (edited) 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 March 20, 2014 by afaaro Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 20, 2014 Share Posted March 20, 2014 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>"; } } } Quote Link to comment Share on other sites More sharing options...
afaaro Posted March 20, 2014 Author Share Posted March 20, 2014 Thank you Ch0cu3r It works but if i want to add date like this it doesn't work getCatid(array('post_enabled'=>'1','post_featured'=>'0', 'post_created'=>'<=UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 DAY))')); Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 20, 2014 Share Posted March 20, 2014 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); } Quote Link to comment Share on other sites More sharing options...
Solution afaaro Posted March 20, 2014 Author Solution Share Posted March 20, 2014 Thank you so much Ch0cu3r You are really a star Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.