简体   繁体   中英

PHP / mySQL ORDER BY issue

Click the "strip'slashes'" link to see the page I'm referring to

As you can see, I have this voting system setup for different definitions of phrases. In the database the votes are dealt with in a table of their own, but are linked to the 'phrases' table via IDs. I want to order the phrases by the balance of votes, but as the votes aren't part of the table I'm selecting in my SQL query, I'm not sure how to do it.

I'm able to create the balance of votes, which can be seen beneath each phrase definition, but I can't work out how to ORDER BY them. I thought about creating the 'balance' and then updating it into the database when a new vote is registered, but as the insertion of votes is into the 'vote' table, I can't recall it when recalling the phrases.

Can somebody point me in the right direction please!

Code:

$query="select id, date, original, inotherwords, author from phrases where original='".$head."'";
$result=mysql_query($query);
$thenumber = 1;

echo '<h2>Original Phrase</h2>';
echo '<p>'.stripslashes($head).'</p><br>';
echo '<h2>In Other Words</h2>';
while($row = mysql_fetch_array($result)){
    $pv = $pulse->countUpVotes($row['id']);
    $nv = $pulse->countDownVotes($row['id']);
    $balance = ($pv - $nv);


    echo '<p>'.$thenumber.'. '.stripslashes(nl2br($row['inotherwords'])).' - ';
    echo $pulse->voteHTML($row['id']);
    echo '</p>';
    echo '<p><b>Author</b> - '.stripslashes($row['author']).' - submitted on '.$row['date'].'</p>';
    echo 'Balance - '.$balance.'<br><br>';

    $thenumber++;  
}

First, your data seems sooo troublesome... Have you been working with magic_quotes turned on and you even saved data in the database with the slashes in it? If so, clear all slashes from the database, disable magic_quotes AND learn about sql injection and how to protect yourself against it. If you don't know what the hell I'm talking about, then don't do nothing, but it sucks to be coding always with stripslashes(...)

Second... I take that the votes for each phrase get counted on another table in the same database, right? If so, you need to join both tables in your query so you'll be able to order by data in the votes table

SELECT p.*, v.upvotes, v.downvotes, (v.upvotes - v.downvotes) as balance
FROM phrases p
LEFT JOIN votes v ON p.id = v.phrase_id
WHERE ...
ORDER BY balance DESC

I'm not 100% sure I understood the issue... but I guess what you want is to do a JOIN .

For instance:

SELECT p.id, p.date, p.original, p.inotherwords, p.author, v.vote  
FROM phrases p
LEFT JOIN votes v ON v.id_phrase = p.id
WHERE original = ...
ORDER BY v.vote DESC

Use join for your query to get the desired result set. It will perform a lot better than looping over all the "phrases" records in php.

SELECT 
  p.id, p.date, p.original, p.inotherwords, p.author 
FROM 
  phrases p
LEFT JOIN 
  votes v on (v.id_phrase = p.id)
WHERE 
  original='".$head."'"
ORDER BY
  v.vote DESC

(UNTESTED)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM