简体   繁体   English

如何使用CodeIgniter中的下拉列表在HTML表中显示过滤的数据库值?

[英]How to display filtered database values in HTML table using a dropdown list in CodeIgniter?

I am new to CodeIgniter and the HMVC framework and I am creating a sample test image dropdown in order to get a blueprint on how to solve the big project that I have on due this week. 我是CodeIgniter和HMVC框架的新手,我正在创建一个示例测试图像下拉列表,以获取有关如何解决本周到期的大型项目的蓝图。 My question is, how can I able to search and display the filtered database values in a table format by using a dropdown list? 我的问题是,如何使用下拉列表以表格式搜索和显示过滤后的数据库值? For example, If I select on one of the values in the dropdown list and click search, all of the retrieved values will be filtered and will display according to the value selected in the dropdown list. 例如,如果我选择下拉列表中的值之一并单击“搜索”,则所有检索到的值将被过滤并根据下拉列表中选择的值显示。 Here is the code for the sample test code for the image dropdown: 这是图像下拉菜单的示例测试代码的代码:

Here is the code in my model: 这是我模型中的代码:

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*/
}

Here is the code for my controller: 这是我的控制器的代码:

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);
    }
}

Here is the code in my view: 这是我认为的代码:

<!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>

I will gladly appreciate any help/suggestion you can provide with regards to my question. 对于您对我的问题所能提供的任何帮助/建议,我将不胜感激。

You should use AJAX for this. 您应该为此使用AJAX。 First, you have to send a Query from ajax to call controller then Use the controller to load data from the database and Send it as an HTML block to the view and display it. 首先,您必须将一个查询从ajax发送到调用控制器,然后使用该控制器从数据库加载数据,并将其作为HTML块发送到视图并显示它。 And also make an OnChange event to the drop-down linked to the ajax Hope this one could help you 并在与ajax链接的下拉列表中创建一个OnChange事件,希望这可以为您提供帮助

Use pagination library and get method to filter your data... get method will help you to create base_url. 使用分页库和get方法来过滤数据... get方法将帮助您创建base_url。 All you need to do is use your get data in for where statement. 您需要做的就是在where语句中使用get数据。 the problem you face is when you go to another page you lost your selected option. 您面临的问题是,当您转到另一个页面时,您丢失了所选的选项。 in controller after writing hole thing just change base_url for configure pagination and set it to 在控制器中写完孔后,只需更改base_url以配置分页并将其设置为

if(isset($_GET))
            {
                $config['first_url']        = $config['base_url'].'/1'.'?'.http_build_query($_GET, '', "&");    
            }

it will help to generate proper link and at your view side .. get the selected value for the selected options.. 这将有助于生成正确的链接,并在您的视图侧..获得所选选项的所选值。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM