469,310 Members | 2,384 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,310 developers. It's quick & easy.

Undefined Variable Solution??

blyxx86
256 100+
Good Morning,

I have a set of variables being passed to a form via CodeIgniter's MVC setup, however I am trying to keep from duplicating the form elements if at all possible.

However, I am getting undefined variable errors (for good reasons) when trying to build the input fields on the "add" form.

I know I can duplicate the form elements, one with the $result['data'] and one without, but I would really prefer not.

How would you suggest I build the form below so that I only pass the form_xxxx() functions once?

Expand|Select|Wrap|Line Numbers
  1. if(isset($result) && is_array($result)) // check to see if data is available to fill form
  2. {
  3.     $action = 'welcome/update';
  4.     $submit = 'Update Old';
  5. } else {
  6.     $action = 'welcome/insert';
  7.     $submit = 'Add New';
  8. }
  9.     echo form_open($action);
  10.     echo form_input('service_id',$result['service_id']); // undefined variable
  11.     echo form_input('updated_date',$result['updateddate']); // undefined variable
  12.     echo form_submit('submit',$submit);
  13.     echo form_close();
  14.  
Mar 12 '09 #1
10 7601
blyxx86
256 100+
AHA!!! Now I get it. Turns out you can use those '@' to stop errors from being reported.

Is there a better solution for this? Something like an internal if statement?

Expand|Select|Wrap|Line Numbers
  1. if(isset($result) && is_array($result))
  2. {
  3.     $action = 'welcome/update';
  4.     $submit = 'Update Old';
  5. } else {
  6.     $action = 'welcome/insert';
  7.     $submit = 'Add New';
  8. }
  9.     echo form_open($action);
  10.     echo form_input('service_id',@$result['service_id']);
  11.     echo form_input('updated_date',@$result['updateddate']);
  12.     echo form_submit('submit',$submit);
  13.     echo form_close();
  14.  
Mar 12 '09 #2
Markus
6,050 Expert 4TB
Check out the ternary operator, to easily set a default value to your variable if it doesn't already exist. Using @ is expensive.
Mar 12 '09 #3
blyxx86
256 100+
YAY! That's what I was looking for.

I did not know the cost of using '@' as with only two variables it is very quick (and running on my local server).

Would you suggest rewriting the form_open and form_submit lines? instead of using the two variables ($action and $submit)?

Expand|Select|Wrap|Line Numbers
  1. if(isset($result) && is_array($result))
  2. {
  3.     $action = 'welcome/update';
  4.     $submit = 'Update Old';
  5. } else {
  6.     $action = 'welcome/insert';
  7.     $submit = 'Add New';
  8. }
  9. echo form_open($action);
  10. echo form_input('service_id',(!isset($result['service_id'])) ? '' : $result['service_id']);
  11. echo form_input('updated_date',(!isset($result['updateddate'])) ? '' : $result['updateddate']);
  12. echo form_submit('submit',$submit);
  13. echo form_close();
  14.  
Mar 12 '09 #4
Markus
6,050 Expert 4TB
@blyxx86
You know, I'm a CI evangelist, but I've never understood the form helper; it seems pretty lazy to me. Also, I'd think it was more load on the server, too, than just using pre-wrote HTML. I'll benchmark it and see.

I would suggest that you set your variables from within your controller, to abstract away any PHP from the presentation (the fundamental target of MVC), and pass it through the $this->load->view($view, $vars) - $vars being an array of your form data.

- Mark.
Mar 13 '09 #5
blyxx86
256 100+
It took me some time to move myself over to the form helper.

I was experimenting with form classes (not to be confused with the helper functions) since I have A LOT of forms that need to be setup dynamically.

You're right though, I do need to pass the "Update/New" through the controller rather than set them in the view. I am wondering if I would be able to do the same with the $view_data['result'] variable that is passed to my view, but I would still receive the error about the variable not being defined. Sadly I have only been using this MVC for about 2 weeks, so it's still new to me.

