Jump to content

Renumbering MySQL row/entires of images for output


cgm225

Recommended Posts

I have MySQL rows/entries for images stored in a table with the following structure:

 

ID(autoincrement)  |      filename      |    timestamp

1                                image1.jpg          some-timestamp-1

2                                image2.jpg          some-timestamp-2         

3                                image3.jpg          some-timestamp-3

4                                image4.jpg          some-timestamp-4

 

I then query and output the images ORDERed by either ID or timestamp.  However, if I want to rearrange the order they sort based on the ID, what would be the best way to do that?  Or should I add another column like "album_order"?  But even if I do that, how do I do the actually renumbering of the entries so their new order is something like this for example:

 

ID(autoincrement)  |      filename      |    timestamp

1                                image1.jpg          some-timestamp-1

2                                image4.jpg          some-timestamp-4         

3                                image2.jpg          some-timestamp-2

4                                image3.jpg          some-timestamp-3

 

So in this example, I moved image4.jpg up two places in the order (based on ID)

 

 

Link to comment
Share on other sites

you can sort any way you like and just use a dummy number for the order instead of the ID

 

<?php
$sql = "SELECT * FROM `tablename` ORDER BY `filename` ASC";
$result = mysql_query($sql) or die(mysql_error());
$i=1;
while($r=mysql_fetch_assoc($result)){
echo $i ." ".$r['filename']." ".$r['timestamp'];
$i++;
}
?>

 

Ray

Link to comment
Share on other sites

Thank you so much for the help.

 

Let me restate what I am trying to get at.  I have a gallery script, and I want to be able to change the order in which the pictures are displayed using a dropdown menu that will read something like "Move to position [dropdown menu with a range of numbers equalling the number of photos in the album (except the number of the position the photo is currently in)]".  Any ideas?

 

The photos are stored in a table as shown above.

Link to comment
Share on other sites

When viewing an individual photo in an album, I want to have a dropdown menu under it that will read something like "Move to position [dropdown menu with a range of numbers equaling the number of photos in the album (except the number of the position the photo is currently in)]". 

 

Any ideas?

 

(Thank you for your help!)

Link to comment
Share on other sites

I found a function which will move a portion of an array up or down and preserve the keys. This example i use a couple of arrows to go up or down, I have not tried the dropdown yet but you might be able to do it with a quick loop to drop the id down a couple spots at once.

 

Here is the function

<?php
function array_move_element($array, $value, $direction = 'up') {

    $temp = array();

    if(end($array) == $value && $direction == 'down') {
        return $array;
    }
    if(reset($array) == $value && $direction == 'up') {
        return $array;
    }

    while ($array_value = current($array)) {

        $this_key = key($array);

        if ($array_value == $value) {
            if($direction == 'down') {
                $next_value = next($array);
                $temp[key($array)] = $next_value;
                $temp[$this_key] = $array_value;
            } else {
                $prev_value = prev($array);
                $prev_key = key($array);
                unset($temp[$prev_key]);
                $temp[$this_key] = $array_value;
                $temp[$prev_key] = $prev_value;
                next($array);
                next($array);
            }
            continue;
        } else {
            $temp[$this_key] = $array_value;
        }

        next($array);
    }
    return $temp;

}
?>

 

And this is what I used to show and move the id's

<?php
if(!isset($_GET['ids'])){
  $list = array();
  $sql = "SELECT * FROM roster ORDER BY sort ASC LIMIT 5";
  $res = mysql_query($sql) or die(mysql_error());
  while($r = mysql_fetch_assoc($res)){
  $list[] = $r['id'];
  }
$ids = implode("|", $list);
}


if(isset($_GET['moveid'])){
$list = explode("|", $_GET['ids']);
$move = explode("|", $_GET['moveid']);
$list = array_move_element($list, $move[0], $move[1]);
$ids = implode("|", $list);
}

foreach($list as $id){
$sql = "SELECT handle, id, real_name FROM roster WHERE id = '$id'";
$res = mysql_query($sql) or die(mysql_error());
$r = mysql_fetch_assoc($res);
echo $r['id']." ".$r['handle']." ".$r['real_name']." <a href=\"$self?ids=$ids&moveid=$id|up\"><img src=\"images/upshrink.gif\" border=0></a>  <a href=\"$self?ids=$ids&moveid=$id|down\"><img src=\"images/downshrink.gif\" border=0></a><br />\n";
}
?>

 

Obviously change the query I used just a table I had set up. If you can't figure out the drop down part let me know.

 

Ray

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.