Jump to content

Store the ID Number Inside "One" Array Inside For Loop?


glassfish

Recommended Posts

The Script:

$desired_width = 110;

if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image

    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
        $target_path = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/uploads/"; //Declaring Path for uploaded images

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable

        $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1];
		$target_path = $target_path . $new_image_name;//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array       
      
	  if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';

                $tqs = "INSERT INTO images (`original_image_name`, `image_file`, `date_created`) VALUES ('" . $_FILES['file']['name'][$i] . "', '" . $new_image_name  . "', now())";
                $tqr = mysqli_query($dbc, $tqs);

                // Select the ID numbers of the last inserted images and store them inside an array.
                // Use the implode() function on the array to have a string of the ID numbers separated by commas.
                // Store the ID numbers in the "image_file_id" column of the "thread" table.

                $tqs = "SELECT `id` FROM `images` WHERE `image_file` IN ('$new_image_name')";
                $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));

                $fetch_array = array();
                $row = mysqli_fetch_array($tqr);
                $fetch_array[] = $row['id'];


                /*
                 * This prints e.g.:
                    Array ( [0] => 542 )

                    Array ( [0] => 543 )

                    Array ( [0] => 544 )
                 */
                print_r($fetch_array);

                // Goes over to create the thumbnail images.
                $src = $target_path;
                $dest = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/thumbs/" . $new_image_name;

                make_thumb($src, $dest, $desired_width);


            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}

Hey, sorry that I am posting this darn image upload script again, I have this almost finished and I am not looking to ask more questions when it comes to this script specifically.

 

With the script above I have that part where the script should store the ID numbers (the auto_increment column of the table) of the image files inside of one array and then the "implode()" function would get used on the array and then the ID numbers would get inserted into the "image_file_id" column of the "thread" table.

 

As you can see at the above part the script prints the following:

                    Array ( [0] => 542 )

                    Array ( [0] => 543 )

                    Array ( [0] => 544 )

And I am looking to insert into the column of the table the following:

542, 543, 544

I thought of re-writing the whole image upload script since this happens inside the for loop, though I thought maybe I could be having this done with the script as it is right now.

 

Any suggestions on how to do this?

Link to comment
Share on other sites

Based on what you've said you know what you should do you just haven't written any code to do so. You want to insert the related IDs onto the thread tables image_file_id column as comma-separated-values.

 

I may be misunderstanding your issue, but this is what it sounds like you want:

<?php
$desired_width = 110;

if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image

    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
        $target_path = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/uploads/"; //Declaring Path for uploaded images

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
        $file_extension = end($ext); //store extensions in the variable

        $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1];
		$target_path = $target_path . $new_image_name;//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array

	  if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';

                $tqs = "INSERT INTO images (`original_image_name`, `image_file`, `date_created`) VALUES ('" . $_FILES['file']['name'][$i] . "', '" . $new_image_name  . "', now())";
                $tqr = mysqli_query($dbc, $tqs);

                // Select the ID numbers of the last inserted images and store them inside an array.
                // Use the implode() function on the array to have a string of the ID numbers separated by commas.
                // Store the ID numbers in the "image_file_id" column of the "thread" table.

                $tqs = "SELECT `id` FROM `images` WHERE `image_file` IN ('$new_image_name')";
                $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));

                $fetch_array = array();
                $row = mysqli_fetch_array($tqr);
                $fetch_array[] = $row['id'];


                /*
                 * This prints e.g.:
                    Array ( [0] => 542 )

                    Array ( [0] => 543 )

                    Array ( [0] => 544 )
                 */
                $values = rtrim(implode(', ', $fetch_array)); // Should print, "542, 543, 544" no whitespace

                if(!mysqli_query($dbc, "INSERT INTO thread (image_file_id) VALUES ('{$values}')"))
                {
                    die(mysqli_error($dbc));
                }

                // Goes over to create the thumbnail images.
                $src = $target_path;
                $dest = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/thumbs/" . $new_image_name;

                make_thumb($src, $dest, $desired_width);


            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}

If this isn't what you want, I would need you to elaborate more on what your end goal is.

Link to comment
Share on other sites

OxMatt, your example inserts the ID numbers one row after the other.

 

For example:

Row 1: Title, Caption, ", Hashtags, Date Created

Row 2: ", ", 557, ", "

Row 3: ", ", 558, ", "

I am looking to have this data which gets printed by the script the way it is currently ...

Array ( [0] => 542 )

Array ( [0] => 543 )

Array ( [0] => 544 )

... inside one array:

Array ( [0] => 542 [1] => 543 [2] => 544 )

Then I would be looking to use the "implode()" function to get this:

542, 543, 544

Barand, the data is referenced to a "thread" (which gets created), I thought this may be the way to do it?

Link to comment
Share on other sites

Placing this outside of the for loop...

print_r($fetch_array);

.. prints:

Array ( [0] => 560 ) 

It prints the last added ID number. Here I am asking, how can I have the other ID numbers too?

 

I thought maybe the "array" has to be printed outside of the for loop to have all ID numbers inside one array.

 

 

 

OxMatt, your suggestions prints the following:

                $values = rtrim(implode(', ', $fetch_array)); // Should print, "542, 543, 544" no whitespace

                print_r($values);
561

562

563

Do you have another suggestion?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.