OpenCart 将数组返回到 JSON 并传递给 jQuery

Posted

技术标签:

【中文标题】OpenCart 将数组返回到 JSON 并传递给 jQuery【英文标题】:OpenCart return array to JSON and pass to jQuery 【发布时间】:2018-04-24 13:33:05 【问题描述】:

我在 jQuery 中返回数组时遇到问题。我使用函数来响应来自 mysql 的数据,我必须添加它做数组并返回查看。就像幻灯片一样,每 5 秒显示一次不同的评论。

这是我的代码: 控制器:

 $this->load->model('catalog/review');
    $current_store = $this->config->get('config_store_id');


    $feedbacks = $this->model_catalog_review->getFeedbacksByStore($current_store);


        $this->data['feedbackscrazys'][] = array(
            'feedback_name'  => $feedbacks['form_name'],
            'feedback_text'  => $feedbacks['feedback'],
        );

型号:

  public function getFeedbacksByStore($id) 
    $sql = "SELECT * FROM " . DB_PREFIX . "feedbackcrazy";
    $sql .= " WHERE shop_id = ".$id." AND show_index=1";
    $sql .= " ORDER BY RAND() LIMIT 10";
    $query = $this->db->query($sql);

    if($query->num_rows > 0) 
        return $query->row;
     else 
        return 0;
    



和视图:

var feedbacks = function() 
// here i want to replace this code with the results of array
var jsontext ='["feedback_author":"Vesela Chobanova","feedback_text"  : "Thanks for the quick delivery and the beautiful clothes! :):)","feedback_author": "Dimitar Nedelchev","feedback_text"  : "You are great! You are one of the few to give sincere and unexpected rewards! Thank you very much!","feedback_author": "Bojidara Karajorova","feedback_text"  : "Thank you for your service responsiveness :)","feedback_author": "Maria Rizova","feedback_text"  : "Thank you very much for the Childrens Gold Contrast Gown. She is very beautiful !! thank you very much .","feedback_author": "Violeta Stefanova","feedback_text"  : "Hello, I ordered several times from Crazy kids. I am very pleased with both the quality of clothes and the service. When I need advice, I always get full co-operation. Thanks!","feedback_author": "Maria Hristova","feedback_text"  : "Excellent quality! Very good attitude and full cooperation. thanks ","feedback_author": "Stefka Mihova","feedback_text"  : "Thanks to the quick delivery and the amazing Polish hats. I expect a further load from them","feedback_author": "Daniela Kosova","feedback_text"  : "The delivery was super fast. Thanks. We are very pleased with the clothes we received","feedback_author": "Silvia Purvanova","feedback_text"  : "The dress I received was amazing. Thanks for the quick delivery and the wonderful attitude on the phone"]';
var json = JSON.parse(jsontext);
var i = 0;
var fnchange = function() 
    $('#footerfeedbackItemContent').animate('opacity': 0, 2000, function () 
        $(this).text(json[i]['feedback_text']);
    ).animate('opacity': 1, 2500);

    $('#footerfeedbackItemCustomer').animate('opacity': 0, 2000, function () 
        $(this).text(json[i]['feedback_author']);
    ).animate('opacity': 1, 2500);

    if( ++i < json.length )
        setTimeout(fnchange, 10000);
     else 
        i = 0;
        setTimeout(fnchange, 10000);
    
;
setTimeout(fnchange, 1);


;
setTimeout(feedbacks,1);

【问题讨论】:

【参考方案1】:

我不确定你的 opencart 购物车版本,但试试这个

public function myData()
     $this->load->model('catalog/review');
    $current_store = $this->config->get('config_store_id');
    $feedbacks = $this->model_catalog_review->getFeedbacksByStore($current_store);

        $this->data['feedbackscrazys'][] = array(
            'feedback_name'  => $feedbacks['form_name'],
            'feedback_text'  => $feedbacks['feedback'],
        );
      //add this line
     json_encode($this->data);

     //if opencart 2.3.x+
    //$this->response->addHeader('Content-Type: application/json');
    //$this->response->setOutput(json_encode($data))  
   

这是在 OpenCart 2.3.x + 中获取典型 JSON 数据的示例

public function about()
    
        $data['name'] = $this->config->get('config_name');
        $data['version'] = "OpenCart ".VERSION;
        //$data['name'] = $this->config->get('config_name');
        $data['config_address'] = $this->config->get('config_address');
        $data['config_telephone'] = $this->config->get('config_telephone');
        $data['config_address'] = $this->config->get('config_address');
        $data['config_currency'] = $this->config->get('config_currency');
        //the last two lines will return json data
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($data));
    

【讨论】:

如果有办法停止缓存这个查询...或以其他方式做...

以上是关于OpenCart 将数组返回到 JSON 并传递给 jQuery的主要内容,如果未能解决你的问题,请参考以下文章

Vue.js - 如何将 prop 作为 json 数组传递并在子组件中正确使用?

如何将对象类型的 json 数组传递给 MVC 控制器类

如何将 JSON 数组(或 JSON 数组)传递给 pg 函数以插入到表中?

如何将数组从 c++ 传递给 python 函数并将 python 返回的数组检索到 c++

如何将 opencv Mat 类型传递给 Python 并返回一个数组?

将列表/数组作为参数/返回类型传递并返回给 Redshift 中的 UDF