Do you think you could show me a sample of your CI forms that you use? How do you create an add/update form?
Mar 13 '09 #6
Markus
6,050 Expert 4TB
It took me some time to move myself over to the form helper.

I was experimenting with form classes (not to be confused with the helper functions) since I have A LOT of forms that need to be setup dynamically.
OK.

You're right though, I do need to pass the "Update/New" through the controller rather than set them in the view. I am wondering if I would be able to do the same with the $view_data['result'] variable that is passed to my view, but I would still receive the error about the variable not being defined. Sadly I have only been using this MVC for about 2 weeks, so it's still new to me.
In the controller, you would check for the variables value (using the ternary operator), and then pass through the values.

Do you think you could show me a sample of your CI forms that you use? How do you create an add/update form?
I don't do my form's dynamically, because I always know what I need from a form. I do, however, use the set_value() function - available when using the form validation class, which you should be ;)

Here's how I generally set up a form:
Expand|Select|Wrap|Line Numbers
  1. <?php if ( strlen ( validation_errors() ) > 0 ) : ?>
  2.         <div id="validation_errors">
  3.             <?=validation_errors()?>
  4.         </div>
  5.         <?php endif; ?>
  6.  
  7.         <fieldset>
  8.             <legend>Log in</legend>
  9.             <form method="post" action="">
  10.  
  11.                 <table align="center">
  12.                     <tr>
  13.                         <td>Username:</td>
  14.                         <td><input type="text" name="username" value="<?=set_value('username')?>" /></td>
  15.                     </tr>
  16.                     <tr>
  17.                         <td>Password:</td>
  18.                         <td><input type="password" name="password" value="<?=set_value('password')?>" /></td>
  19.                     </tr>
  20.                     <tr>
  21.                         <td>&nbsp;</td>
  22.                         <td align="right"><input type="submit" name="log_in" value="Log in" /></td>
  23.                     </tr>
  24.                 </table>
  25.  
  26.             </form>
  27.         </fieldset>
- Most developers would kill me for using 'short tags', but the jokes on you because CodeIgniter has a config option to rewrite short tags to full tags! So nerr.
Mar 13 '09 #7
blyxx86
256 100+
So how would you pass values from a database to populate your form?

The form helper has a set_value() function that allows you to specify a value to enter in the form, but the form validation set_value() doesn't appear to have that.

I plan to start using the form validation class, but have not yet had the chance to experiment with it.
Mar 13 '09 #8
Markus
6,050 Expert 4TB
So how would you pass values from a database to populate your form?

The form helper has a set_value() function that allows you to specify a value to enter in the form, but the form validation set_value() doesn't appear to have that.
Ah, my error. set_value() is part of the form helper.

To pass values from my database, I'd call my model to return an array (or object) of the data I need. Then I'd pass that into the view (using the second parameter of load->view( ). In the view I'd output the data into the input's value.

I plan to start using the form validation class, but have not yet had the chance to experiment with it.
It's a great tool.

- Mark.
Mar 14 '09 #9
blyxx86
256 100+
Thank you Mark.

I guess the form validation calls part of the form helper into it. Makes sense.

I will play around with that tutorial, though I am still confused as to how to setup the models within CI. I still have a lot to learn, but I am thankful for how well put together the CI user guide is.

Hopefully I will understand the MVC concepts more by the time I am supposed to have some 50+ table database application ready. HAHA!
Mar 16 '09 #10
Markus
6,050 Expert 4TB
@blyxx86
I didn't understand the 'model' side of MVC for a while, either. But, in basic terms, the model is what handles grabbing data from your database and returning it to your controller, and taking data from your controller and sticking it in your database.

Any questions you have, be sure to post 'em.

- Markus.
Mar 16 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by bissatch | last post: by
4 posts views Thread by Chris Beall | last post: by
6 posts views Thread by Jeremy Felt | last post: by
2 posts views Thread by Bob Bruyn | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.