FireDrake Posted November 11, 2007 Share Posted November 11, 2007 hi, in my code, im trying to get the poll answers out of my database. Poeple have voted in the poll, their answers are in the database table named poll_ip(id, ip, poll_ant = answer), and now I want to make a top 3 out of that. I think I was thinking to complicated when I was trying to make that top 3, bcuz I am selecting everything from poll_ip, order it by poll_ant descending and limit it 0,3. Then I make a loop with while. Anyway, when I output this, I have indeed everything descended, but i wanted only one answer of each kind. I know I have to add some row to my table with count, but I really don't know how to use that. The next thing I tried, was getting the link to one of the top3 out of the database, from another table called link(lid, link, image). I tried to have the link id (lid) the same as poll_ant but that didn't work. Can anyone please help me getting the top3 part up? I need to finish this 4 school.. Here is the part of that code: case "top3": echo "<span>De wedstrijden in de top 3!</span>"; echo"</div>"; echo"<div class=\"stream-ads\">"; echo"<div class=\"ads\">"; echo"<img src=\"/goal.jpg\">"; echo"<div class=\"c pt\">"; echo"</div>"; echo"</div>"; echo"<div class=\"stream\">"; $query = "SELECT * FROM poll_ip ORDER BY poll_ant DESC LIMIT 0,3"; $result = mysql_query ($query); $nummer = 1; echo "<br/><br/><br/></div><br/><div class=\"stream2\">"; echo "<center><table><tr><td>"; while ($rij = mysql_fetch_array ($result)) { echo "<br /><br /><table><tr><td>Nummer ".$nummer.": <tr /><td><b>Wedstrijd nummer:</b><td><b>".$rij ['poll_ant']."</b>"; $query = "SELECT * FROM link WHERE lid ='$rij ['poll_ant']'"; $result = mysql_query ($query); $rij = mysql_fetch_array ($result); echo "<tr><td><a href=".$rij ['link'].">Bekijk wedstrijd</a><tr></table>"; $nummer++; } echo "</td></tr></table></center>"; break; Quote Link to comment Share on other sites More sharing options...
toplay Posted November 11, 2007 Share Posted November 11, 2007 You stated "...one answer of each kind". Each "kind" of what? You want to look into using "GROUP BY" in your query. The column to group by would be what ever your calling as each "kind". http://dev.mysql.com/doc/refman/5.0/en/select.html http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html Topic moved to MySQL area. Quote Link to comment Share on other sites More sharing options...
FireDrake Posted November 11, 2007 Author Share Posted November 11, 2007 I have 15 answers in the poll, and all of them are valued as numbers (1-15) thats what I meant by each kind say that 13 people voted on number 3, 11 on number 1 and 6 on number 8. Now i want to show number 3 in first, number 1 on second, and number 8 on third, because its a top 3, but it shows just the first three records in my table, because i don't have anything that can count all of the same numbers (example 13 of number 3) in the database, and i realy don't know how to. Quote Link to comment Share on other sites More sharing options...
toplay Posted November 11, 2007 Share Posted November 11, 2007 Try something like this: $query = "SELECT count(poll_ant) AS total, pi.* FROM poll_ip pi GROUP BY poll_ant ORDER BY total DESC LIMIT 0,3"; Quote Link to comment Share on other sites More sharing options...
FireDrake Posted November 11, 2007 Author Share Posted November 11, 2007 thanks, that solves one of my problems =) and btw that site is very nice, im reading it to solve further problems of the same kind in the future the next thing was that I tried to get something out of a database while the loop is still on, so i have a table with links called link(lid, link, image) I'm using a mysql query in a: while ($row = $query){ $row = $some other query to get the links out of the other table }, But ill get an error when I do so: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\Program Files\wamp\www\Index.php on line * Here is the code again, please have a look at it (only posting the while part) while ($rij = mysql_fetch_array ($result)) { echo "<br /><br /><table><tr><td>Nummer ".$nummer.": <tr /><td><b>Wedstrijd nummer: </b><td><b>".$rij ['poll_ant']."</b>"; $query = "SELECT * FROM link WHERE lid ='$rij ['poll_ant']'"; $result = mysql_query ($query); $rij = mysql_fetch_array ($result); echo "<tr><td><a href=".$rij ['link'].">Bekijk wedstrijd</a><tr></table>"; $nummer++; } Quote Link to comment Share on other sites More sharing options...
FireDrake Posted November 11, 2007 Author Share Posted November 11, 2007 nvm, solved, I had to change the second $query, $result and $rij to $query1, $result1, $rij1 and change the query from $query = "SELECT * FROM link WHERE lid ='$rij['poll_ant']'"; to $query = "SELECT * FROM link WHERE lid ='".$rij['poll_ant']."'"; tnx to daukan for the query change Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.