1

I have a form that includes 4 text fields and multiple file upload (5 files/fields):

The database table has 9 fields, 4 for the text fields and the remaining 5 should hold a URL path to the images being uploaded by the form, for the 5 URL fields in the database they are simply named as image1, image2, image3…

Normally we collect the form data and do whatever MySQL query we need to insert or update, but I cant figure out how to assign the foreach loop results to a progressive/dynamic variable name so that after the iterations I am only doing 1 MySQL query/insert.

$inum=0;

foreach ($_FILES["prodi"]["error"] as $key => $error)
{
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["prodi"]["tmp_name"][$key];
        $name = $_FILES["prodi"]["name"][$key];
        $type = $_FILES["prodi"] ["type"] [$key];
        if ($type=="image/jpg" OR $type=="image/jpeg" OR $type=="image/pjpeg" OR $type=="image/png")
        {move_uploaded_file($tmp_name, "../zzz/zzz-images/$name");
        $ipath=$domain."/zzz/zzz-images/".$name;
        $inum++;
        $i="image".$inum; // STUCK AT THIS POINT //}
}

So, if i were to echo $i and $ipath inside the loop, i will get:

image1 (The table field storing the url)  /  *ttp://www.whaterver......(the url stored)

image2 (The table field storing the url)  /  *ttp://www.whaterver......(the url stored)

And so on until there are no more files in the array to process, but how do i set each loop result to a new variable so that the mysqli query works like this:

mysqli_query($con,"INSERT INTO `table1` (`field1`,`field2`,`field3`,`field4`,`image1`,`image2`,`image3`,`image4`,`image5`) VALUES ('field1',field2','field3','field4','loop_result1','loop_result2','loop_result3' etc. etc. etc.);")
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
DMSJax
  • 1,709
  • 4
  • 22
  • 35
  • What are you stuck doing? Unclear even with your detailed explanation. – Giacomo1968 Dec 27 '13 at 20:14
  • @JakeGould the file upload part of this is looping through each temporary file stored on the server and moving them to the right directory, i need the full path of each file (1 >= 5) stored in a corresponding variable ($path1, $path2, etc. etc.) for use in the mysqli query. so if: variable $a = path/file_name from the first iteration of the loop, how/where do i tell it that the next path/file_name result needs to be assigned to variable $b and not write over $a... hope that explained it better – DMSJax Dec 27 '13 at 20:26

2 Answers2

1

With php you can use variables to create your variable names! So where you were stuck you should:

$varname  = "image".$inum; // "image1" for example
$$varname = $ipath; // this magic puts the value of $ipath into the variable $image1, $image2, etc

Here is an example of it working: https://eval.in/83538

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Digital Chris
  • 6,177
  • 1
  • 20
  • 29
  • Thank you, I thought the variable-variables was part of the answer but i've not used it before and wasn't sure i understood it. But I do get it now and that worked.Thanks! – DMSJax Dec 27 '13 at 20:45
  • Yes, things like `$$varname` seem like a mistake, but they work great in scenarios like this. – Giacomo1968 Dec 28 '13 at 00:38
0

Try this:

$inum++;
${'image' . $inum} = $ipath; // STUCK AT THIS POINT //}
}

See : Dynamic variable names in PHP

Community
  • 1
  • 1
Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26