glassfish Posted October 15, 2014 Share Posted October 15, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/ Share on other sites More sharing options...
0xMatt Posted October 15, 2014 Share Posted October 15, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/#findComment-1493640 Share on other sites More sharing options...
Barand Posted October 15, 2014 Share Posted October 15, 2014 Any suggestions on how to do this? Yes, don't. Do not store delimited values in a column. Normalize the data correctly and store them in another table as separate records, each with a link to the parent record. Quote Link to comment https://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/#findComment-1493641 Share on other sites More sharing options...
glassfish Posted October 16, 2014 Author Share Posted October 16, 2014 (edited) 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? Edited October 16, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/#findComment-1493659 Share on other sites More sharing options...
glassfish Posted October 16, 2014 Author Share Posted October 16, 2014 (edited) 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? Edited October 16, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/#findComment-1493776 Share on other sites More sharing options...
Solution glassfish Posted October 16, 2014 Author Solution Share Posted October 16, 2014 Addendum: I have gotten this to work by creating the array (first) "$fetch_array = array();" outside of the for loop, the printing of the array would then have to happen outside of the for loop as well. Quote Link to comment https://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/#findComment-1493810 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.