查看:尝试根据长度,宽度和高度检索项目。
<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 file:
$("#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"}
答案 0 :(得分:1)
本节存在问题
foreach($one_exp as $exp) {
$html = $exp->width . $exp->color . $exp->filename;
}
每次初始化变量$html
所以进行更新
foreach($one_exp as $exp) {
$html .= $exp->width . $exp->color . $exp->filename."<br>";
}
答案 1 :(得分:0)
就查询而言,您可以根据参数创建查询字符串,例如:
function one_exp($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);
/* do query */
}
确保至少有一个参数具有有效值