473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7940
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

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

Similar topics

3
by: Dan Finn | last post by:
OpenBSD 3.2 Apache 1.3.26 PHP 4.3.4 PHP-Nuke 6.9 getting these in the apache error log: Sun Nov 16 20:20:16 2003] PHP Notice: Undefined variable: HTTP_USER_AGENT in...
3
by: Jason | last post by:
hello, i am new to PHP, so go easy. I am using the examples in the book: PHP: Your Visual Blueprint For Creating Open Source, Server Side Content In the section where they talk about...
3
by: bissatch | last post by:
Hi, I get the following error: Notice: Undefined variable: end_while in C:\Program Files\Apache\Apache2\htdocs\csp\inc\xmlmenu.php on line 102 This is a script that works on the server...
4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
6
by: Jeremy Felt | last post by:
Newbie here. I'm sure I'm missing something EXTREMELY simple, but an hour of searching has led to nothing. I'm playing around with ajax and trying to pass a variable to a function. If I do:...
2
by: Bob Bruyn | last post by:
I've recently installed Apache 2 and php 5.2 on my WIndows XP machine. Everything is up and running. I'm passing some vars via the URL. It works fine online:...
1
by: bob johnson | last post by:
Notice: Undefined variable: db_host in C:\wamp\www\cbmall\index.php on line 7 Notice: Undefined variable: db_user in C:\wamp\www\cbmall\index.php on line 7 Notice: Undefined variable: db_pass...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.