Jump to content

Complex sort for forum software


bigmatt19

Recommended Posts

On the one hand I hope this is simple, but on the other I'd like to think of myself as being pretty good with this stuff  ;)... so here's the issue... I am creating a threaded message forum from scratch.  I have figured out a way to display the threads using a recursive algorithm... but it seems inefficient.  The forum would be sorta like slashdot's in that the replies to each post will be nested...

I have a two tables: messages and messagerelation.  Messages contains:
[b][u]messages[/u][/b]
[table]
[tr]
[td][b]messageID[/b][/td]
[td][b]parentID[/b][/td]
[td][b]etc.[/b][/td]
[/tr]
[tr][td]0[/td][td][i]NULL[/i][/td][td][i]NULL[/i][/td][/tr]
[tr][td]3[/td][td]1[/td][td]etc..[/td][/tr]
[tr][td]4[/td][td]2[/td][td]etc..[/td][/tr]
[tr][td]5[/td][td]2[/td][td]etc..[/td][/tr]
[tr][td]1[/td][td]0[/td][td]etc..[/td][/tr]
[tr][td]2[/td][td]1[/td][td]etc..[/td][/tr]
[tr][td]6[/td][td]3[/td][td]etc..[/td][/tr]
[/table]

Then messagerelationships contains:
[b][u]messagerelationships[/u][/b]
[table]
[tr][td][b]messageID[/b][/td][td][b]relatedto[/b][/td][/tr]
[tr][td]1[/td][td]0[/td][/tr]
[tr][td]2[/td][td]1[/td][/tr]
[tr][td]2[/td][td]0[/td][/tr]
[tr][td]3[/td][td]1[/td][/tr]
[tr][td]3[/td][td]0[/td][/tr]
[tr][td]4[/td][td]2[/td][/tr]
[tr][td]4[/td][td]1[/td][/tr]
[tr][td]4[/td][td]0[/td][/tr]
[tr][td]5[/td][td]2[/td][/tr]
[tr][td]5[/td][td]1[/td][/tr]
[tr][td]5[/td][td]0[/td][/tr]
[tr][td]6[/td][td]3[/td][/tr]
[tr][td]6[/td][td]1[/td][/tr]
[tr][td]6[/td][td]0[/td][/tr]
[/table]

Ok.. so here is what I want to get when I perform a query to grab every message related to a messageID:
[b][u]RESULT DESIRED[/u][/b]
[table]
[tr]
[td][b]messageID[/b][/td]
[td][b]parentID[/b][/td]
[td][b]etc.[/b][/td]
[/tr]
[tr][td]1[/td][td]0[/td][td]etc..[/td][/tr]
[tr][td]2[/td][td]1[/td][td]etc..[/td][/tr]
[tr][td]4[/td][td]2[/td][td]etc..[/td][/tr]
[tr][td]5[/td][td]2[/td][td]etc..[/td][/tr]
[tr][td]3[/td][td]1[/td][td]etc..[/td][/tr]
[tr][td]6[/td][td]3[/td][td]etc..[/td][/tr]
[/table]

but what I get:
[b][u]ACTUAL RESULT[/u][/b]
[table]
[tr]
[td][b]messageID[/b][/td]
[td][b]parentID[/b][/td]
[td][b]etc.[/b][/td]
[/tr]
[tr][td]1[/td][td]0[/td][td]etc..[/td][/tr]
[tr][td]2[/td][td]1[/td][td]etc..[/td][/tr]
[tr][td]3[/td][td]1[/td][td]etc..[/td][/tr]
[tr][td]4[/td][td]2[/td][td]etc..[/td][/tr]
[tr][td]5[/td][td]2[/td][td]etc..[/td][/tr]
[tr][td]6[/td][td]3[/td][td]etc..[/td][/tr]
[/table]

The query I am using I knew wouldn't give me what I wanted, but I hoped it would be a good start to inspire me to write the correct query.  Here it is:
[code]SELECT * FROM messages
WHERE messageid IN (SELECT messageid
                FROM messagerelationships
                WHERE relatedto = 1)
      OR messageid = 1
ORDER BY parent;[/code]

Any help you can give would be a life-saver!

TIA!
Matt
Link to comment
Share on other sites

The desired result would allow me to produce something like this:
[pre]
Original Message (1)
  Reply to Original (2)
      Reply to 2 (4)
      Reply to 2 (5)
  Reply to Original (3)
      Reply to 3 (6)
[/pre]

And really to be able to nest as deep as I want... dynamically.

Hope that clarifies things a bit.

-Matt
Link to comment
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.