Jump to content

Help sorting images in a scroll-down menu


quote

Recommended Posts

Hi everybody,

First of all I'd like to specify that I'm a total newbie regarding php and coding in general. I'm turning to you because I don't know anybody that has the time to fix my problem, which is that my job requires me to work on uploading articles for a (buggy) commercial website. That isn't much of a problem, except for the fact that when I try to select an image in the drop-down menu for the product I'm uploading, the images aren't sorted in any (apparent) fashion. I have to scroll down the whole menu to find the picture I'm looking for, and there are hundreds, if not thousands of them. It's taking a lot of time, and my patience is growing thin.

 

I managed to find what I suspect is the main culprit, this code portion, which I think lack some sort of sorting function :

 

<p>Image</p>

<td>

 

 

 

    <select name="sImage" onchange="ChangeImage('ImgProduct','image/'+this.options[selectedIndex].value)">

<?

//

 

      echo "<option value=\"\">Aucune</option>\n";

  $d = dir ("image");

      while(false !== ($FileName=$d->read ()))

{

  if ($FileName !="." && $FileName !="..")

{

  echo "<option value=\"$FileName\">$FileName</option>\n";

}

      }

 

$d->close ();

?>

</select>

 

 

If anybody could give me an easy solution to this problem, I'd be infinitely grateful. Thanks in advance.

Link to comment
Share on other sites

change

 

while(false !== ($FileName=$d->read ()))
         {
           if ($FileName !="." && $FileName !="..")
            {
               echo "<option value=\"$FileName\">$FileName</option>\n";
            }
      }

 

to

 

while (false !== ( $FileName = $d->read() )) {

if ($FileName !="." && $FileName !="..") {
	$mtime = filemtime($FileName);

	// check to make sure we're not overwriting a file
	if ($files[$mtime]) {
		// if there was already a file modifed at this time, increment
		// the mtime by one second until we reach a time that doesn't have
		// a file
		while ($files[$mtime]) {
			$mtime++;
		}
	}

	// save the file
	$files[filemtime($FileName)] = '<option value="' . $FileName . '">' . $FileName . '</option>';

}

}

ksort($files);

echo implode("\n", $files);

Link to comment
Share on other sites

<?

//

     $k = 0;

     echo "<option value=\"\">Aucune</option>\n";

       $d = dir ("image");        

     while(false !== ($FileName=$d->read ()))

        {

          if ($FileName !="." && $FileName !="..")

           {

              $files[$k]['name'] = $Filename;

              $files[$k]['last'] = filectime("image/$Filename");

           }

     }        

        $d->close ();

array_multisort($files, SORT_DESC, SORT_NUMERIC);

foreach ($files as $file) {

   echo "<option value=\"$file\">$file</option>\n";

 

?>

 

I was thinking like this.  You might need to change the array_multisort parameters if I didn't get it right. XD

Link to comment
Share on other sites

That didn't work out hitman6003. The page won't load. Is it possible that your script would need to be added in the upload section of the site ?

 

Basically, the way it works is that I upload a picture using a page, and then use the "add a product" section to write the article and select the appropriate image. The code snippet I posted was from the "add a product" form.

 

Here is the code for the upload page :

 

<HTML>

<HEAD>

<TITLE>Admin Shop Stop Shopping Center</TITLE>

<HEAD>

 

<?php

 

include ("CommonFunction.inc.php");

 

?>

 

<script language="Javascript" type="text/javascript">

    <!--

    function AddFileSlotToList() {

 

FileList.innerHTML = FileList.innerHTML + "<input name='FileUpload[]' type='file'><br>";

 

    }

 

    //-->

</script>

 

 

<?

 

if (!eregi("admin.php", $PHP_SELF)) { die ("Access Denied"); }

include("header.php");

include("config.php");

require("VarCWC02.php");

 

 

if ($AddPicture):

 

$Error= UploadFileToDisk ("image/");

 

 

OpenTable();

echo ("<center><H4>Product List</h4></center>" );

$page = "admin.php?op=PhotoList";

 

if (!$Error)

{

  Echo ("<BR><center>Les Photos ont été rajoutés</center>");

  Echo ("<BR><center>Click <a href=$page>here</a> or wait 3 seconds to go back to Admin Center</center>");

 

}

else

{

  Echo ("<BR><center>Erreur dans l'ajout de photo</center>");

  Echo ("<BR><center>Click <a href=$page>here</a> to go back to Photo List</center>");

}

CloseTable();

 

?>

<script language="Javascript" type="text/javascript">

    <!--

    function gotoThread() {

    window.location.href="<?php print $page ?>";

    }

 

<?

   if (!$Error) echo ("window.setTimeout('gotoThread()', 3000)");

?>

    //-->

</script>

<?

 

else:

 

// Request info

// SYSTEM MENU

 

// MAIN MENU

include("menushop.php");

 

Echo ("<BR>");

 

OpenTable();

?>

 

<form enctype="multipart/form-data" action="admin.php?op=PhotoList" method="POST">

 

<div id=FileList>

<b>Liste des Fichier</b><br>

<input name="FileUpload[]" type="file"><br>

<input name="FileUpload[]" type="file"><br>

<input name="FileUpload[]" type="file"><br>

<input name="FileUpload[]" type="file"><br>

<input name="FileUpload[]" type="file"><br>

</div>

 

<br><br><button onclick='AddFileSlotToList()'>Ajouter une entrée de fichier</button><br><br>

 

 

<input type="submit" name="AddPicture" value="Send File">

</form>

 

<?

 

CloseTable();

 

 

endif;

 

include ("footer.php");

?>

 

Dark Water, I am going to try your suggestion. Do I need to replace the same portion of the code that hitman did ?

 

Thanks for the help guys

 

 

 

EDIT :

 

Might have something to do with this as well

 

<?

 

 

echo ("<TABLE><tr>");

 

$Img=0;

 

 

    $d = dir ("image");

      while(false !== ($FileName=$d->read ()))

{

  echo ("<td>");

 

  if ($FileName !="." && $FileName !="..")

{

  echo "<option value=\"$FileName\">$FileName</option>\n";

}

 

echo ("<br><b>$FileName</b></td>");

 

$Img++;

 

if ($Img >= 5)

{

$Img = 0;

echo ("</tr><tr>");

}

      }

 

$d->close ();

 

echo ("</tr></table>");

 

?>

Link to comment
Share on other sites

Yes, there seems to be a mistake, but as I am extremely new to any of this, I don't have a clue what to look for. Here's what my browser said when I tested it :

 

Parse error: syntax error, unexpected T_ENDIF in /home/lirelana/public_html/admin/modules/ShopStop/index.php on line 339

 

Which seems to refer to this portion :

<p>

<?

CloseTable();

endif;

include("footer.php");

?>

 

Again thanks for the help guys, sorry for being such a painful newbie.

Link to comment
Share on other sites

<?

//

      $k = 0;

      echo "<option value=\"\">Aucune</option>\n";

        $d = dir ("image");       

      while(false !== ($FileName=$d->read ()))

        {

          if ($FileName !="." && $FileName !="..")

            {

              $files[$k]['name'] = $Filename;

              $files[$k]['last'] = filectime("image/$Filename");

            }

          $k++;

      }       

        $d->close ();

array_multisort($files, SORT_DESC, SORT_NUMERIC);

foreach ($files as $file) {

    echo "<option value=\"$file\">$file</option>\n";

}

?>

 

Ahaha, I found my error.  And you know this replaces that ENTIRE portion of code, correct?  Not just the while loop that he was replacing. 

 

P.S: Yay, a French site. :D

Link to comment
Share on other sites

French CANADIAN, thank you ;)

 

