Jump to content

Recommended Posts

I am making a script to delete images from user albums, each photo has a value to show what place in the album they come, when I delete an image the order will change and may look like this "1, 2, 3, 7, 8...." to I am trying to change this so the order does not change and will always increase by one every time. "1, 2, 3, 4, 5......".

 

Here is what I have tried....

//count how many photos are remaining in the album
$query = mysql_query("SELECT recordListingID FROM photos WHERE usr_id='{$uid}' AND album='{$aid}' ORDER BY recordListingID ASC");
$photoCount = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
//if there are still images remaining update recordListingID
if ($photoCount != 0)
{
	for($i=1;$i>=$photoCount;$i++)
	{
		$recordListingID[$i] = $row['recordListingID'];
		$update_listing = mysql_query("UPDATE photos SET recordListingID='{$i}' WHERE recordListingID='{$recordListingID[$i]}' AND usr_id='{$uid}' AND album='{$aid}'");
	}
}
else
exit();

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/208761-for-loop-to-re-index-data/
Share on other sites

You didn't say what the problem is.

 

Try this instead:

<?php
$q = "SELECT recordListingID FROM photos WHERE usr_id='$uid' AND album='$aid' ORDER BY recordListingID ASC";
$query = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
$i = 0;
while ($row = mysql_fetch_assoc($query) {
     $uq = "UPDATE photos SET recordListingID='$i' WHERE recordListingID='{$recordListingID[$i]}'";
     $update_listing = mysql_query($uq) or die("Problem with the update query: $uq<br>" . mysql_error());
     $i++;
}
?>

 

Ken

 

 

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.