• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Concatenate problem in php

Maarten Martens

New Member
Hello all,

This might be a stupid question, but I'm hopelesly stuck on this thing. So if there is anyone who can help me, please do so!

I'm having a php file where 10 inputs are created. I'm using a "for" statement. The elements are like this <input type="file" name="image<?=i?>" > ... So they are named dynamically using the $i var in the for statement, like image1 imgae2 image3...

I do a method=post of this page. Now I have another php page where I would like to go through this inputs and check what is filled in them. I'm again using a for statement, and go 10 times through it. I would like to check on this inputs, like if ($image.$i != '') ...But thats is not correct. I have tried everything, I just don't know how I can dynamically concat these elements to create a image1 image2 ... How do I plave the $i and the $image creating a image1 where I can check upon?

Please help!!

Thanks,
Maarten
 
Thank you already, hohoho ... I still have a problem ... This is my code :

for($i=1; $i <= 10; $i++)
{
eval("if(\$image$i) != \"\");
{
$data = addslashes(fread(fopen(eval("$image$i"), "r"),
filesize (eval("$image$i"))));

$sdbquery = "INSERT INTO main_screenshot
(game_id, screenshot_data)
VALUES ($id,'$data')"
if (@mysql_query($sdbquery))
{
echo "image $i has been added";
}
else
{
echo "could not add image $i";
}
}
}

So I've added your eval code, but now I get an error in the $data part of the code. I just want to fopen the data from every image input ... I just don't know how to write it!

Anybody out there who can help me? This is really griping my --- here, I had no idea concatenating would be so difficult in php :(

Thanks already,
Maarten
 
for($i=1; $i <= 10; $i++)
{
eval("if(\$image$i != \"\")");
{
$data = addslashes(fread(fopen(eval("\$image$i"), "r"),
filesize (eval("\$image$i"))));

$sdbquery = "INSERT INTO main_screenshot
(game_id, screenshot_data)
VALUES ($id,'$data')"
if (@mysql_query($sdbquery))
{
echo "image $i has been added";
}
else
{
echo "could not add image $i";
}
}
}

you forgot to escape the $ ( bold text )
and the if clause was messed up :p
 
Last edited:
Okay, first off, thanks for all the help.

I implemented your code, hohoho ... But sadly it still doesn't work, I get parse errors right from the start, in the first eval...

This is frustrating ... It's getting ridiculous ... I never knew dynamically looping over elements would be such a pain in php ...

:cry2:

Maarten
 
concat in PHP is like this

$image = blah;
$w00t = huh;
$temp = $image . $w00t;

Or in an if statement of eval statement:

if (($w00t . $image) == "huhblah") {

// code to do if true

}

Note: I personally like to add extra spaces for readability and just for neat coding standards, i dont know if they are needed or not.
 
Back
Top