Page 1 of 1

Loopity Loop Help

Posted: Fri Nov 21, 2008 8:04 pm
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;
  ???
 
}
}
 

Re: Loopity Loop Help

Posted: Fri Nov 21, 2008 8:26 pm
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 ...?

Re: Loopity Loop Help

Posted: Fri Nov 21, 2008 8:30 pm
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.

Re: Loopity Loop Help

Posted: Fri Nov 21, 2008 8:40 pm
by SheDesigns
:D Thank you Pyrtin!! That worked perfectly.