Welcome Guest, Not a member yet? Register   Sign In
Looking for solution for easier variable setting.
#1

[eluser]Unknown[/eluser]
Hi, i'm new to CI but know some PHP. Although this OO is new to me.

I'm building a web-page where users can register their participation for a competition.

In doing so, I have created a form, but before displaying the view containing the form I fetch some data about the competitor based on his/her registration number. This data is fetched from another site using some HTML-parsing through PHP (placed in a model) and stored in an array.

I then pass this data to the form view. If it was successfull in fetching the data, I want some of the fields to be populated with this data, if not, well, populated with explaining text, as "postal code" and such.

I have managed to complete this, but in the beginning of the form view, I have to do some testing for each variable, it looks like this:

Code:
if ( !isset($name) ) $name = "Name";
if ( !isset($street) ) $street = "Street";
if ( !isset($date) ) $date = "Date of first participation";

etc. etc...

As you can see, this becomes a bit tedious for a long list of variables. Is there a way to make this more easily? I would like to create a function that takes an array and does this for me, something like:

Code:
$variables = array (
'name' => "Name",
'street' => "Street",
'date' => "Date of first participation",
);

test_variables( $variables );

But I can't figure out how to create the array, nor the function.

I would appreciate any ponts to where I should look. I have tried these forums and read about form_validation and pistolPetes extended form_validation, but I'm unable to see how it could help me.

Thanks in advance.
#2

[eluser]pistolPete[/eluser]
I didn't really get what you are trying to achieve and how it is connected to the form validation library, but you can use the following:

Code:
$variables = array (
'name' => "Name",
'street' => "Street",
'date' => "Date of first participation",
);

foreach ($variables as $key => $value)
{
   if(!isset($$key)
   {
      $$key = $value;
   }
}

What are the results of the HTML-parsing? Is there no other way to get the data?
#3

[eluser]xwero[/eluser]
another way is to set the defaults and let them be overwritten by the fetched data.
#4

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter community!

Assuming that the data is coming via POST, there's no need to check if it's set if you use $this->input->post(). You can set the default data from within the view by passing a second parameter to set_value().

EDIT: If you would be so kind as to post your view and function, we might be able to offer you a better solution.
#5

[eluser]Unknown[/eluser]
Thanks for the answers!

pistolPete's solution would probably work, and do the things i asked for. I just didn't know how to code it. But a more suitable solution to my situation is xweros, it's simple and will make my code clearer.

The data is not coming through a _POST, it's originating from a curl to the other web-page, which is then processed and returned.

Basically it's:

Code:
$this->load->model('Participants_model');

$data = $this->Participants_model->get_participant_data($reg_num);

$this->load->view('competition/register', $data);

But my problem is solved in a great way, thankyou!




Theme © iAndrew 2016 - Forum software by © MyBB