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. 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.
main_imageand themain_image_urlcolumns to display as a table in HTML and CodeIgniter. My question is though is how can I be able to use the dropdown list in order to search for the images in the table by using the value that I selected in the dropdown list?