Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach help 1

Status
Not open for further replies.

bam720

Technical User
Sep 29, 2005
289
US
I have never used foreach becuase I have never fully understand it. However I am at a point where I think foreach is the best possible way to accomplish this task. I have a page which lists equipment and each row has a checkbox for deletion. This table is dynmacially long so there is no set length on number of checkboxes. I used a trick I read on these forums (either Jpadies or Sleipnir's) so that all my checkboxes are are named "Delete[$i]" basically dunmically populated with the right numbers, and the post arry reads the deletes as an array. The thing is I'd like these checkboxes stored as booleans, 1 or 0, so here is my attempt.
Code:
foreach($Delete as $Value){
		if($Value == "on")
			$Value = 1;
		else
			$Value = 0;
	}
However, that does not seem to work. If someone could help me out it'd be much appreciated. I am not sure of how the value part works. I tried using an &$Value but it doesn't seem to want to work.
 
Your code is fine.

however, what is happening is for each element in your array you are changing the value of the variable $Value. so the value of $Value will be the result of your test as applied to the LAST of the array elements.

I suspect what you are trying to do is alter the value of the $Delete array elements directly. to do this you need to pass the element by reference rather than create a copy of it (which is what your code does).

this is a very simple change to your code
Code:
foreach($Delete as &$Value){    //note addition of ampersand
        if($Value == "on")
            $Value = 1;
        else
            $Value = 0;
    }

check out the php manual on passing variables by reference for a more lucid explanation.
 
jpadie If you notice below my code I tried that. Here is what happens when I add the apersand.
Code:
Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /home/aspexent/public_html/support/addequip.php on line 19

foreach($Delete as &$Value){
	if($Value == "on")
		$Value = 1;
	else
		$Value = 0;
}
which looks just like yours.
 
That whole ampersand idea is a bad one. It could only work when passing parameters to a user-supplied function.

Use the form of the foreach where you get both a value and a key:

foreach ($Delete as $key => $value)
{
if ($value == 'on')
$Delete[$key] = 1;
else
$Delete[$key] = 2;
}



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks sleipnir. I saw that on php.net but was confused about how it worked. Thanks for the help :)
 
The only thing I am noticing is that if I set it to 0, print_r will only print the values in the array with a 1. Is this normal?
 
You have to remember that your browser only sends those checkboxes that are set to "on". The ones that aren't will not appear in the $_POST element array.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I use the & in this siutation all the time and it works fine.

Code:
$i = 0;
$i ++; $del[$i] = "on";
$i ++; $del[$i] = "on";
$i ++; $del[$i] = "off";
$i ++; $del[$i] = "off";
$i ++; $del[$i] = "on";
$i ++; $del[$i] = "off";

print_r($del);
echo '<BR><BR>';

foreach($del as &$Value){    //note addition of ampersand
        if($Value == "on")
            $Value = 1;
        else
            $Value = 0;
    }
    
print_r($del);

results:
Code:
Array ( [1] => on [2] => on [3] => off [4] => off [5] => on [6] => off )

Array ( [1] => 1 [2] => 1 [3] => 0 [4] => 0 [5] => 1 [6] => 0 )
 
That whole ampersand idea is a bad one.
that's a bit harsh.

i accept that php5 is not ubiquitous but at some point it's got to be reasonable to assume that users have migrated to the latest version unless they state otherwise. bear in mind that php 5.0.0 was released in july 2004 - two years ago now.
 
PHP version 4.4.2, which if I'm reading the manual correctly does not support this feature, was released less than 6 months ago.

Until Zend quits publishing the PHP 4.x line, it is unsafe to assume anything.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top