Jump to content


Photo

me agian up for some more help


  • Please log in to reply
6 replies to this topic

#1 joshbro90

joshbro90
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 30 April 2006 - 03:23 PM

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


#2 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 30 April 2006 - 04:49 PM

[!--quoteo(post=370110:date=Apr 30 2006, 11:23 AM:name=joshbro90)--][div class=\'quotetop\']QUOTE(joshbro90 @ Apr 30 2006, 11:23 AM) View Post[/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;

?>
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#3 joshbro90

joshbro90
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 30 April 2006 - 05:00 PM

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

This is where it gets the score to place the list down in order of closeness

$i = 0;

gets the results from mysql database
$sql = mysql_query("SELECT rate_col FROM thing_table");

This kind of converts it to $row variable
while ($row = mysql_fetch_array($sql)) {

This is to say if the number from there databse is greater then the $i varable
if ($row['rate_col'] > $i) {


This tells the script to say the row of results
$i = $row['rate_col'];

}

//END WHILE

}

This prints the results
//$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

#4 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 30 April 2006 - 05:14 PM

[!--quoteo(post=370125:date=Apr 30 2006, 01:00 PM:name=joshbro90)--][div class=\'quotetop\']QUOTE(joshbro90 @ Apr 30 2006, 01:00 PM) View Post[/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

This is where it gets the score to place the list down in order of closeness

[!--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;

gets the results from mysql database
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Yes.[!--colorc--][/span][!--/colorc--]
$sql = mysql_query("SELECT rate_col FROM thing_table");

This kind of converts it to $row variable
[!--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)) {

This is to say if the number from there databse is greater then the $i varable
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Yes.[!--colorc--][/span][!--/colorc--]

if ($row['rate_col'] > $i) {

This tells the script to say the row of results
[!--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

}

This prints the results
[!--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]
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#5 joshbro90

joshbro90
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 30 April 2006 - 05:19 PM

Right ok thanks, im going to try and give it ago, il post al ink once i am done

#6 joshbro90

joshbro90
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 30 April 2006 - 05:40 PM

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)) {


#7 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 01 May 2006 - 02:47 AM

[!--quoteo(post=370138:date=Apr 30 2006, 01:40 PM:name=joshbro90)--][div class=\'quotetop\']QUOTE(joshbro90 @ Apr 30 2006, 01:40 PM) View Post[/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.
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users