My table 'list' is: `id` `name` `votes_up` `votes_down` I want to output this to a webpage, but instead of having votes_up and votes_down, I just want 'Votes', which would obviously be (votes_up - votes_down) How would I express this in PHP/SQL?
Cheers! Would you be able to tell me how I can produce a page with, say, the Top 10 voted entries? Would that require a loop?
$sql = 'SELECT id,name, votes_up - votes_down as Votes ORdeR by votes_up desc;'; this does sort it from most votes_up to 0 votes_up
The above query will not work if something has more votes_down than UP. You need to ORDER BY Votes DESC, not BY votes_up DESC. $sql = 'SELECT id,name, votes_up - votes_down as votes ORDER BY votes DESC LIMIT 10'; Code (markup):
This is what's happening right now... http://www.ijustcantgetenough.net/topten.php It's looping the whole page! this is my code <body> <?php /** Display the results from the database **/ $q = "SELECT id, title, votes_up - votes_down as votes FROM entries ORDER BY votes DESC LIMIT 3"; $r = mysql_query($q); if(mysql_num_rows($r)>0): //table is non-empty while($row = mysql_fetch_assoc($r)): $net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down $subdate = date('F d, Y', strtotime($row['time'])); ?> <div class='entry'> <center><img src="images/logo.png" /><div class="menu"><?php include("menu.php"); ?></div></center> <span class='link'> <h2>Top Ten</h2> <p>Soon you will be able to see the Top 10 things that visitors Just Can't Get Enough of!</p> <h1><?php echo $row['title']; ?></h1> </span> <p> <?php if($row['time']!=0){ echo "Submitted on " .$subdate.""; } ?><br /> <span class='votes_count' id='votes_count<?php echo $row['id']; ?>'><?php echo $net_vote." votes"; ?></span> <span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'> <a href='javascript:;' title='Vote Up' class='vote_up' id='<?php echo $row['id']; ?>'>Vote Up!</a> <a href='javascript:;' title='Vote Down' class='vote_down' id='<?php echo $row['id']; ?>'>Vote Down!</a><br /> </span> <a href="/"><img src="images/refresh_icon.png" width="16" height="16" border="0" title="Another wan!" /></a> <a href="/index.php?report=<?php echo $row['id']; ?>"><img src="images/flag.gif" width="16" height="16" border="0" title="Flag for abuse" /></a></p> PHP:
you do not have any </div> for class 'entry' there. To have top10, you have to check also votes down, so $sql = 'SELECT id,name, votes_up - votes_down as Votes ORdeR by votes_up-votes_down desc;';
I also don't have a </body>... that's only a snippet of the code (the relevant part of that page) I'll try that query, thanks
Have you run SELECT id, title, votes_up - votes_down as votes FROM entries ORDER BY votes DESC LIMIT 3 Code (markup): Directly in MySQL console or other administration tool?
Just did it there mate and it works a treat. Now I just need to figure out how to echo those results onto a PHP page