afaaro Posted March 20, 2014 Share Posted March 20, 2014 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 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>"; } } } Link to comment https://forums.phpfreaks.com/topic/287116-array-in-function-need-help/#findComment-1473286 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))')); Link to comment https://forums.phpfreaks.com/topic/287116-array-in-function-need-help/#findComment-1473290 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); } Link to comment https://forums.phpfreaks.com/topic/287116-array-in-function-need-help/#findComment-1473299 Share on other sites More sharing options...
afaaro Posted March 20, 2014 Author Share Posted March 20, 2014 Thank you so much Ch0cu3r You are really a star Link to comment https://forums.phpfreaks.com/topic/287116-array-in-function-need-help/#findComment-1473314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.