![]() |
repopulate a form with data from table - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: repopulate a form with data from table (/showthread.php?tid=41962) Pages:
1
2
|
repopulate a form with data from table - El Forum - 05-22-2011 [eluser]ralf0909[/eluser] i'm working on crud operations and i have a problem with the edit form. i need to extract a single row from the table user, and make the fields appear in the values of the form. with traditional procedural php it was very simple, but with CI it's a bit difficult for me. so i tried this but i need help: model Code: function get_data_user() controller Code: function edit() and the view: Code: <?php echo validation_errors(); ?> someone can help me? repopulate a form with data from table - El Forum - 05-22-2011 [eluser]fivefinger-bd[/eluser] Instead of this Code: <input type="text" name="firstname" value="<?php $data['single_user']->user_firstname; ?> " /> Try this: Code: <input type="text" name="firstname" value="<?php $single_user->user_firstname; ?> " /> repopulate a form with data from table - El Forum - 05-22-2011 [eluser]ralf0909[/eluser] the error is the same: Message: Undefined variable: single_user and Message: Trying to get property of non-object (referred to the line <input type="text" name="firstname" value="<?php $single_user->user_firstname; ?> " /> ) repopulate a form with data from table - El Forum - 05-22-2011 [eluser]Ellli[/eluser] well, you overwrite $data['single_user'] with $data = array(...); add an indec to your $data after update like this $data['new_data']: Code: function edit() and in view if you want to get value of $data['single_user']->... write $single_user and if you want to get value of $data['new_data'] write $new_data->... repopulate a form with data from table - El Forum - 05-22-2011 [eluser]ralf0909[/eluser] just another little help, now the error is: Error Number: 1054 Unknown column 'single_user' in 'field list' UPDATE `user` SET `single_user` = Array, `new_data` = Array WHERE `user_id` = '50' seems that single_user is considered a column, i'm searching a solution but i don't know how to resolve repopulate a form with data from table - El Forum - 05-22-2011 [eluser]Ellli[/eluser] why are you updating table in database before form is submitted? I cant see any logic in it. You can create two functions, one to get details from database and show it in form, and second one for actions after form is submitted. Or in one function use if statemant to decide if you wan't get details from database and show it, or just update it. something like: Code: if($this->input->post('your_submit_name')){ repopulate a form with data from table - El Forum - 05-22-2011 [eluser]InsiteFX[/eluser] Code: <input type="text" name="firstname" value="<?php echo set_value('firstname', $single_user); ?>" /> InsiteFX repopulate a form with data from table - El Forum - 05-23-2011 [eluser]fivefinger-bd[/eluser] [quote author="InsiteFX" date="1306099711"] Code: <input type="text" name="firstname" value="<?php echo set_value('firstname', $single_user); ?>" /> InsiteFX[/quote] This method also let you use error displaying. repopulate a form with data from table - El Forum - 05-23-2011 [eluser]ralf0909[/eluser] [quote author="Ellli" date="1306097626"]why are you updating table in database before form is submitted? I cant see any logic in it. You can create two functions, one to get details from database and show it in form, and second one for actions after form is submitted. Or in one function use if statemant to decide if you wan't get details from database and show it, or just update it. something like: Code: if($this->input->post('your_submit_name')){ so what i need to do? i want to show the form with old data inside (or near the fields, is the same), if i change data in the fields and click on submit button, new data will saved in the database. i'm new to codeigniter and object oriented programming. i tried the if command but it doesn't show anything, i'm doint it wrong. i think the problem is the function to extract data, tell me if is correct: in the model i use a select on the row with the user_id of the user, and then i use a get to retrieve data. then i save the data on a variable named $query and put a "return $query;" right? what's the differencre from "return $query;" and "return $query->result();" ? repopulate a form with data from table - El Forum - 05-23-2011 [eluser]ralf0909[/eluser] i tried this, now works but i still get an error message: controller: Code: function edit() view: Code: <?php foreach($single_user as $row) : ?> the single user data is shown in the fields, if i change something and click on submit, it works, but i get this error message from the view: Severity: Notice Message: Undefined variable: single_user and Severity: Warning Message: Invalid argument supplied for foreach() |