Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does codeigniter know how to pass parameters from controller to the model
    primarykey
    data
    text
    <p>I am starting to learn codeigniters active record and i am querying my database using parameters passed from the controller to the model.</p> <p>First i am passing the id from the controller to the model and that works.</p> <p>Controller</p> <pre><code>function bret($id){ $this-&gt;load-&gt;model('school_model'); $data = $this-&gt;school_model-&gt;get_city_and_population($id); foreach ($data as $row) { echo "&lt;b&gt;Name Of The City&lt;/b&gt;...........". $row['Name']; echo "&lt;br/&gt;"; echo "&lt;b&gt;Total Population&lt;/b&gt;...........".$row['Population']; } } </code></pre> <p>Model</p> <pre><code>function get_city_and_population($id){ $this-&gt;db-&gt;select('Name,Population'); $query = $this-&gt;db-&gt;get_where('city', array('ID'=&gt;$id)); return $query-&gt;result_array(); } </code></pre> <p>I went ahead and put in multiple parameters expecting to fail but this works but i am not so sure why it worked or what worked.</p> <p>Controller</p> <pre><code>public function parameters($id,$name,$district){ $this-&gt;load-&gt;model('school_model'); $data = $this-&gt;school_model-&gt;multiple_parameters($id,$name,$district); foreach ($data as $row) { echo "&lt;b&gt;Total Population&lt;/b&gt;...........".$row['Population']; } } </code></pre> <p>Model</p> <pre><code>function multiple_parameters($id,$name,$district){ $this-&gt;db-&gt;select('Population'); $query = $this-&gt;db-&gt;get_where('city', array('ID'=&gt;$id,'Name'=&gt;$name,'District'=&gt;$district)); return $query-&gt;result_array(); } </code></pre> <p>In my multiple parameters example i visited <code>http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/</code></p> <p>Here,i know the name <code>Haag</code> is in id <code>7</code> and the district is <code>Zuid-Holland</code></p> <p>Here are my questions.How does codeigniter know how to pass the parameters from the url to the model and secondly,what if i was slightly wrong like <code>7/Haag/Zuid-Hollandes/</code>,how would i show the user that,that url is wrong and fallback to a default value instead of showing blank when the parameters are wrong?.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload