Welcome Guest, Not a member yet? Register   Sign In
Help Me... Search with dropdown menu or radio button
#1

[eluser]myrna[/eluser]
I'm making a search function.

Model

Code:
<?php
    class PencarianModel extends Model
    {    
        function PencarianModel()
        {
            parent::Model();
        }    
        
        function SearchResult($perPage,$uri,$judul)
        {
            $this->db->select('*');
            $this->db->from('tbl_master_properti');
            if(!empty($judul)) {
                $this->db->like('judul',$judul);
            }
            $this->db->order_by('id_master_properti','asc');
            $getData = $this->db->get('', $perPage, $uri);
            
            if($getData->num_rows() > 0)
                return $getData->result_array();
            else
                return null;
        }
    }
?>


controller
Code:
<?php
    class Search extends Controller
    {    
        function Search()
        {
            parent::Controller();    
        }
        
        function index()
        {
            if(isset($_POST['submit']))
            {
                $data['judul'] = $this->input->post('judul');
                $this->session->set_userdata('sess_judul', $data['judul']);
            } else {
                $data['judul'] = $this->session->userdata('sess_judul');
            }
            
            $this->db->like('judul', $data['judul']);
            $this->db->from('tbl_master_properti');
            
            $pagination['base_url']=base_url().'index.php/ulang37/index/page/';
            $pagination['total_rows']     = $this->db->count_all_results();
            $pagination['full_tag_open'] = "<p><div class=\"pagination\">";
            $pagination['full_tag_close'] = "</div></p>";
            $pagination['cur_tag_open'] = "<span class=\"current\">";
            $pagination['cur_tag_close'] = "</span>";
            $pagination['num_tag_open'] = "<span class=\"disabled\">";
            $pagination['num_tag_close'] = "</span>";
            $pagination['per_page']     = "100";
            $pagination['uri_segment'] = 4;
            $pagination['num_links']     = 4;
            
            $this->pagination->initialize($pagination);
            
            $data['ListBerita'] = $this->pencarianmodel->SearchResult($pagination['per_page'],$this->uri->segment(4,0),$data['judul']);
            $data['current'] = 'search';
            $data['content'] = 'pencarian';
            $this->load->vars($data);
            $this->load->view('container', $data);
            }
    }
?&gt;

view

Code:
<div class="clear"></div>
<div id="content_box" class="grid_24 box">
&lt;form id="form_pencarian" name="form_pencarian" method="post" action="&lt;?=base_url();?&gt;/search/"&gt;
  &lt;input name="judul" type="text" id="judul" size="30" maxlength="255" class="inputbox" value="&lt;?=$judul;?&gt;"/&gt;&nbsp;&nbsp;
  &lt;input name="submit" type="submit" id="submit" value="Cari" class="button"/&gt;
&lt;/form&gt;
<hr />
&lt;?php
    
    if(count($ListBerita) > 0) {
        foreach($ListBerita as $row)
        {
            echo "<div class=\"letter\">";
            echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
                <tbody>";
            echo "<tr>
                        <td width=32 align=\"center\" valign=\"middle\">&nbsp;</td>
                        <td width=500>
                        <span>". $row['pengarang'] ."</span><br/>
                        <a >".$row['id_kategori']."</a>
                        <div> ".$row['judul']."</div>
                        </td>
                </tr>";
            echo "</tbody></table>";    
            echo "</div>";
        }
        echo $this->pagination->create_links();
    } else {
        echo "<div><strong>No result.</strong></div>";
    }
?&gt;
</div>
<div class="clear"></div>
&lt;/body&gt;
&lt;/html&gt;

If I want to add a dropdown menu or radio buttons on my program. what should I add to my program?
Code:
$criteria = array();
$query = "";

$i = 0;
if (!empty($judul))
$criteria[$i++] = "judul LIKE $judul";

if (!empty($editor))
$criteria[$i++] = "editor LIKE $editor";

if (!empty($pengarang))
$criteria[$i++] = "pengarang LIKE $pengarang";

$query = $criteria[0];
for ($i = 1; $i < size($criteria) - 1; $i++)
{
$query .= " AND " + $criteria[$i];
}

echo $query;


If I want to add the code above. what must I change on the model, view and controllernya?

Thanks for your help.
#2

[eluser]Flemming[/eluser]
Hmmm ... how did you get so far and then not know how to add extra criteria to your search?

anyway - hopefully this will get you started (it isn't complete code, only enough to give you some clues!)

Model
Code:
function SearchResult($perPage,$uri,$judul,$editor,$pengarang)
{
    $this->db->select('*');
    $this->db->from('tbl_master_properti');
    if(!empty($judul)) {
        $this->db->like('judul',$judul);
    }
    if(!empty($editory)) {
        $this->db->like('editor',$editor);
    }
    if(!empty($pengarang)) {
        $this->db->like('pengarang',$pengarang);
    }

controller
Code:
function index()
{
    if(isset($_POST['submit']))
    {
        $data['judul'] = $this->input->post('judul');
        $this->session->set_userdata('sess_judul', $data['judul']);
        $data['editor'] = $this->input->post('editor');
        $this->session->set_userdata('sess_editor', $data['editor']);
        $data['pengarang'] = $this->input->post('pengarang');
        $this->session->set_userdata('sess_pengarang', $data['pengarang']);
        
    //...
    
    // pass the extra post data to your model
    $data['ListBerita'] = $this->pencarianmodel->SearchResult($pagination['per_page'],$this->uri->segment(4,0),$data['judul'],$data['editor'],$data['pengarang']);
    
    //...
    }

view
Code:
&lt;input name="editor" type="text" id="editor" size="30" maxlength="255" class="inputbox" value="&lt;?=$editor;?&gt;"/&gt;

&lt;!-- or for a select (dropdown): --&gt;

<select name="editor" id="editor">
    <option value="some_value">Editor One</option>
    <option value="some_value2">Editor Two</option>
    <option value="some_value3">Editor Three</option>
</select>
#3

[eluser]myrna[/eluser]
Thanks for the help, Flemming. I actually just learning CI and I'm making a final project in college and my teacher suggested to use the CI. So, I have yet to understand in depth about the CI and the programs that I wrote above is a program from others that my modifications.

If I have any trouble may I ask you?

If you have any examples of the search function?

Thank you, sorry with my english not perfect.
#4

[eluser]Flemming[/eluser]
yes, ask any questions and I or somebody else should be able to help.

just make sure you have a clear understanding of the basics of CI and MVC.

Try to keep things in the right place, then it will be easier for us to help you! :-)




Theme © iAndrew 2016 - Forum software by © MyBB