視圖:試圖根據長度,寬度和高度檢索項目。從數據庫返回帶有ajax的多行 - Codeigniter
<form method="post" action="">
<label for="title">L</label>
<input type="text" name="length" id="length" value="" />
</br>
<label for="title">W</label>
<input type="text" name="width" id="width" value="" />
</br>
<label for="title">H</label>
<input type="text" name="height" id="height" value="" />
<input type="submit" name="submit" id="submit" />
</form>
js文件:
$("#submit").click(function (e) {
event.preventDefault();
var length = $('#length').val();
var width = $('#width').val();
var height = $('#height').val();
$.ajax({
url: "http://domain.com/index.php/welcome/get_items",
data: {
length: length, width: width, height: height
}
}).done(function(data) {
$('div#cont').text(data.content);
}).fail(function() {
alert('Failed!')
});
});
控制器:我不知道如何來構建這個。如果只輸入一個值,則應該查找該值,如果兩個值匹配具有兩個維度的行,並且如果全部三個都存在,則相同。我是否檢查並查看給定的值,並創建多個模型函數?
我似乎也有一個問題,下面的foreach語句。它不會返回多行。
public function get_items()
{
$this->load->model('home_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('length', 'Length', 'trim|required|xss_clean');
$this->form_validation->set_rules('width', 'Width', 'trim|required|xss_clean');
$this->form_validation->set_rules('height', 'Height', 'trim|required|xss_clean');
$length = $_GET['length'];
$width = $_GET['width'];
$height = $_GET['height'];
$one_exp = $this->home_model->one_exp($length, $width, $height);
if($one_exp != false){
//$html = '<ul>';
foreach($one_exp as $exp) {
$html = $exp->width . $exp->color . $exp->filename;
}
//$html .= '</ul>';
$result = array('status' => 'ok', 'content' => $html);
header('Content-type: application/json');
echo json_encode($result);
exit();
}else{
$result = array('status' => 'no', 'content' => 'nothing here');
header('Content-type: application/json');
echo json_encode($result);
exit();
}
}
型號:以下功能僅供測試。
function one_exp($length, $width, $height) {
$query_str = "SELECT * FROM files WHERE length =? || width=? || height=?";
$query = $this->db->query($query_str, array($length, $width, $height));
if($query->num_rows() > 0) {
foreach($query->result() as $item) {
$data[] = $item;
}
return $data;
} else {
return false;
}
}
*編輯
function one_exp($length, $width, $height) {
//$query_str = "SELECT * FROM files WHERE length =? || width=? || height=?";
$query_str = "SELECT * FROM files WHERE ";
$query_and=array();
if(!empty($length)){
$query_and[]= ' length =?';
}
if(!empty($width)){
$query_and[]= ' width=?';
}
if(!empty($height)){
$query_and[]= ' height=?';
}
$query_str .= implode(' AND ', $query_and);
$query = $this->db->query($query_str, array($length, $width, $height));
if($query->num_rows() > 0) {
foreach($query->result() as $item) {
$data[] = $item;
}
return $data;
} else {
return false;
}
}
響應
Array
(
[0] => width=?
[1] => height=?
)
string(7) "Width50"
string(8) "Height60"
SELECT * FROM files WHERE width=''AND height='50'{"status":"no","content":"nothing here"}
我的回答是h elpful ??? –
你不需要'status:ok/no'。響應的'200 OK'HTTP狀態碼就足夠了 - 如果沒有要顯示的記錄,只需返回'[]'。 – moonwave99
@ moonwave99我該如何檢查?你能告訴我一個例子嗎? – Ciprian