Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a better understanding of your problem now - thanks for posting the updates to your code. I'm updating and elaborating on my answer to explain your problem and offer some solutions.</p> <p>I'm sure you know, but it won't hurt to preface this with a basic review of the general MVC pattern as it relates to URI structure. That is, in the example of <code>site.com/home/survey/new</code>, the first segment <code>home</code> is the controller class name, the second segment <code>survey</code> is the controller function name, and the third segment <code>new</code> (and any segments after this) is an ID or variable passed to <code>survey()</code>. <a href="http://codeigniter.com/user_guide/general/urls.html" rel="nofollow noreferrer">More here</a>.</p> <p>Your code isn't working because you never pass a variable to <code>survey()</code> via the URI when you submit the form, so "new" never exists in the URL, and therefore <code>$method</code> is always set to "update". So,</p> <blockquote> <p>"How can I pass $method to the _submit() function correctly?"</p> </blockquote> <p>There are a couple of options for doing this. Each one simply requires that you determine whether a user->survey relationship exists in order to set <code>$method</code>. You can then pass <code>$method</code> on to the appropriate functions. For my example below, I'm going to assume that a user can only have one survey. If that's not the case you'll just have to account for a unique survey ID associated with a unique user ID.</p> <p>One possible scenario goes like this (again, this assumes that a user can be associated with only one survey):</p> <ol> <li>User submits survey</li> <li>Upon form submission, check to see if a survey already exists for that user</li> <li>If so, set <code>$method="update"</code> otherwise set <code>$method="create"</code></li> <li>Proceed to pass <code>$method</code> to the other necessary functions</li> </ol> <p>An alternative solution might look like this:</p> <ol> <li>In your controller function that calls the view file of your survey, determine if a record already exists for the user.</li> <li>Pass that data to your survey view</li> <li>In the view, modify the form action so that it passes that data (either "create" or "update") to the survey() function when the form is submitted.</li> <li>In <code>survey()</code>, set <code>$method</code> based on the data supplied</li> <li>Proceed to pass <code>$method</code> to the other necessary functions</li> </ol> <p>I'll use the first option as an example and outline the logic you would need (forgive the somewhat pseudo-code, but you'll get the idea):</p> <p>survey() function:</p> <pre><code>function survey() { // instead of determining whether "new" is part of the URI (which it never is), // check for an existing survey associated with the logged in user's ID $id = $this-&gt;session-&gt;userdata('id'); $query = $this-&gt;db-&gt;get_where('Survey', array('user_id' =&gt; $id)); // if the above query returns a result, set $method="update" // otherwise set $method="create" if ($query-&gt;num_rows() &gt; 0) { $method="update"; } else { $method="create"; } // whatever other logic you need here // pass $method to survey_form() $this-&gt;survey_form_processing-&gt;survey_form($this-&gt;_container, $data, $method); } </code></pre> <p>survey_form() function:</p> <pre><code>function survey_form($container, $data, $method) { // your other code here if ( $this-&gt;CI-&gt;validation-&gt;run() === FALSE ) { // Output any errors $this-&gt;CI-&gt;validation-&gt;output_errors(); } else { // Submit form (w/ $method parameter - this comes from survey()) $this-&gt;_submit($method); } } </code></pre> <p>_submit() function:</p> <pre><code>function _submit($method) { // your other code here if ($method == "update") { // do something } elseif ($method == "create") { // do something else } } </code></pre> <p>As a side note, there are probably better ways to structure what you're doing. For example, in my pseudo-code above, the database interactions should probably be happening in a model. For clarity, though, they are included in the controller. I hope that helps!</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.
 

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