CodeIgniter Forums
how to retrieve the values of mulple list? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to retrieve the values of mulple list? (/showthread.php?tid=5326)



how to retrieve the values of mulple list? - El Forum - 01-17-2008

[eluser]Nagabhushana[/eluser]
how to retrieve the values of mulple list?

example html code is follows

<select class= "box-small13" id="community[] " name="community[]" size=5 multiple style='width:150px';>
<option value=item1 >item1</option>
<option value=item2 >item2</option> <option value=item3 >item3</option> <option value=item4 >item4</option> <option value=item5 >item5</option> <option value=item6 >item6</option> <option value=item7 >item7</option> </select>

thank u


how to retrieve the values of mulple list? - El Forum - 01-17-2008

[eluser]xwero[/eluser]
Code:
print_r($_POST['community'])
This would give you an array with all the user selected options.


how to retrieve the values of mulple list? - El Forum - 01-17-2008

[eluser]tonanbarbarian[/eluser]
Code:
$this->input->post('community');
is safer. Just do not use XSS_Clean with it because it will not work with arrays


how to retrieve the values of mulple list? - El Forum - 01-17-2008

[eluser]jcopling[/eluser]
Yeah I would typically do something like this:

Code:
$list = $this->input->post('community');
for($i=0;$i<=count($list);$i++){
//do something with $list[$i]
}



how to retrieve the values of mulple list? - El Forum - 01-18-2008

[eluser]xwero[/eluser]
[quote author="jcopling" date="1200631253"]Yeah I would typically do something like this:

Code:
$list = $this->input->post('community');
for($i=0;$i<=count($list);$i++){
//do something with $list[$i]
}
[/quote]
a foreach loop takes less time and variables to do the same thing.


how to retrieve the values of mulple list? - El Forum - 01-18-2008

[eluser]jcopling[/eluser]
It's true that a foreach loop is more efficient. But I find that when I need multiple values from a form post, I need it for more than one corresponding fields. So it's just as easy to do something like this:

Code:
$community = $this->input->post('community');
$location = $this->input->post('location');
$membercount = $this->input->post('membercount');
for($i=0; $i<=count($community);$i++){
//do something with community[$i]
//do something with location[$i]
//do something with membercount[$i]
}

But you're absolutely correct. If you are only needing to loop through 1 set of values a foreach loop would be preferable.


how to retrieve the values of mulple list? - El Forum - 01-18-2008

[eluser]xwero[/eluser]
This is what i do when i need to handle several equal length arrays
Code:
$community = $this->input->post('community');
$location = $this->input->post('location');
$membercount = $this->input->post('membercount');
$i = 0;
foreach($community as $user){
//do something with user
//do something with location[$i]
//do something with membercount[$i]
$i++;
}
When you use a foreach you can split the item up in key and value so you can work with associate arrays too.

btw using the count function every time the loop restarts isn't good for performance. This is better:
Code:
for($i=0, $max = count($community); $i<=$max;$i++){

Foreach is not absolute performance but i think flexibility is a big issue too. But of course everyone has his/her own style.


how to retrieve the values of mulple list? - El Forum - 01-18-2008

[eluser]jcopling[/eluser]
Good points. I'll have to give this method a try.