Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two good ways to achieve this using methods intended by the Codeigniter developers.</p> <h2>OPTION ONE:</h2> <p>If you always expect an "id" parameter to be present you could take advantage of a feature where you pass the value in the URI immediately after the method (function) you want to call.</p> <p>Example passing <code>/[controller]/[method]/[value]</code>:</p> <pre><code>http://www.website.com/query/index/5 </code></pre> <p>You would then access the value of "id" as an expected parameter of the function.</p> <pre><code>Class Query extends Controller { ... // From your URL I assume you have an index method in the Query controller. function index($id = NULL) { // Show current ID value. echo "ID is $id"; ... } ... } </code></pre> <h2>OPTION TWO:</h2> <p>If you would like to allow many parameters to be passed in addition to ID, you could add all parameters as key=>value pairs to the URI segments in any order.</p> <p>Example passing <code>/[controller]/[method]/[key1]/[val1]/[key2]/[val2]/[key3]/[val3]</code>:</p> <pre><code>http://www.website.com/query/index/id/5/sort/date/highlight/term </code></pre> <p>You would then parse all the URI segments from the 3rd segment ("id") forward into an array of key=>value pairs with the <code>uri_to_assoc($segment)</code> function from the URI Class.</p> <pre><code>Class Query extends Controller { ... // From your code I assume you are calling an index method in the Query controller. function index() { // Get parameters from URI. // URI Class is initialized by the system automatically. $data-&gt;params = $this-&gt;uri-&gt;uri_to_assoc(3); ... } ... } </code></pre> <p>This would give you easy access to all the parameters and they could be in any order in the URI, just like a traditional query string.</p> <p><code>$data-&gt;params</code> would now contain an array of your URI segments:</p> <pre><code>Array ( [id] =&gt; 5 [sort] =&gt; date [highlight] =&gt; term ) </code></pre> <h2>HYBRID OF ONE AND TWO:</h2> <p>You could also do a hybrid of these where ID is passed as an expected parameter and the other options are passed as key=>value pairs. This is a good option when ID is required and the other parameters are all optional.</p> <p>Example passing <code>/[controller]/[method]/[id]/[key1]/[val1]/[key2]/[val2]</code>:</p> <pre><code>http://www.website.com/query/index/5/sort/date/highlight/term </code></pre> <p>You would then parse all the URI segments from the 4th segment ("sort") forward into an array of key=>value pairs with the <code>uri_to_assoc($segment)</code> function from the URI Class.</p> <pre><code>Class Query extends Controller { ... // From your code I assume you are calling an index method in the Query controller. function index($id = NULL) { // Show current ID value. echo "ID is $id"; // Get parameters from URI. // URI Class is initialized by the system automatically. $data-&gt;params = $this-&gt;uri-&gt;uri_to_assoc(4); ... } ... } </code></pre> <p><code>$id</code> would contain your ID value and <code>$data-&gt;params</code> would contain an array of your URI segments:</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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