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.
6 2801
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.
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.
Example: - 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
-
}
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.
Okay, well, what I'm really trying to do is apply mysqli_real_escape_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.
Okay, well, what I'm really trying to do is apply mysqli_real_escape_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.
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.
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.
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.
[PHP]$query = "INSERT INTO database (field1,field2,field3) VALUES ('".mysql_real_escape_string($_POST['var1'])."','".mysql_real_escape_string($_POST['var2'])."','".mysql_real_escape_string($_POST['var3'])."')";[/PHP]
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","favorite_color");
Then, I build my query string like this: -
$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]));
-
}
-
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).
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: James Curran |
last post by:
I'd like to make the following proposal for a new feature for the C#
language. I have no connection with the C# team at Microsoft. I'm posting
it here to gather input to refine it, in an "open...
|
by: cody |
last post by:
What about an enhancement of foreach loops which allows a syntax like that:
foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in 'a'..'z') { } // chars...
|
by: Joe Rattz |
last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to
intialize them to false.
bool bits = new bool;
foreach(bool bit in bits)
{
bit = false;
}
The compiler complains...
|
by: bughunter |
last post by:
This is partly 'for the record' and partly a query about whether the
following is a bug somewhere in .Net (whether it be the CLR, JITter, C#
compiler). This is all in the context of .Net 1.1 SP1.
...
|
by: Anthony Bouch |
last post by:
Everything I know about looping structures says to be careful about
expressions that need to be evaluated again and again for each
test/increment of a loop.
I came across this piece of code the...
|
by: bg_ie |
last post by:
Hi,
What is wrong with the following -
short arrfile = new short;
foreach (short s in arrfile)
{
s = 10;
}
|
by: Osiris |
last post by:
Just something I would like to share:
I just learned the hard way (2 days detective work on a bug) that foreach
loops are not at all like for loops, not intuitive at all. BEWARE: arrays
and...
|
by: fjm |
last post by:
Hello everyone,
I need some help and direction with this code. I am trying to have the same functionality that this forum has with regard to the allowable length of time the post (Or in my case,...
|
by: natarajmtech |
last post by:
Hi all,
I have defined some array variables a
*****************************************
@L=('CTT','CTC','CTA','CTG','TTA','TTG');
@S=('TCT','TCC','TCA','TCG','AGT','AGC');
...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |