CodeIgniter Forums

Full Version: Would like to condense some code.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]BrianDHall[/eluser]
Oh sorry, I wasn't sufficiently clear. Global needs to be declared in every scope where $data is used.

So you would need global reference in index and your function, and in any other functions that make reference to $data. Otherwise the function will create $data in it's own local scope and one will not have any effect on the other.

If you initialize $data in your constructor or a my_controller, you need to tell PHP you want $data to mean the GLOBAL variable before you manipulate it.

El Forum

[eluser]Damien K.[/eluser]
I've simplified your code to the following. The below code should work for you. Note that I extended the CI_Input class to support a set_post() function that sets data in $_POST.

Code:
function _populate_contact()
{
    if($this->userlib->logged_in())
    {
        $this->load->model('User_model', 'user_m');
        $userid    = $this->session->userdata('userid');
        $result    = $this->user_m->name_and_email($userid);
        $this->input->set_post('name', $result['name']);
        $this->input->set_post('email', $result['email']);
    }
}

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Input extends CI_Input
{
     function __construct()
     {
         parent::CI_Input();
     }

    // --------------------------------------------------------------------

    function set_post($field, $value)
    {
        $_POST[$field] = $value;
    }
}

You need to complement this using set_value() in your view. I mentioned that you should look into the manual for the Form helper functions. An example below:

Code:
<input type="text" name="email" value="<?=set_value('email')?>" />

El Forum

[eluser]Samuurai[/eluser]
[quote author="BrianDHall" date="1255133966"]Global needs to be declared in every scope where $data is used.[/quote]
Ah ha! It works perfectly now! Thanks a lot!!!

El Forum

[eluser]Samuurai[/eluser]
[quote author="Damien K." date="1255139512"]I've simplified your code to the following.[/quote]

Woah, that's really smart! Thanks! I'm going to implement it into my code tomorrow!

Thanks a lot!

B

El Forum

[eluser]Samuurai[/eluser]
Hi Damien,

Thanks for your help so far.. really appreaciate it!!

Out of curiosity, how do you set your values when you load the form initially... ie $this->data->firstname = ""; Or do you use another trick to avoid this?

Thanks

B

El Forum

[eluser]Damien K.[/eluser]
With, as you can see in the code above:

Code:
$this->input->set_post('name', $result['email']);

.. which will set the email field when the form is first loaded..

.. and in the view with:

Code:
<input type="text" name="email" value="<?=set_value('email')?>" />

With this setup, CI will handle subsequent requests (ie, post backs).

El Forum

[eluser]Samuurai[/eluser]
Right, I didn't realise set_value actually used post data. I've only just realised I can easily open up the CI source and have a look (i'm a right newbie, me). I was using it like this: set_value($email) - no wonder it wasn't working !!

I had it all working perfectly before using form_input('blah', "Blah!', $blah) which set it, and the form worked fine in "new" mode and "edit" mode...if I pre-set the value before loading the view. Howeverm set_value requires post data, how should I be passing the data to my view? With the set_post and set_value, will it just work? Sounds like I need to detach myself wanting to pass data using this method: $this->load->view('contact', $data);

Thanks for bearing with me Smile I'm sure there's a gold star in it for you... or good karma or something!

El Forum

[eluser]Damien K.[/eluser]
Yes, set_value() form helper function will use post data if available otherwise it will use the default value if provided as the second parameter -- e.g., set_value('email', 'default_address@mydomain.com').

I would imagine that most people in the community use this sort of approach.

If you are using the code I provided before, then set_post() will do all the 'magic' for you and you don't need to pass this data explicitly via $data (there still may be other data you might want to pass via $data). Note again that set_post() is not a CI build-in function--I've extended CI_Input to have this custom-made function to set post data so I don't need to pass it the 'normal' CI-way via $data.

I don't want to encourage you to detach yourself from passing data via $this->load->view('contact', $data), because this is 'common CI practice' and everyone does this! Furthermore, you still need to pass data, for example, page title using this method.

I'm probably only a few out there doing it the way I presented. It saves me a few keystrokes and it is intuitive for me. You should only try my approach if it is intuitive for you too -- don't try to force a square into a circle! There are more than one right way to do things. Feel free to inquire further -- it is a fairly big jump from where you started to the code I provided.

El Forum

[eluser]Samuurai[/eluser]
Hi Damien,

Ahh.. that's how it works.. I actually followed the advise of an earlier poster and gave the "@" method a go. It seems to be working well for me, although it does feel somewhat hackish. - It's great not having to define a bunch of variables to "" though, as my form is very long.

I'm really pleased with the setup I have now.. it feels much less clunky than before. set_post rocks as well, so thanks very much for that. I fits intuitively with me.. although, my some (il?)logical part of me would've prefered set_value() to be called get_value.

Thanks again for your help, really appreciate it, Damien!

Beren_
Pages: 1 2