PDA

View Full Version : Problems with For Loop


RVK
05-20-2003, 01:02 AM
I can't seem to get this to work no matter what I do!

here's my code:


$nlink1=$HTTP_POST_VARS['nlink1'];
$nlink2=$HTTP_POST_VARS['nlink2'];
$nlink3=$HTTP_POST_VARS['nlink3'];

for ($i = 1; $i <= 3; $i+=1) {
$temp = "$nlink".$i;
"$nlink".$i="nlink".$i."=".$temp;
print "$nlink".$i;
}


I'm trying to basically replace this.....


$nlink1="nlink1=".$nlink1;
$nlink2="nlink2=".$nlink2;
$nlink3="nlink3=".$nlink3;


thanks

-R

CyanBlue
05-20-2003, 03:26 AM
Here you go, my friend... ;)<?
$nlink1 = $_POST['nlink1'];
$nlink2 = $_POST['nlink2'];
$nlink3 = $_POST['nlink3'];

for ($i = 1 ; $i <= 3 ; $i++)
{
$temp = ${"nlink" . $i};
${"nlink" . $i} = "nlink" . $i . "=" . $temp . "&";
print ${"nlink" . $i};
}
?>

freddycodes
05-20-2003, 04:06 AM
And if you wanted to have a variable number of nlinks you could use this.


<?php
//Here for example.
$HTTP_POST_VARS['nlink1'] = "foo1";
$HTTP_POST_VARS['nlink2'] = "foo2";
$HTTP_POST_VARS['nlink3'] = "foo3";
$HTTP_POST_VARS['nlink4'] = "foo4";
$HTTP_POST_VARS['nlink5'] = "foo5";

$links = array();
foreach($HTTP_POST_VARS as $key => $val) {
if(preg_match("/^nlink[\\d]+$/", $key)) $links[] = "$key=$val";
}

print implode("&", $links);
?>

RVK
05-20-2003, 04:33 AM
Holy Moly guys! Thanks sooo much!

-R

RVK
05-22-2003, 05:53 PM
Would it be possible to break down the following :


$nlink1 = $_POST['nlink1'];
$nlink2 = $_POST['nlink2'];
$nlink3 = $_POST['nlink3'];


into a for loop :


for ($i = 1 ; $i <= 3 ; $i++)
{
${"nlink" . $i} = $_POST."[\'nlink".$i."\']";
}


Yikes! That's completely wrong, I know.

Thanks for the help!

-R

freddycodes
05-22-2003, 05:56 PM
extract($_POST);


Now you can access those variables as $nlink1 and $nlink2, etc....


http;//www.php.net/extract

RVK
05-22-2003, 06:25 PM
So, I'm probably misunderstanding this.

But, if I pass a bunch of variables to php:

for example:

a = 10;
myVar = "hello";
myAddress = "Canada";

then in php I can to do :

extract($HTTP_POST_VARS);

and this is exactly the same as doing :

$a = $HTTP_POST_VARS['a'];
$myVar = $HTTP_POST_VARS['myVar'];
$myAddress = $HTTP_POST_VARS['myAddress'];

or does extract() only work with arrays that are passed to PHP??

thanks for the info.

-R

freddycodes
05-22-2003, 06:28 PM
$HTTP_POST_VARS is $_POST is an array passed to php. Yes it takes an array and extracts making variables from the array keys with the corresponding values.

RVK
05-22-2003, 06:35 PM
Wow, thanks! Life just doesn't get better than extract()

-R

pj88
05-22-2003, 06:35 PM
it should also be mentioned this is a great thread methinks. this shows the PHP equivalent of what in Actionscript (or ECMA) would be the array accessor method. this is a really handy way of automating all sorts of programming.

cheers,
-pj

http://pj88.net

freddycodes
05-22-2003, 06:40 PM
Well another point worth mentioning, when you start talking to hardcore security advocates, you will often hear, you'd better know exactly where your variables are coming from. By using extract() you are essentially making all members of an array of the global scope.

For instance I would probably never use extract on the $_POST array, I would pass the $_POST array to the function I would be using it in, and then possibly extract it there, so those globals only exist in the scope of the function.

pj88
05-22-2003, 06:47 PM
yep freddy is very right. :)

extract() can be very dangerous... you should always know where and what you data is