1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Images upload in the wrong order

Discussion in 'PHP' started by qwikad.com, Jul 19, 2016.

  1. #1
    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):
     
    qwikad.com, Jul 19, 2016 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #2
    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?
     
    qwikad.com, Jul 19, 2016 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Post your HTML/PHP code. You must be doing something to mix them up. According to the standards:

     
    ThePHPMaster, Jul 19, 2016 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    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):
     
    qwikad.com, Jul 19, 2016 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #5
    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.
     
    qwikad.com, Jul 19, 2016 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    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, Jul 19, 2016 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #7
    @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
     
    Last edited: Jul 20, 2016
    qwikad.com, Jul 20, 2016 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    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.
     
    PoPSiCLe, Jul 20, 2016 IP
  9. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #9
    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
     
    qwikad.com, Jul 21, 2016 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    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, Jul 24, 2016 IP
  11. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #11
    @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).
     
    qwikad.com, Jul 24, 2016 IP