FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


help with 'loops on variable variables'





jstoeffler
Hopefully I titled this somewhat correctly...?
OK... I don't get this and it's kinda pissin me off! Laughing

Shouldn't this:
Code:
      
for ($i=1,$i<=5,$i++)
   $q['$i'] = $_POST['$i'];


be the same as writing it out like this:
Code:

$q1 = $_POST['1'];
$q2 = $_POST['2'];
$q3 = $_POST['3'];
$q4 = $_POST['4'];
$q5 = $_POST['5'];

??????????
It won't work for me right now, and I need a lot more than 5 of these, and I don't really want to use so much code if there is something easier. (I'm just using this as an example of what I need to do)
Thanks in advance! Very Happy
woodenbrick
Your loop should be written like this:
Code:
for ($i=1;$i<=5;$i++){
   $q[$i] = $_POST[$i];
}

basically, you just need to remove the quote marks, otherwise it looks for an index called $i (which doesn't exist).
Also put semi colons to separate each step of the loop, not commas.
kv
if you want to use a string index instead of integer index, use this

Code:

for ($i=1,$i<=5,$i++)
   $q["$i"] = $_POST["$i"]; //Observe double quotes
imagefree
Both of the pieces of code have mistakes (logical).
The first piece of code should be like this:

Code:
      
for ($i=1;$i<=5;$i++)
   $q[$i] = $_POST[$i];


The second should be like:

Code:

$q['1'] = $_POST['1'];
$q['2'] = $_POST['2'];
$q['3'] = $_POST['3'];
$q['4'] = $_POST['4'];
$q['5'] = $_POST['5'];


In the first code you were creating a string but in the second code, you were assigning values to just variables. The correct form of the first example would be as shown above.



Later on you can call any variable with its name or using the variable inside it like

Code:

echo $q['1'];


or

Code:

echo $q[$variable]; //remember i used $array[$variable] instead of $array['$variable']


single quotes takes the inside text as string while double quote string can differentiate between variables and strings.

Any problem?
Related topics
php variables
PHP Toturail
How To : Improve Your PHP Programming
Fortran Tutorials(77)
FLASH HELP NEEDED BADLY!!!
php..free account help
Variable Name With A Variable in it. Is It possible?
Ruby(on rails) vs PHP
Basic C Programming Help?
[HELP] PHP (and some MyBB) Administration
C++ Program Basics
RESOLVED: Debugging help with PHP $_SESSION...
Help With C++ Searching
Is PHP Worth learning?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.