trouble with foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

trouble with foreach

Post by someguyhere »

Code: Select all

	$designation_array = $_POST['designation'];
		foreach ($designation_array as &$value) {
			$value = '<li>' . $value . '</li>' . "\n";
		}
	$designation = '<ul>' . "\n" . $value . '</ul>' . "\n";
I can't tell why this is only returning one value. This comes from a series of check boxes, but it only stores the last one checked.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: trouble with foreach

Post by someguyhere »

Solved it:

Code: Select all

	$designation_array = $_POST['designation'];
		foreach ($designation_array as &$value) {
			$designation = $designation . '<li>' . $value . '</li>' . "\n";
		}
	$designation = '<ul>' . "\n" . $designation . '</ul>' . "\n";
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: trouble with foreach

Post by Darhazer »

someguyhere wrote:Solved it:

Code: Select all

	$designation_array = $_POST['designation'];
		foreach ($designation_array as &$value) {
			$designation = $designation . '<li>' . $value . '</li>' . "\n";
		}
	$designation = '<ul>' . "\n" . $designation . '</ul>' . "\n";
You can use

Code: Select all

$designation .= '<li>' . $value . '</li>' . "\n";
It's shorter.
Also, no need of & in front of the value, since you do not modify it (and you can run in another problems with the reference)
Post Reply