【发布时间】:2020-03-04 19:10:57
【问题描述】:
我需要一份关于在 Codeigniter 中使用 ajax 获取结果数据库的经验指导。
实际上我是从表中使用 category_id 获取图像,重要的是 category_id 可以不止一个,因为我们的表中有多个 category_id
我的方法是我使用 select 发送 category_id 并且该 id 通过 ajax 发送然后获取结果,但我在数组中获取结果我不明白如何在我的视图中显示该数据。
my_view 功能
函数 productData(product_id) { var product_id = $("#product_name").val();
$.ajax({ url: base_url + 'products/getProductValueById', type: 'post', data: {product_id : product_id}, dataType: 'json', success:function(response) { console.log(response); } // /success }); // /ajax function to fetch the product data }
我的数据库查询
公共函数 getProductDataByCategoryId($id = null) { 如果(!$id) { 返回假; }
$sql = "SELECT products.image FROM products WHERE products.category_id = $id"; $query = $this->db->query($sql, array($id)); return $query->result_array(); }
我发送和接收数据的模型函数
公共函数 getProductValueById() { $product_id = $this->input->post('product_id'); // 返回 print_r($product_id); 如果($product_id){ $product_data = $this->model_products->getProductDataByCategoryId($product_id);
// print_r($product_data); echo json_encode($product_data); } }
【问题讨论】:
-
这个问题主要不适合这个网站。您可以表达您面临的确切问题或查看官方文档。
-
您可以使用联系表单 7 插件并应用 css 来看起来像您的引导表单。
标签: php jquery ajax codeigniter