我是CodeIgniter和HMVC框架的新手,我正在创建一个示例测试图像下拉列表,以获取有关如何解决本周应完成的大型项目的蓝图。我的问题是,如何使用下拉列表以表格式搜索和显示过滤后的数据库值?例如,如果我选择下拉列表中的值之一并单击“搜索”,则所有检索到的值将被过滤并根据下拉列表中选择的值显示。这是图像下拉菜单的示例测试代码的代码:
这是我模型中的代码:
class Image_model extends CI_Model{
function __construct(){
parent::__construct();
}
/*Sample test model function for the image dropdown list*/
//display the images table
public function displayTableImages()
{
$query = $this->db->select('main_image, main_image_url');
$query = $this->db->from('product_master');
$query = $this->db->get();
return $query->result();
}
//dropdown search list for images
public function searchDropdownImages($type)
{
switch ($type) {
case 'all':
{
$query = $this->db->where("main_image != '', 'main_image_url != ''");
}
case 'with-image':
{
$query = $this->db->where("'main_image != '', 'main_image_url != ''");
}
break;
case 'no-image':
{
$query = $this->db->where("'main_image = ''", "'main_image_url = ''");
}
break;
default:
$query = $this->db->select('main_image', 'main_image_url');
$query = $this->db->from('product_master');
break;
if ($query->num_rows()) {
return $query->result();
}
}
}
/*End sample test function*/
}
这是我的控制器的代码:
if(! defined('BASEPATH')) exit('No direct script access allowed');
class Sample_image_dropdown extends MX_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->display_table_images();
}
public function display_table_images()
{
$this->load->model('Sample_model/image_model');
$data['images'] = $this->image_model->displayTableImages();
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->load->view('sample_view/image_dropdown_view', $data);
}
public function search_dropdown_images($type)
{
$this->load->model('Sample_model/image_model');
$type['dropdown_images'] = $this->input->post('type');
switch ($type) {
case 'all':
$data['images'] = $this->image_model->get('all');
break;
case 'with-image':
$data['images'] = $this->image_model->get('with-image');
break;
case 'no-image':
$data['images'] = $this->image_model->get('no-image');
break;
default:
echo 'There are no images to be returned';
break;
}
$this->load->view('sample_view/image_dropdown_view', $type);
}
}
这是我认为的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<div>
<?php echo "<select name='type' id='type'>
<option value='all'>All Image</option>
<option value='with-image'>With Image</option>
<option value='no-image'>Without Image</option>
</select>" ?>
<?php echo "<input type='submit' value='Search'>"; ?>
</div>
<div>
<h3>Images</h3>
</div>
<div>
<?php if (isset($images)): ?>
<?php foreach($images as $image): ?>
<table border="1">
<tr>
<td><?php echo "$image->main_image"; ?></td>
<td><?php echo "$image->main_image_url"; ?></td>
</tr>
</table>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</body>
</html>
对于您可以就我的问题提供的任何帮助/建议,我将非常高兴。
答案 0 :(得分:2)
您应该为此使用AJAX。 首先,您必须将一个查询从ajax发送到调用控制器,然后使用该控制器从数据库加载数据,并将其作为HTML块发送到视图并显示它。 并在链接到ajax的下拉列表中创建一个OnChange事件 希望这可以帮助您
答案 1 :(得分:0)
使用分页库和get方法来过滤数据... get方法将帮助您创建base_url。 您需要做的就是在where语句中使用get数据。您面临的问题是,当您转到另一个页面时,您丢失了所选的选项。 在控制器中 写入孔后,只需更改base_url以配置分页并将其设置为
if(isset($_GET))
{
$config['first_url'] = $config['base_url'].'/1'.'?'.http_build_query($_GET, '', "&");
}
这将有助于生成正确的链接 并从您的角度来看..获得所选选项的所选值。
<select name='type' id='type'>
<option value='all' <?php echo (isset($_get['type']) && $_get['type'] == 'with-image')?'selected':''; ?> >
All Image
</option>
<option value='with-image' <?php echo (isset($_get['type']) && $_get['type'] == 'with-image')?'selected':''; ?> >
With Image
</option>
<option value='no-image' <?php echo (isset($_get['type']) && $_get['type'] == 'no-image')?'selected':''; ?> >
Without Image
</option>
</select>