RIRedinPA Posted August 10, 2010 Share Posted August 10, 2010 I'm trying to work myself through learning this framework and though I have a lot of PHP experience MVC frameworks just frustrate the hell out of me. I've been able to write a controller that queries a database and return the results but I am stumped at how to pass data back to the controller say from a form...so user fills out form with items they want to search the db on, when form is submitted how is the data sent back to the controller or model so I can build a custom query? My understanding of CI (and I'll admit it is limited right now) is that I have to put variables in the URI...is that correct? I could have 10 different search items so I have to build out a string like index.php/controller/item1/item2/item3/item4/item5...? I'd rather not have the search variables exposed, is there a way to pass vars through $_POST? Any help or links to help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/ Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 Sorry, Ive not used CI so I'm not sure I'll be much help but any post data likely still turns up within $_POST. Unless they unset it somewhere along the line. I would look at the documentation in regards to the Request object or similar. Post data should be within there somewhere. There is likely form helpers as well. Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1097900 Share on other sites More sharing options...
petroz Posted August 11, 2010 Share Posted August 11, 2010 There is honestly a few way's todo this. CI Has a great form validation tool that has a function called set_value which allows you to present the previously typed data into it, but thats not the only way... One thing I find myself doing all the time is storing the user input as a array on the controller, and since I like to reuse my views, I simply tell the controller to pass any post data to that array and pass that array to the view. Then, the view will always look to see if the array has data in it for the specific field, if its there... put the contents into the value section... It's actually rather simple once you do it a few times... Here's an example. MyController.php <?php class Welcome extends Controller { function Welcome() { parent::Controller(); } function index() { if($_SERVER['REQUEST_METHOD'] == "POST"){ //get the form data $data['search_keyword'] = array( "field1" => $this->input->post('field1'), "field2" => $this->input->post('field2') ); //load the database model $this->load->model('my_model'); //pass the data to the database $data['results'] = $this->my_model->search($data['search_keywords']); //load the view with the results and form data $this->load->view('search', $data); } else { $this->load->view('search'); } } } Then my view <html> <head> <title>My View</title> </head> <body> <form method="post" action="MyController/form"> <input type="text" name="field1" value="<?php if(!empty($search_keyword['field1'])){ echo $search_keyword['field1'];?>"> <input type="text" name="field2" value="<?php if(!empty($search_keyword['field2'])){ echo $search_keyword['field2'];?>"> </form> </body> </html> Now I havent tested this stuff, but its basically what I do most of the time I am trying to accomplish what you are doing. Even though this all works fine, CI has some great libraries for forms and validation and databases and I highly recommend spending the time looking into to leveraging those over just plain old php and html. Let me know if that helps! Thanks, Peter Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1097913 Share on other sites More sharing options...
RIRedinPA Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks Petroz and Thorpe... Petroz, virtual beer for you on me... Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1098105 Share on other sites More sharing options...
RIRedinPA Posted August 12, 2010 Author Share Posted August 12, 2010 This worked nicely but I did run into a problem trying to pass a <select> option back to the controller - "field1" => $this->select->post('field1') didn't seem to be an option... can anyone point me in the right direction here? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1098561 Share on other sites More sharing options...
sKunKbad Posted August 14, 2010 Share Posted August 14, 2010 This worked nicely but I did run into a problem trying to pass a <select> option back to the controller - "field1" => $this->select->post('field1') didn't seem to be an option... can anyone point me in the right direction here? Thanks $this->input->post('field1'); input does not indicate an input element, but rather a class that you are calling the post method from. This means that even though the post value came from a select element, the value of that element was filtered and retrieved through the input class/post method. Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1099352 Share on other sites More sharing options...
ignace Posted August 15, 2010 Share Posted August 15, 2010 CodeIgniter From Scratch Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1099459 Share on other sites More sharing options...
burton72stephens Posted August 18, 2010 Share Posted August 18, 2010 well i was thinking to use CI....dunno more abt it...may be ur post can help. Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1100614 Share on other sites More sharing options...
pkphp Posted September 21, 2010 Share Posted September 21, 2010 You can go to the CodeIgniter main site to look for more tutorial , also you can order some books about it from ebay. Quote Link to comment https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/#findComment-1113575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.