I managed to find the missing } by myself by closely examining your code. But now all my images are gone from my menu, and I only have the choice between "aucune" (none) and "Array".

 

Here's what the code looks like right now :

 

<p>Image</p>

<td>

 

 

 

    <select name="sImage" onchange="ChangeImage('ImgProduct','image/'+this.options[selectedIndex].value)">

<?

//

      $k = 0;

      echo "<option value=\"\">Aucune</option>\n";

        $d = dir ("image");       

      while(false !== ($FileName=$d->read ()))

        {

          if ($FileName !="." && $FileName !="..")

            {

              $files[$k]['name'] = $Filename;

              $files[$k]['last'] = filectime("image/$Filename");

            }

      }       

        $d->close ();

array_multisort($files, SORT_DESC, SORT_NUMERIC);

foreach ($files as $file) {

    echo "<option value=\"$file\">$file</option>\n";

}

 

?>

 

</select>

 

 

</td>

Link to comment
Share on other sites

I know what Aucune means. xD

 

And I wasn't paying attention before to something.  Here:

 

<?

//

      $k = 0;

      echo "<option value=\"\">Aucune</option>\n";

        $d = dir ("image");     

      while(false !== ($FileName=$d->read ()))

        {

          if ($FileName !="." && $FileName !="..")

            {

              $files[$k]['name'] = $Filename;

              $files[$k]['last'] = filectime("image/$Filename");

            }

          $k++;

      }     

        $d->close ();

array_multisort($files, SORT_DESC, SORT_NUMERIC);

foreach ($files as $file) {

    echo "<option value=\"{$file['name']}\">{$file['name']}</option>\n";

}

?>

 

=)

Link to comment
Share on other sites

Alright then, I won't try to teach you French anymore ;)

 

Now there seems to be a long list of pictures, but no names are displayed, and no picture is displayed when you select any of the "ghost" entries.

Link to comment
Share on other sites


<?
//
function sort_ctime($a, $b) 
{
	if ($a['last'] == $b['last']) {
		return 0;
	}
	return ($a['last'] < $b['last']) ? -1 : 1;
}


      $k = 0;
      echo "<option value=\"\">Aucune</option>\n";
        $d = dir ("image");       
      while(false !== ($FileName=$d->read ()))
         {
           if ($FileName !="." && $FileName !="..")
            {
               $files[$k]['name'] = $FileName;
               $files[$k]['last'] = filectime("image/$FileName");
            }
           $k++;
      }       
         $d->close ();
usort($files, 'sort_ctime');
foreach ($files as $file) {
    echo "<option value=\"{$file['name']}\">{$file['name']}</option>\n";
}
?>

 

Tested it, it works.  I changed two things:

1) Change array_multisort to usort and created a sorting algorithm for this.

2) Used the right capitalization on $FileName.  That's what caused those "ghost entries" before, by the way.

Link to comment
Share on other sites

Awesome, it works now ! You just boosted my productivity by approximatively 500% !

Thanks a lot pal, I was really tired of spending 5 minutes scrolling down that bloody menu every time I added a product...

 

If I didn't have so much trouble paying the rent each month, I'd gladly paypal you a few bucks, but I can't spare anything right now. If you're into astronomy or birding, or just need some binoculars or a tripod, come look at our website : http://lirelanature.com , and mention you've helped David with the website. I'll manage a friendly discount for you.

 

Thanks again,

 

Dave

Link to comment
Share on other sites

Hello again,

 

We've been using the last script from DarkWater for a few days. It works fine, except for one detail : when we try to modifiy a product, we have to retrieve the picture in the scroll-down menu. It wasn't so before, the edit menu "remembered" what image file it used.

 

I was wondering if anybody could tweak so that it works that way ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.