1

I am trying to implement something similar to a functionality on Stack Overflow.

On a user's profile page, where there are listed the Answers by user, when you click on the link to question, you are redirected to the question page and the answer is highlighted. Answer by a user

This works even if the answer clicked on happens to be lying on like 2nd page below, the page is displayed.

Paged answer

How can I implement this? The only way I can think of right now is by implementing the following steps:

When listing the answers by a user..

  • Find out the list of answers to every question the user has answered
  • Sort the list
  • Find out where the answer by user lies in that list
  • This number can be to create a link to the page where answer by the user lies on question page

But this seems very memory-consuming process. Is there an easier way to do this?

rene
  • 41,474
  • 78
  • 114
  • 152
vikmalhotra
  • 9,981
  • 20
  • 97
  • 137

2 Answers2

1

There's no 4-step process, you just make one database query for the user's answers from your answers table. The question ID is part of the same row, so you have everything you need to link to the question and pass on the ID of the answer to highlight.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • What happens if the user's answer lies on 2nd page of answers like over here - http://stackoverflow.com/questions/3076078/check-if-at-least-2-out-of-3-booleans-is-true/3098764#3098764 - How will I highlight the answer in that case? – vikmalhotra Feb 04 '11 at 07:02
  • 1
    It's the code that displays the question's job to figure that out and display the appropriate page, not the code that links to the answer. It's just another SQL query, though. There's no significant extra memory overhead in doing this, it's just a little logic in your code to figure out which page to display instead of automatically displaying the first page of answers. – Dan Grossman Feb 04 '11 at 07:04
  • Okk. I got it. Thanks man. I was connecting it the wrong way. :) – vikmalhotra Feb 04 '11 at 07:08
0

That's the idea.

You have to retrieve the results for the whole page, because you're displaying the whole page. You have to sort as well, because that's what you do when you display a page. And in that case, you'll know which page your desired answer is own because of how far down it is in the list.

tylerl
  • 30,197
  • 13
  • 80
  • 113
  • May be you misunderstood my question. The idea that you describe is pertinent to the scenario like I am displaying a question page in stackoverflow. I am talking about the one when you display a user's profile page and I need to display the answers, like they are displayed over here http://stackoverflow.com/users/86060/tylerl – vikmalhotra Feb 04 '11 at 06:57
  • 1
    The profile page neither knows nor cares which page the answer is on in the question view. It links directly to the answer. When you click the link for the answer, it determines which answer page to show. The same link remains valid even as more answers are added and the linked answer shifts from one page to another. – tylerl Feb 04 '11 at 07:21