I am aware it's an old script (an old way of doing things). The script works, however, the images do not upload in the right order. I am not exactly sure but it looks to me they upload from last to first instead of from first to last. What makes it do that? if (count($_FILES['pic']['tmp_name'])) { $ipval = ipval(); $uploaderror = 0; $uploadcount = 0; $errorMessages = array(); foreach ($_FILES['pic']['tmp_name'] as $k=>$tmpfile) { if ($tmpfile) { $thisfile = array("name"=>$_FILES['pic']['name'][$k], "tmp_name"=>$_FILES['pic']['tmp_name'][$k], "size"=>$_FILES['pic']['size'][$k], "type"=>$_FILES['pic']['type'][$k], "error"=>$_FILES['pic']['error'][$k]); if ($_FILES['pic']['size'][$k] > $pic_maxsize*1000) { $errorMessages[] = $thisfile['name'] . " - " . $lang['ERROR_UPLOAD_PIC_TOO_BIG']; $uploaderror++; } elseif (!isValidImage($thisfile)) { $errorMessages[] = $thisfile['name'] . " - " . $lang['ERROR_UPLOAD_PIC_BAD_FILETYPE']; $uploaderror++; } else { $newfile = SaveUploadFile($thisfile, "{$path_escape}{$datadir['adpics']}", TRUE, $images_max_width, $images_max_height); if($newfile) { $sql = "INSERT INTO $t_adpics SET adid = $adid, picfile = '$newfile'"; mysql_query($sql); if (mysql_error()) { ... Code (markup):
Somebody suggested doing this: "One way you could keep the order consistent is by sending another variable to the server on upload that contains an array of the file names in order, then base your INSERT order off of that array, not the order of the $_FILES array." Not sure how to go about it. Does anyone know?
When you say HTML code do you mean the input file code? It goes like this: <?php for ($i=1; $i<=$pic_count; $i++) { ?> <input type="file" name="pic[]" size="100"><br> <?php } ?> Code (markup):
I am wondering if the problem is not in how the images are uploaded, rather, since I see the order consistently reversed, it's possible I need to change some code in the script that serves them up: ASC instead of DESC. But I am not yet sure.
If that's the case, that they're always in the exact opposite order, that's a very likely scenario. If what you said to begin with was the case, one would expect some sort of randomness, or an array_reverse() in the script.
@PoPSiCLe I need to tell the LEFT OUTER JOIN $t_adpics p ON a.adid = p.adid AND p.isevent = '0' to be sorted by ASC and everything else by DESC (which is already there). Not sure if I know how. I tried all kinds of things, nothing worked. picid is what should be used there to sort it out. When I add ORDER BY picid ASC anywhere there it just gives me an error. What is the right way of having two ORDER BY? "SELECT a.*, ct.cityname, UNIX_TIMESTAMP(a.createdon) AS timestamp, feat.adid AS isfeat, COUNT(*) AS piccount, p.picfile AS picfile, scat.subcatname, scat.catid, cat.catname FROM $t_ads a INNER JOIN $t_featured feat ON a.adid = feat.adid AND feat.adtype = 'A' AND feat.featuredtill >= NOW() INNER JOIN $t_cities ct ON a.cityid = ct.cityid INNER JOIN $t_subcats scat ON a.subcatid = scat.subcatid INNER JOIN $t_cats cat ON scat.catid = cat.catid LEFT OUTER JOIN $t_adpics p ON a.adid = p.adid AND p.isevent = '0' WHERE $visibility_condn $loc_condn GROUP BY a.adid ORDER BY a.createdon DESC LIMIT $latest_featured_ads_count"; Code (markup): PS I guess I also need to add that picid somewhere there after the SELECT
Just change the ORDER BY line to: ORDER BY a.picid ASC, a.createdon DESC Code (markup): This assumes that picid is part of the table aliased by a, of course. If it's not, you'll have to add it otherwise before it will be available.
Figured this one out. I took a wrong route and it got me nowhere. All I needed to do is change p.picfile AS picfile to MIN(p.picfile) AS picfile
If you are concerned about the order, GIVE THEM AN ORDER instead of the automatic [] <?php for ($i = 0; $i < $pic_count; $i++) echo ' <input type="file" name="pic[', $i, ']" size="100"><br>'; ?> Code (markup): ... end of problem. Will force them to be indexed 0.. ($pic_count - 1) Also, avoid starting at one... it's really not how things like FOR are meant to work. I've been seeing a lot of people blindly relying on [] these days, interestingly I think I know what's going on. In some browsers if you have multiple type="file" and you select them out of order, the [] gets populated in the order you filled them out! You're not the first person I've seen bitten by this in the past week or so. Of course, if you don't populate some of them, beware the gaps! Foreach is your friend in this case. Oh, and that you're passing variables in your query string? Worrisome... as is a query with that many join in general... takes the relational out of relational databases.
@deathshadow the script isn't mine. I wish I could sit down and whip out another script that will be more lightweight. I simply can't. Maybe some day I'll hire someone to go through the script with me and change some things around. Actually, the script I use does have it enumerated. I pulled that other example with blank [] from the original script, but thank you for pointing it out. Like I said there, I took a wrong route. Spent 3 days trying to figure it out. Somehow I missed the fact that it wasn't the loading that caused the issue but rather how the other script with p.picfile AS picfile was fetching the images. It was kind of random (almost always the last image, instead of the first one).