473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

foreach loop variables.

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.
Jul 1 '07 #1
6 2801
kovik
1,044 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. foreach($items as $id => $item)
  2. {
  3.     $item['qty'] += $id;    // Doesn't affect anything outside of the loop
  4.     $items[$id]['qty'] += $id;    // Affects the original $items array
  5. }
Give an example of what you want to do.
Jul 1 '07 #2
mwasif
802 Expert 512MB
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.
Jul 1 '07 #3
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.
Jul 1 '07 #4
kovik
1,044 Expert 1GB
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.
Jul 1 '07 #5
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]
Jul 2 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1. $fields = ARRAY("lastname","phone","favorite_color");
  2. $query = sprintf("INSERT INTO table SET firstname='%s'",mysql_real_escape_string($_REQUEST['firstname']));
  3.  
  4. foreach ($fields as $field){
  5.    $query .= sprintf(",$field='%s'",mysql_real_escape_string($_REQUEST[$field]));
  6. }
  7.  
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 :)
Jul 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

32
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...
104
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...
32
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...
2
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. ...
9
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...
4
by: bg_ie | last post by:
Hi, What is wrong with the following - short arrfile = new short; foreach (short s in arrfile) { s = 10; }
7
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...
2
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,...
28
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'); ...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.