gpt4 book ai didi

php - Codeigniter 事件记录类(从哪里选择)

转载 作者:行者123 更新时间:2023-11-30 23:04:41 26 4
gpt4 key购买 nike

只是想让 codeigniter 过滤器表单工作。例如,如果将从下拉列表中选择“Samsung”并从复选框字段中选择“2G”,则应返回 ID 为 1 和 4 的行。但是我的模型返回空数组。请帮助我。

这是我的数据库表:

database table

这是我的过滤器形式:

filter form

这是我的模型:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_example extends CI_Model {

function __construct()
{
parent::__construct();
}

public function did_filter() {


$types = $this->input->post('types');

$data = array(
'2g' => 0,
'3g' => 0,
'4g' => 0,
);
foreach ($types as $type) {
$data[$type] == 1;
}
$this->db->select('*');
$this->db->from('table_example');
$this->db->where('phone', $this->input->post('phone'));
$this->db->where((
(('2g' == 1) AND ($data['2g'] == 1)) OR
(('3g' == 1) AND ($data['3g'] == 1)) OR
(('4g' == 1) AND ($data['4g'] == 1))
),NULL, FALSE );
if (
$query = $this->db->get())
{
return $result = $query->row_array();
}
else {
return false;
}
}
}

这是我的观点 1:

    <?php 

$this->load->helper("form","file");

echo validation_errors();

echo form_open_multipart("example/search");

echo form_label("Phone:<br>","phone");
$data = array(
"" => "Select Phone",
"samsung" => "Samsung",
"htc" => "HTC",
"nokia" => "Nokia",
);
echo form_dropdown('phone', $data, set_value('phone'));

echo br(5);
?>

<?php echo form_label("Network Type:<br>","type");?>
<input type="checkbox" name="types[]" value="2g" id="types" <?php echo set_checkbox('types[]', '2g', FALSE); ?>/>2G<br />
<input type="checkbox" name="types[]" value="3g" id="types" <?php echo set_checkbox('types[]', '3g', FALSE); ?>/>3G<br />
<input type="checkbox" name="types[]" value="4g" id="types" <?php echo set_checkbox('types[]', '4g', FALSE); ?>/>4G<br />
<br />
<?php

echo br(1);

echo form_submit("filterSearch", "Search");

echo form_close();

?>

这是我的观点 2:

<?php 

print_r($result);

这是我的 Controller :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Example extends MX_Controller {

public function index() { //main function

$this->load->view("view_example");
}

public function search() {

$this->load->library('form_validation');
$this->load->model('model_example');

$this->form_validation->set_rules('phone', 'Phone','required');
$this->form_validation->set_rules('types[]', 'Network Type','required');

if($this->form_validation->run()) {

$data["result"] = $this->model_example->did_filter();


$this->load->view("view_search_results",$data);

}
else
{

$this->load->view("view_no_search_results");

}

}
}

最佳答案

public function did_filter($phone, $2g, $3g, $4g){

//setup query
$this->db->select('id, phone, 2g, 3g, 4g')
->from('example_table')
->where('phone', $phone)
->where('2g', $2g)
->where('3g', $3g)
->where('4g', $4g);

//execute query
$query = $this->db->get();

//fetch result
return $result = $query->row_array();
}

这将返回一个结果数组。

解决使用问题

echo $this->db->last_query;

然后在 phpMyAdmin 中运行生成的字符串以帮助调试。

同时修复您的 HTML 输入。

<input type="checkbox" name="2g" value="1" id="types" />2G<br />
<input type="checkbox" name="3g" value="1" id="types" />3G<br />
<input type="checkbox" name="4g" value="1" id="types" />4G<br />

关于php - Codeigniter 事件记录类(从哪里选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22385384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com