Jump to content

Help with mySql query


inversesoft123

Recommended Posts

I have a following fields in mySQL database.

 

1. ups = thumb up for content (votes)

2. downs = thumb down for the content (votes)

 

Now I want to create next and previous navigation links for required data and my query is.

 

$subfactor = $currentfile['ups'] - $currentfile['downs'];
$albumid =  $currentfile['id'];

  $selectprevlink="SELECT * from pf_table where (((ups - downs) <= $subfactor) AND (id != $albumid)) ORDER by (ups - downs) DESC LIMIT 1";
  $selectprevlink2=mysql_query($selectprevlink);
  $selectprevlink3=mysql_fetch_array($selectprevlink2);

 

(id < $albumid) above is also giving wrong results.

 

Please help me with this query here I want to get next file from database hence I have included id != (current album id).

However the code bellow works perfect if value of (ups - down) is not equal to $subfactor

  $selectprevlink="SELECT * from pf_table where (ups - downs) < $subfactor ORDER by (ups - downs) DESC LIMIT 1";
  $selectprevlink2=mysql_query($selectprevlink);
  $selectprevlink3=mysql_fetch_array($selectprevlink2);

 

Thanks, very much in advance.. :)

Link to comment
Share on other sites

Well, what, if a post has 5 up and 3 down, then the next post is on that has 5 up and 2 down, and previous is one that has 4 up and 3 down?

 

What if the posts have the same number of up and down votes?

Link to comment
Share on other sites

If posts has same number of ups and downs then it will rank automatically according to unique id and it works fine in index page. but when we browse content then along with back link I am trying to include quick next and previous links but getting different records from database.

 

Like in your example

 

(5 - 2) = 3

(5 - 3) = 2

(4 - 3) = 1

(5 - 5) = 0

 

5 - 5 goes last

Link to comment
Share on other sites

So how about something like SELECT TOP 1 * FROM table WHERE (for - against) > $voting_score OR ((for-against) = $voting_score and id > $current_id) ORDER BY (for - against) ASC, id ASC

 

Sorry about it being so pseudocody instead of specific. You'll have to translate to your exact code. That code is meant to have all interpolations of variables into the SQL string escaped using something like mysql_real_escape_string() as well.

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.