Jump to content

Recommended Posts

How to sort rows without skipping numbers when its deleted? For example I use row numbers to display post number in forum script. First message in topic have post_numb 1, second 2, etc...I use it for anchors (permanent link) - viewtopic.php?forum=id&postid=id#post_number . If I have 10 posts in topic I have following numbers: 1,2,3,4,5,6...etc. If I delete post 5, I get this: 1,2,3,4,6,7...

Script for increase post number:

$postnum = mysql_num_rows($result2)+1;

row is post_numb:

00003z.gif

I want to sort this row in order (on picure post with numb 5 is deleted), so I need to post 6 became 5, 7 became 6 etc.

 

Here how it looks

00001a.gif

 

and when I delete post#5 I gotthis:

 

00002.gif

Link to comment
https://forums.phpfreaks.com/topic/206247-sort-rows-mysql/
Share on other sites

maybe every time you have the delete post operation called, you can add a function called to reorder the post_numb values to avoid having missing numbers. or maybe you should just get them ordered from database after the post_num and then use an index to display #1,#2, etc in the php.

hope this helps!

Link to comment
https://forums.phpfreaks.com/topic/206247-sort-rows-mysql/#findComment-1079017
Share on other sites

This lookss kinda like a forum, surely posts should be sorted by date/time.

Yes, thats minimalistic forum (about 50kb) on which I currently working.

Here is test online. Still no .css styles. I just finished template system.

 

maybe every time you have the delete post operation called, you can add a function called to reorder the post_numb values to avoid having missing numbers. or maybe you should just get them ordered from database after the post_num and then use an index to display #1,#2, etc in the php.

hope this helps!

Thanks for note. But how to reorder post_numb row ?

Link to comment
https://forums.phpfreaks.com/topic/206247-sort-rows-mysql/#findComment-1079057
Share on other sites

My idea will be to have a separate function where you get all results from database with (post_numb>just deleted post_numb) ordered by post_numb ASC and using an counter(in PHP) to update each  field post_numb  with that counter(that starts from just deleted post_numb to the total number of records).  You call this function every time you do a delete post and have the deleted post_numb as a param for the function.

 

If you have a lot of records in the database, this function might be a little time consuming, so you might just want with the second option I have you.

 

Hope this is helpful!

 

Link to comment
https://forums.phpfreaks.com/topic/206247-sort-rows-mysql/#findComment-1079070
Share on other sites

You are making this more difficult than it needs to be. The records should have a unique ID which doesn't have to have anything to do with the sort order. Records should be ordered by the timestamp the record was created (as thorpe suggests). You can then add an index number to be "displayed" to the user within the PHP code that creates the HTML (as miancu suggests).

 

Link to comment
https://forums.phpfreaks.com/topic/206247-sort-rows-mysql/#findComment-1079072
Share on other sites

Thanks.

I dont need that extra row for that. I use it only cause I have page system (I though that would be easier).

Here is how I solve it:

$counter= 1;

while ... mysql_fetch_assoc{

$temp_numb = $posts_row['post_id'];

$temp_numb = ($posts_row['post_id'] - $temp_numb+$counter + ($post_per_page*mysql_real_escape_string($_GET['page']))-$post_per_page);

...drawing tables$counter += 1;}

 

Thanks guys.

Link to comment
https://forums.phpfreaks.com/topic/206247-sort-rows-mysql/#findComment-1079082
Share on other sites

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.