Getting results from view to the controller

#1
[eluser]RobertB.[/eluser]
Hello guys,
I can't figure out how to do this if is not hard code.

Hard Code to send what the user selected by email.
This work good.
View
Code:
<input type="checkbox" name="ase" value="1" /> ASE
<input type="checkbox" name="atra" value="1" /> ATRA
<input type="checkbox" name="asa" value="1" /> ASA
<input type="checkbox" name="cert_other" value="1" /> Other
Controller
Code:
$ase = $this->input->post('ase');
$atra = $this->input->post('atra');
$asa = $this->input->post('asa');
$cert_other = $this->input->post('cert_other');

But if I try to do this
Dynamic
Code:
<?php foreach($certifications as $row => $cert):?>
<input type="checkbox" name="<?=$cert->certification?>" value="<?=$cert->id?>" /> <?=$cert->certification?>
<?php endforeach; ?>
I cant figure out the controller side of it. If I print the $_POST i get what the user selected.
Code:
[ASE] => 1  [ATRA] => 2

Do I have to do another foreach in the controller?

Thanks

#2
[eluser]RobertB.[/eluser]
How can I do this
Code:
$this->email->message(
   'Specialties : '.$specialties."\r\n".
   'Categories : '.foreach($categories['category'] as $key => $cat){echo $cat;}."\r\n".
   'Description : '.$description
);
I have a Parse error: on the foreach.

Thanks

#3
[eluser]cahva[/eluser]
For the first question, make the checkbox field names as array. Like:
Code:
<input type="checkbox" name="certification[<?=$cert->certification?>]" value="<?=$cert->id?>" /> <?=$cert->certification?>

With a name like that, you can use foreach on $_POST['certification'].


The latter error with the email.. You are actually echoing $cat inside $this->email->message(). Also making any kind of loop in the middle of variable setting is not allowed. So dont Smile
You dont have to foreach the $categories['category']. Just use implode() function. So if you wanted to add categories separated by comma, you would do it like this:
Code:
$this->email->message(
   'Specialties : '.$specialties."\r\n".
   'Categories : '.implode(',',$categories['category'])."\r\n".
   'Description : '.$description
);

#4
[eluser]RobertB.[/eluser]
cahva, thanks a lot that was what I needed

#5
[eluser]RobertB.[/eluser]
Hey cahva, I'm sorry that I keep asking this php basic questions but I now I have this problem.
If the user don't select any of the check boxes I get an empty variable and an error
Code:
implode() [function.implode]: Invalid arguments passed

I've tried
In the form and the controller but no luck
Code:
value="<?=(isset($cert->id)) ? $cert->id : '0'?>"

Can u please show me how can I go about this

Thanks again

#6
[eluser]daelsepara[/eluser]
Assuming you followed cahva's lead:

Code:
<input type="checkbox" name="certification[<?=$cert->certification?>]" value="<?=$cert->id?>" /> <?=$cert->certification?>

It's probably better if you do a foreach, in case no checkbox was selected, as you did originally.

Code:
$categories_list = "";
foreach($this->input->post('certification') as $key => $value):
     if (!empty($categories_list)) $categories_list .= ",";
     $categories_list .= $value;
endforeach;

$this->email->message(
   'Specialties : '.$specialties."\r\n".
   'Categories : '.$categories_list."\r\n".
   'Description : '.$description
);

#7
[eluser]RobertB.[/eluser]
Thanks, daelsepara.
This is what I end up doing and is working.
Please let me know if I should not do it this way

Code:
if($this->input->post('category')){
   $categories = implode(',',$this->input->post('category'));
}else{
   $categories = 0;
}

$this->email->message('Categories : '.$categories."\r\n");

Thanks again,
Robert

#8
[eluser]daelsepara[/eluser]
that will work well too. cheers!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.