duancuo1234 2014-09-23 19:33
浏览 30
已采纳

$ params-> set方括号之间的数组

i'm using K2 in Joomla 3.3. I'm trying to set params (items ids ) to module k2_content from item.php file. The result must to be between brackets, something like:

["96","68"]

My code is:

        $query = "SELECT   *  FROM #__k2_items WHERE  extra_fields_search = '$myautor' AND catid !=1 " ;
        $db->setQuery($query);
        $losautores = $db->loadObjectList();
        $result = array();
        foreach ($losautores as $key => $value) {
       $result[] = '" '.$value->id.' "';
       }

      $string_version = implode(',', $result);
     $autoresfinal = '['.$string_version.']';

If i test using print, looks ok. But passing the var to pramas, i get 1064 error.

$params->set('items', $autoresfinal);

To test I tried

$autoresfinal = ["96","68"];

And works fine. Any idea why doesn't work? Thank you.

  • 写回答

1条回答 默认 最新

  • doulu8446 2014-09-23 19:37
    关注

    If you assign ["x","y"] you are assigning an array. Here you are transforming the array in a string.

    Try simply

       $result = [ ];
       foreach ($db->loadObjectList() as $key => $value) {
           $result[] = $value->id;
       }
       $params->set('items', $result);
    

    Also, if you wanted to convert the array into a string (possibly JSON), a faster and safer way is to use json_encode (with the appropriate options).

    UPDATE

    The above remains true, but I had missed your complaint about error 1064. That is a SQL syntax error and it happens before you encode the results.

    The reason - as noticed by Fred -ii- - is that in this query, #__k2_items needs escaping with backticks:

     $query = "SELECT   *  FROM #__k2_items WHERE  
                 extra_fields_search = '$myautor' AND catid !=1 " ;
    

    should be:

     $query = "SELECT   *  FROM `#__k2_items` WHERE  
                 extra_fields_search = '$myautor' AND catid !=1 " ;
    

    Also, you probably want to use prepared statements and parameterized queries (find an example here) instead of just plugging $myautor into a string. If you had an author called D'Artagnan, the query would become

    ....search = 'D'Artagnan' AND ...
    

    which would again fail. Or if I called an author ' OR ''=', the query would become

    ...search = '' OR ''='' AND ...
    

    which, since '' is always equal to '', would match for all the records in your table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题