Re: Taking X records, removing one record in the middle, then renumber...

From: Date: Mon, 06 Nov 2000 15:33:42 +0000
Subject: Re: Taking X records, removing one record in the middle, then renumber...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-23900@lists.php.net to get a copy of this message
Hi, Rather than try to renumber the actual cid of each record, I would look at changing your database and page structure to try to avoid the problem. Even the function just sent in by Pavel is going to be very hard to maintain if you have any amount of users on the site at all. Imagine if the function was running twice at the same time... your database would be so messed up you'd never get it straightened out. And if you needed to go back and analyze a change you made a week ago to cid 7 you couldn't because cid 7 now points to another record. If I understood better what you were doing, I may be able to offer a better solution. If you are just trying to make a page to edit numbered records, you could link to the cid in the page, but display a count instead of the cid. $query = "SELECT * FROM TABLE"; $result = mysql_db_query("$db_database", $query); if (mysql_numrows($result) > 0) { $count = 0; while ($r = mysql_fetch_array($result)) { $cid = $r["cid"]; $count++; echo "<a href=\"http://www.domain.com/edit.html?cid=$cid\">Edit record $count</a><br>\n"; } } Hope that helps, and makes any sense, Brian > Hi, > > I'm having a problem where by I have a table containing the following data > stored in a table in MySQL: > > |-------| > | cid | > --------- > | 1 | > | 2 | > | 3 | > | 4 | > | 5 | > --------- > > With PHP, I'm trying to work out some sort of algorithm that would, in a > reasonably optimised fashion, enable me to do the following: > > Delete record 3 - obviously mysql_query("DELETE FROM table_name WHERE cid = > 3") > Then move anything after three up one spot, hence leaving the table with 1, > 2, 3 & 4, so no holes in the numbering. > > Before anyone asks, it's not something that could be done by MySQL's > auto_increment option :) > > Thanks :) > Mark. > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#23900) next »