Reorder random number of items in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Reorder random number of items in a form

Post by speedy33417 »

I'm trying to do something like what Netflix does to reorder your DVD queue. It will be used in the admin page in my photo album, where different albums contain different number of pictures.
I would display a text input box with the current value of the picture's order number and the thumb picture, loop it as many times as many picture I have and close it with a submit button.

My problem is I can do it with one variable, but I'm not sure how to loop it. I need to use either $pic1 $pic2 $pic3 $pic4 or $pic[1] $pic[2] $pic[3] $pic[4].
How do I send these variables using HTML forms and making up the full name of the variable in a loop on the fly?

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not exactly sure what you're talking about, can you post some code, or explain in more detail?
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post by aliasxneo »

I'm lost as well. Are you trying to use an array of values in an HTML form?

If so it's pretty simple:

Code: Select all

$pictures = array("pic1.jpg", "pic2.jpg", "pic3.jpg");

foreach ($pictures as $picture)
{
     echo "<input type=\"text\" name=\"pictures[]\" />";
}
And when the form is submitted:

Code: Select all

foreach ($_POST['pictures'] as $picture)
{
    echo $picture;
}
But I'm really not sure what you're talking about.
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

How about having php read through your directory and count the pictures, no?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

I thought the whole Netflix thing would help, but apperently nobody understood what I meant. :oops:

I'm using database to store my picture filename and the order they should be displayed. There's more data stored, but that's not relevant here.

For example displaying an album with album_id 1. My query would be WHEN album_id = 1 ORDER BY picture_order

Code: Select all

picture_id   album_id  picture_order  picture_filename
123            1             1              IMG_0235.jpg
524            1             2              IMG_1347.jpg
678            1             3              IMG_2568.jpg
159            1             4              IMG_1234.jpg
Now I'm trying to do an admin page to reorder these pictures if needed. The admin page would display all four thumb pictures in the current order with an input box for each filled in with the current value of picture_order

Like so:

Code: Select all

+---+
| 1 |  Thumb of IMG_0235.jpg
+---+

+---+
| 2 |  Thumb of IMG_1347.jpg
+---+

+---+
| 3 |  Thumb of IMG_2568.jpg
+---+

+---+
| 4 |  Thumb of IMG_1234.jpg
+---+

   +--------+
   | UPDATE |
   +--------+
So, to switch the first two pictures I would only have to update the numbers 2 1 3 4 and hit update.

Hope this explains a little better. I can display everything, but I'm having trouble sending and receiving the data as an array.

This is my code with the loop in place, but no arrays.

Code: Select all

for ($i = 1; $i < $totalPicsInAlbum; $i++)
{
    echo "<td><input name=\"txtPicOrder\" value=\"" . $pictureOrder[$i]. "\" class=\"form1\"></td>";
    echo "<td><image border=\"0\" src=\"blah blah\"></td>";
    echo "</tr><tr>";
}

then

$pictureOrder = $_POST['txtPictureOrder'];
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

for ($i = 1; $i < $totalPicsInAlbum; $i++)
{
    echo "<td><input name=\"txtPicOrder[".$i."]\" value=\"" . $pictureOrder[$i]. "\" class=\"form1\"></td>";
    echo "<td><image border=\"0\" src=\"blah blah\"></td>";
    echo "</tr><tr>";
}

then

$pictureOrder = $_POST['txtPictureOrder'];
now you'll have the array you're looking for
Post Reply