Author Topic: I need a CodeIgniter tutorial...  (Read 1169 times)

0 Members and 1 Guest are viewing this topic.

Offline RIRedinPATopic starter

  • Enthusiast
  • Posts: 256
    • View Profile
I need a CodeIgniter tutorial...
« on: August 10, 2010, 10:47:28 AM »
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. 

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: I need a CodeIgniter tutorial...
« Reply #1 on: August 10, 2010, 10:26:36 PM »
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.

Offline petroz

  • Enthusiast
  • Posts: 178
    • View Profile
Re: I need a CodeIgniter tutorial...
« Reply #2 on: August 10, 2010, 11:47:12 PM »
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


 

Offline RIRedinPATopic starter

  • Enthusiast
  • Posts: 256
    • View Profile
Re: I need a CodeIgniter tutorial...
« Reply #3 on: August 11, 2010, 10:32:47 AM »
Thanks Petroz and Thorpe...

Petroz, virtual beer for you on me... :D

Offline RIRedinPATopic starter

  • Enthusiast
  • Posts: 256
    • View Profile
Re: I need a CodeIgniter tutorial...
« Reply #4 on: August 12, 2010, 01:33:27 PM »
This worked nicely but I did run into a problem trying to pass a <select> option back to the controller -

Code: [Select]
"field1" => $this->select->post('field1')
didn't seem to be an option...

can anyone point me in the right direction here? Thanks

Offline sKunKbad

  • Devotee
  • Posts: 1,477
  • Gender: Male
    • View Profile
    • Brian's Web Design - Temecula
Re: I need a CodeIgniter tutorial...
« Reply #5 on: August 14, 2010, 06:15:58 PM »
This worked nicely but I did run into a problem trying to pass a <select> option back to the controller -

Code: [Select]
"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.
Brian's Web Design - Temecula

Freedom is only available through death.

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: I need a CodeIgniter tutorial...
« Reply #6 on: August 15, 2010, 03:09:31 AM »
Developer from Belgium, Vlaams-Brabant

Offline burton72stephens

  • Irregular
  • Posts: 1
  • Gender: Male
    • View Profile
Re: I need a CodeIgniter tutorial...
« Reply #7 on: August 18, 2010, 01:21:23 AM »
well i was thinking to use CI....dunno more abt it...may be ur post can help.

Offline pkphp

  • Irregular
  • Posts: 11
    • View Profile
    • www.PHPKode.com
Re: I need a CodeIgniter tutorial...
« Reply #8 on: September 21, 2010, 01:51:34 AM »
You can go to the CodeIgniter main site to look for more tutorial , also you can order some books about it from ebay.