How would one force variables within a foreach loop take effect globally, even outside the loop? I know foreach variables are usually local to the loop, but I'd like to work around that. Thanks in advance.
foreach loop variables.
Collapse
X
-
Tags: None
-
That depends on what you want to alter. The point of the "as $foo" is to allow you to avoid altering data. But if you use the original variable name, you can alter it.Originally posted by cheesecakerHow would one force variables within a foreach loop take effect globally, even outside the loop? I know foreach variables are usually local to the loop, but I'd like to work around that. Thanks in advance.
Example:
[code=php]foreach($items as $id => $item)
{
$item['qty'] += $id; // Doesn't affect anything outside of the loop
$items[$id]['qty'] += $id; // Affects the original $items array
}[/code]
Give an example of what you want to do. -
Do you want to use array keys as variables? To achieve this, you can use variable variable method
[PHP]$items = array("var1"=>" value1", "var2"=>"value2 ");
foreach($items as $k=>$v)
{
$$k = $v;
}
echo $var1;[/PHP]
Now you can use array keys as variables.Comment
-
Okay, well, what I'm really trying to do is apply mysqli_real_esc ape_string() to each value in an array, without having to do it manually. But I don't think this is the right way to go about it now.Comment
-
You only need to escape string as they are going into the database. The only time you should call that function is while writing the SQL query.Originally posted by cheesecakerOkay, well, what I'm really trying to do is apply mysqli_real_esc ape_string() to each value in an array, without having to do it manually. But I don't think this is the right way to go about it now.Comment
-
Yes, but I've got my $_POST array of user input, which I need to place inside the database. I'd prefer not to do this, though.Originally posted by volectricityYou only need to escape string as they are going into the database. The only time you should call that function is while writing the SQL query.
[PHP]$query = "INSERT INTO database (field1,field2, field3) VALUES ('".mysql_real_ escape_string($ _POST['var1'])."','".mysql_r eal_escape_stri ng($_POST['var2'])."','".mysql_r eal_escape_stri ng($_POST['var3'])."')";[/PHP]Comment
-
To do long queries like that, I create an array at the beginning of my save function of all the variables I'm trying to put into a db. So if my form had the fields, firstname, lastname, phone, and favorite color:
$fields = ARRAY("lastname ","phone","favo rite_color");
Then, I build my query string like this:
Obviously, your db column names have to match your form names. Also, one of the fields has to be hard coded into the first part of the query, so that you can have the comma separation be correct (so you don't end on a comma).Code:$fields = ARRAY("lastname","phone","favorite_color"); $query = sprintf("INSERT INTO table SET firstname='%s'",mysql_real_escape_string($_REQUEST['firstname'])); foreach ($fields as $field){ $query .= sprintf(",$field='%s'",mysql_real_escape_string($_REQUEST[$field])); }
One benefit of this is that if you ever add a field in your form, all you have to do is add it to the $fields array, and it will automatically be dealt with.
Hopefully that helps :)Comment
Comment