php - $params->set Array between square bracket -
i'm using k2 in joomla 3.3. i'm trying set params (items ids ) module k2_content item.php file. result must between brackets, like:
["96","68"]
my code is:
$query = "select * #__k2_items extra_fields_search = '$myautor' , catid !=1 " ; $db->setquery($query); $losautores = $db->loadobjectlist(); $result = array(); foreach ($losautores $key => $value) { $result[] = '" '.$value->id.' "'; } $string_version = implode(',', $result); $autoresfinal = '['.$string_version.']';
if test using print, looks ok. passing var pramas, 1064 error.
$params->set('items', $autoresfinal);
to test tried
$autoresfinal = ["96","68"];
and works fine. idea why doesn't work? thank you.
if assign ["x","y"]
assigning an array. here transforming array in string.
try simply
$result = [ ]; foreach ($db->loadobjectlist() $key => $value) { $result[] = $value->id; } $params->set('items', $result);
also, if wanted convert array string (possibly json), faster , safer way use json_encode
(with appropriate options).
update
the above remains true, had missed complaint error 1064. sql syntax error , happens before encode results.
the reason - noticed fred -ii- - in query, #__k2_items
needs escaping backticks:
$query = "select * #__k2_items extra_fields_search = '$myautor' , catid !=1 " ;
should be:
$query = "select * `#__k2_items` extra_fields_search = '$myautor' , catid !=1 " ;
also, want use prepared statements , parameterized queries (find example here) instead of plugging $myautor
string. if had author called d'artagnan, query become
....search = 'd'artagnan' , ...
which again fail. or if called author ' or ''=', query become
...search = '' or ''='' , ...
which, since '' equal '', match all records in table.
Comments
Post a Comment