Welcome Guest, Not a member yet? Register   Sign In
How to pass multiple values from view->controller->model
#1

[eluser]johnmiller[/eluser]
hi...

I have a search page...

The search fields are first_name, last_name, email, city, state etc...

When i click the search button, i need to pass all these values to the controller and from there to model.

how to pass all these values???
#2

[eluser]Pascal Kriete[/eluser]
When you submit a form the values are added to the post array (or get - don't use that though).

Then in your receiving controller you can access the fields like this:
Code:
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
...

And to pass them to your model, you put them in an array, and pass it along as a parameter:
Code:
$data = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => // ... you get the idea
);

$this->the_model->the_function($data);
#3

[eluser]Colin Williams[/eluser]
It's a good idea to wrap your head around request methods and HTTP in general. Take the evening off and read this: http://www.w3.org/Protocols/
#4

[eluser]Eric Cope[/eluser]
You probably should not access directly the post variables. Instead, check out Code Igniter's validation library. I use it extensively. Also, always assume your incoming data is dirty. If you do not assume this, you will regret it some time in the future. Reference the user manual, it is excellent.
#5

[eluser]johnmiller[/eluser]
thanks a lot... :-)
#6

[eluser]johnmiller[/eluser]
i tried passing one value... but didn't work..

this is my textbox

Code:
<input type="text" id="txt_fname" name="txt_fname" />

this is my button
Code:
<?php
    $this->load->helper('form');
    echo form_open('blog/do_search',array('fname' => 'txt_fname'));
  ?>
    <input type="submit" name="button" id="button" value="Search" /></form>


in the controller, i wrote

Code:
function do_search()
    {
      $first_name = $this->input->post('fname');
      $this->load->model('search_model');
      $data['search_details'] =  $this->search_model->search($first_name);
      $this->load->view('search_results', $data);
    }


but i am not getting $first_name value in the model.

where i am making mistakes???
can you please help me?
#7

[eluser]Eric Cope[/eluser]
In your view, I would do this...
Code:
<?php echo $this->validation->error_string; // this echos the error string from validation?>
<?=form_open('blog/do_search');?>
<input type="text" value="<?=$this->validation->fname;?>" name="fname"></input>
<input type="submit" value="search the blog"></input>
</form>

in your controller...
Code:
<?php
    $this->validation->set_error_delimiters('<div id="errorLogin">', '</div>');
    $rules['fname'] = "trim|required|max_length[100]|xss_clean|..."; / /add more if necessary
    $this->validation->set_rules($rules);
    $fields['fname'] = "First Name";
    $this->validation->set_fields($fields);
    if($this->validation->run() == FALSE)
    {
        $this->load->view('your_search_view');
    }
    else{
        // your data is clean here, so do what you want
        $fname = $this->validation->fname; // this is how you access validated data
        $this->load->view('search_results_view');
    }
?&gt;
This basic structure will allow you to clean your data and then perform the operations as necessary. The user guide outlines this well.

Remember, you can always look at the CI code too. Its not the Windows API!
#8

[eluser]johnmiller[/eluser]
my view

Code:
&lt;?php echo $this->validation->error_string;?&gt;
&lt;?php form_open('blog/do_search');?&gt;

&lt;input type="text" value="&lt;?php $this-&gt;validation->fname;?&gt;" name="fname">

&lt;/input&gt;&lt;input type="submit" name="button" id="button" value="Search" />&lt;/form&gt;

my controller

Code:
function do_search()
    {

    $this->validation->set_error_delimiters('<div id="errorLogin">', '</div>');
    $rules['fname'] = "trim|required|max_length[100]|xss_clean";
    $this->validation->set_rules($rules);
    $fields['fname'] = "First Name";
    $this->validation->set_fields($fields);
    if($this->validation->run() == FALSE)
    {
        $this->load->view('search');
    }
    else{
        
        $fname = $this->validation->fname;
        $this->load->model('search_model');
        $data['search_details'] = $this->search_model->search_students($fname);
        $this->load->view('search_results', $data);
    }

    }

my model
Code:
function search_students($fname)
    {
        $search_details = $this->db->query("SELECT * from student WHERE first_name = '$first_name'");
        return $search_details->result();
    }



but when i load search page, i get an error like

Quote:Undefined property: CI_Loader::$validation

where i have made mistake???

any idea???
#9

[eluser]Eric Cope[/eluser]
you must load the validation library.
In your model, you pass in $fname, but you use $first_name in the sql statement.
#10

[eluser]johnmiller[/eluser]
i have changed my model like this

Code:
function search_students($fname)
    {
        $search_details = $this->db->query("SELECT * from student WHERE first_name = '$fname'");
        return $search_details->result();
    }


i still get the error..

how to load the validation library???

the error i get is

Quote:Severity: Notice

Message: Undefined property: CI_Loader::$validation

Filename: views/search.php

Line Number: 272

The error is in the view

Line number 272 is

Code:
&lt;?php echo $this->validation->error_string;?&gt;

How to add the validation library?




Theme © iAndrew 2016 - Forum software by © MyBB