我有以下代码根据所选位置加载网站中的数据,更改ajax被激活并在控制器中获取值,从控制器到模型并进入sql的where子句,sql的结果未显示在网站上。但总是进入模型
的其他循环查看:
$(document).on('change','#delvloc',function(e){
e.preventDefault(); // stops the jump when an anchor clicked.
var title = this.value; // anchors do have text not values.
$.ajax({
url: 'index.php?route=product/category/newfunc',
data: {'title': title}, // change this to send js object
type: "get",
success: function(data){
//document.write(data); just do not use document.write
console.log(data);
}
});
});
控制器:
public function newfunc(){
$data = array(
'title' => $this->request->get['title']
);
$this->load->model('catalog/product');
$this->model_catalog_product->getProducts($data);
}
型号:
public function getProducts($data = array()) {
$sql="SELECT p.product_id,p.city, (SELECT AVG(rating) AS total FROM monster_review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM monster_product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '1' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM monster_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special FROM monster_product p LEFT JOIN monster_product_description pd ON (p.product_id = pd.product_id) LEFT JOIN monster_product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0'"
if (!empty($data['title'])) {
$sql .= " and p.city='". $data['title']. "' GROUP BY p.product_id";
}
else{$sql .= " and p.city='Pune' GROUP BY p.product_id";}
}
我的HTML:
<select name="delvloc" id="delvloc" style="width: 100px;" ">
<?php foreach ($city as $user_group) { ?>
<?php if ($user_group['city'] == $user_group_id) { ?>
<option value="<?php echo $user_group['user_group_id']; ?>" selected="selected"><?php echo $user_group['city']; ?></option>
<?php } else { ?>
<option value="<?php echo $user_group['city']; ?>"><?php echo $user_group['city']; ?></option>
<?php } ?>
<?php } ?>
</select>
</li>
答案 0 :(得分:0)
在模型中
function getProducts($data) {
$sql="SELECT p.product_id,p.city, (SELECT AVG(rating) AS total FROM monster_review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM monster_product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '1' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM monster_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special FROM monster_product p LEFT JOIN monster_product_description pd ON (p.product_id = pd.product_id) LEFT JOIN monster_product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0'"; # Mssing ; here
if (!empty($data['title']))
{
$sql .= " and p.city='". $data['title']. "' GROUP BY p.product_id";
}
else{
$sql .= " and p.city='Pune' GROUP BY p.product_id";
}
$query = $this->db->query($sql);
$result = $query->result_array();
return $result;
}
在控制器中
function newfunc()
{
$data = array(
'title' => $this->request->get['title']
);
$this->load->model('catalog/product');
$result = $this->model_catalog_product->getProducts($data);
$response['data'] = '';
if (empty($result))
{
$response['data'] = false;
}
else {
$response['data'] = $result;
}
echo json_encode($response);
}