472,364 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 7815
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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.