Loopity Loop Help

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
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Loopity Loop Help

Post by SheDesigns »

Creating an app that allows the user to upload 6 photos, it saves into the SQL database with an order.

If someone deletes Photo2, I need it to move Photo3 to Photo2, Photo4 to Photo3, Photo5 to Photo4, Photo6 to Photo5 -- leaving Photo 6 blank.

I know that I would need to do this with a loop, just a little stumped how.

Code: Select all

//if they deleted last photo, don't bother
if($order < 6)
{
$query = "select * from propertyimages where property = '$property'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
 
// See if any files apply
$query = "select * from propertyimages where property = '$property' and `order` > $order";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0)
{
  //Change the Files, -1 from order for anything that applies, Make a Loop
  $i = $count;
  ???
 
}
}
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Loopity Loop Help

Post by VladSun »

Hello, madam :)
Could you please, post the details of your DB design.
Also, I need you to clarify - do you need to change the names of the files or ...?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Loopity Loop Help

Post by Eran »

You don't need a loop, you can update all the other images with one query -

Code: Select all

 
if($count > 0) {
  $query = "UPDATE `propertyimages` SET `order` = `order` - 1 WHERE `property` = '"  . $property . "' AND `order` > " . $order;
  mysql_query($query);
}
 
You should be removing the deleted image first though, so you won't have two images with the same order.
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Loopity Loop Help

Post by SheDesigns »

:D Thank you Pyrtin!! That worked perfectly.
Post Reply