joshbro90 Posted April 30, 2006 Share Posted April 30, 2006 righti have a site with a voting system people vote for things then on the things page it will show there ratingif i have a table in a mysql database storing the amout of votes the things have. how could i create a page to show the thing with the highest amout of votessorry its very brief and please say if you dont get what im sayingthanks in advanced Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted April 30, 2006 Share Posted April 30, 2006 [!--quoteo(post=370110:date=Apr 30 2006, 11:23 AM:name=joshbro90)--][div class=\'quotetop\']QUOTE(joshbro90 @ Apr 30 2006, 11:23 AM) [snapback]370110[/snapback][/div][div class=\'quotemain\'][!--quotec--]righti have a site with a voting system people vote for things then on the things page it will show there ratingif i have a table in a mysql database storing the amout of votes the things have. how could i create a page to show the thing with the highest amout of votessorry its very brief and please say if you dont get what im sayingthanks in advanced[/quote]//THIS SCRIPT ASSUMES THAT YOUR RATINGS ARE ALWAYS NUMERIC//IT WILL WORK WITH NON-DIGIT DATA TYPES BUT THERE ARE OTHER THINGS TO CONSIDER WHEN//DEALING WITH THE COMPARISION OF ALNUM OR ALPHA CHARS<?php$i = 0;$sql = mysql_query("SELECT rate_col FROM thing_table");while ($row = mysql_fetch_array($sql)) { if ($row['rate_col'] > $i) { $i = $row['rate_col'];}//END WHILE }//$i CONTAINS THE GREATEST (OR HIGHEST) VALUE OF THE RATE COL IN THE THING TABLEecho $i;?> Quote Link to comment Share on other sites More sharing options...
joshbro90 Posted April 30, 2006 Author Share Posted April 30, 2006 Forgot to mention that im a beginer, il tell you what i inderstand onf the script//THIS SCRIPT ASSUMES THAT YOUR RATINGS ARE ALWAYS NUMERIC//IT WILL WORK WITH NON-DIGIT DATA TYPES BUT THERE ARE OTHER THINGS TO CONSIDER WHEN//DEALING WITH THE COMPARISION OF ALNUM OR ALPHA CHARS<?php[b]This is where it gets the score to place the list down in order of closeness[/b]$i = 0;[b]gets the results from mysql database[/b]$sql = mysql_query("SELECT rate_col FROM thing_table");[b]This kind of converts it to $row variable[/b]while ($row = mysql_fetch_array($sql)) {[b]This is to say if the number from there databse is greater then the $i varable[/b]if ($row['rate_col'] > $i) {[b]This tells the script to say the row of results[/b]$i = $row['rate_col'];}//END WHILE}[b]This prints the results[/b]//$i CONTAINS THE GREATEST ( OR HIGHEST) VALUE OF THE RATE COL IN THE THING TABLEecho $i;?>Am i correct in waht i sat above in bold ?also how is the connection made to the database ?thanks Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted April 30, 2006 Share Posted April 30, 2006 [!--quoteo(post=370125:date=Apr 30 2006, 01:00 PM:name=joshbro90)--][div class=\'quotetop\']QUOTE(joshbro90 @ Apr 30 2006, 01:00 PM) [snapback]370125[/snapback][/div][div class=\'quotemain\'][!--quotec--]Forgot to mention that im a beginer, il tell you what i inderstand onf the script//THIS SCRIPT ASSUMES THAT YOUR RATINGS ARE ALWAYS NUMERIC//IT WILL WORK WITH NON-DIGIT DATA TYPES BUT THERE ARE OTHER THINGS TO CONSIDER WHEN//DEALING WITH THE COMPARISION OF ALNUM OR ALPHA CHARS<?php[b]This is where it gets the score to place the list down in order of closeness[/b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]This is simply to make sure that $i won't be greater than the rate_col value during the FIRST loop. If the rate_col value from the FIRST while loop doesn't make it in to the value of " $i " then it wouldn't be accurate because the first row MySQL finds could very well be the greatest value.[!--colorc--][/span][!--/colorc--]$i = 0;[b]gets the results from mysql database[/b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Yes.[!--colorc--][/span][!--/colorc--]$sql = mysql_query("SELECT rate_col FROM thing_table");[b]This kind of converts it to $row variable[/b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Yes, but it also tells php to iterate over all records in the table based on your query. It's like telling php, " While condition = true/false, yes, no ... etc do this....". In our case, "While there are still unretrieved records in the table do this value comparison.[!--colorc--][/span][!--/colorc--]while ($row = mysql_fetch_array($sql)) {[b]This is to say if the number from there databse is greater then the $i varable[/b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Yes.[!--colorc--][/span][!--/colorc--]if ($row['rate_col'] > $i) {[b]This tells the script to say the row of results[/b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]No, This is setting the value of " $i " and when it sets the value then when it loops back the second time, the above line will be able to compare the next row with the previous row, because " $i " contains the previous result.[!--colorc--][/span][!--/colorc--]$i = $row['rate_col'];}//END WHILE}[b]This prints the results[/b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Yes.[!--colorc--][/span][!--/colorc--]//$i CONTAINS THE GREATEST ( OR HIGHEST) VALUE OF THE RATE COL IN THE THING TABLEecho $i;?>Am i correct in waht i sat above in bold ?also how is the connection made to the database ?[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$link_resource = mysql_connect('localhost', 'mysql_user', 'mysql_password');[!--colorc--][/span][!--/colorc--]thanks[/quote][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--][/color][color=#FF0000][!--colorc--][/span][!--/colorc--][color=#990000] Quote Link to comment Share on other sites More sharing options...
joshbro90 Posted April 30, 2006 Author Share Posted April 30, 2006 Right ok thanks, im going to try and give it ago, il post al ink once i am done Quote Link to comment Share on other sites More sharing options...
joshbro90 Posted April 30, 2006 Author Share Posted April 30, 2006 I Got a error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/joshbro9/public_html/test/test.php on line 220line 22 is belowwhile ($row = mysql_fetch_array($sql)) { Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted May 1, 2006 Share Posted May 1, 2006 [!--quoteo(post=370138:date=Apr 30 2006, 01:40 PM:name=joshbro90)--][div class=\'quotetop\']QUOTE(joshbro90 @ Apr 30 2006, 01:40 PM) [snapback]370138[/snapback][/div][div class=\'quotemain\'][!--quotec--]I Got a error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/joshbro9/public_html/test/test.php on line 220line 22 is belowwhile ($row = mysql_fetch_array($sql)) {[/quote]its telling you that the resource handle you supplied to the mysql_fetch_array method isn't valid.the line right above " 22 "should be:$sql = mysql_query("SELECT rate_col FROM thing_table");if you change the string variable name of the above from " $sql " then make sure you change it for the mysql_fetch_array() method.If it were simply executing but not returning results, you wouldn't get THIS error, THIS error is because something is wrong with the " $sql= ..... " line. 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.