Jump to content

me agian up for some more help


joshbro90

Recommended Posts

right

i have a site with a voting system people vote for things then on the things page it will show there rating

if 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 votes

sorry its very brief and please say if you dont get what im saying

thanks in advanced
Link to comment
Share on other sites

[!--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--]
right

i have a site with a voting system people vote for things then on the things page it will show there rating

if 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 votes

sorry its very brief and please say if you dont get what im saying

thanks 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 TABLE
echo $i;

?>
Link to comment
Share on other sites

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 TABLE
echo $i;

?>

Am i correct in waht i sat above in bold ?

also how is the connection made to the database ?

thanks
Link to comment
Share on other sites

[!--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 TABLE
echo $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]
Link to comment
Share on other sites

[!--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 22
0
line 22 is below

while ($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.
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.