wwfc_barmy_army Posted September 30, 2006 Share Posted September 30, 2006 Hello.I have a number of columns in a table being listed using 'print' for each. After using a voting script tutorial i found on the internet i've added it correctly. Although i cannot put the code it gives in the 'print' for showing the rating. Here is the code that i'm trying to use:[code]print "<td>$getcount = mysql_query("SELECT * FROM site where id='$_GET[id];'"); while ($result=mysql_fetch_array($getcount)){ $rating = $result['rating']; } $new_rating2 = $rating;//gets the average rating from the database and puts it in a variable. if((($new_rating2 >= 0)||($new_rating2 == 0)) && ($new_rating2 <= 0.50)){ echo "<img src='images/0.png'>"; } if((($new_rating2 >= 0.50)||($new_rating2 == 0.50)) && ($new_rating2 <= .99)){ echo "<img src='images/05.png'>"; } if((($new_rating2 >= 1.00)||($new_rating2 == 1.50)) && ($new_rating2 <= 1.49)){ echo "<img src='images/1.png'>"; } if((($new_rating2 >= 1.50)||($new_rating2 == 1.50)) && ($new_rating2 <= 1.99)){ echo "<img src='images/15.png'>"; } if((($new_rating2 >= 2.00)||($new_rating2 == 2.00)) && ($new_rating2 <= 2.49)){ echo "<img src='images/2.png'>"; } if((($new_rating2 >= 2.50)||($new_rating2 == 2.50)) && ($new_rating2 <= 2.99)){ echo "<img src='images/25.png'>"; } if((($new_rating2 >= 3.00)||($new_rating2 == 3.00)) && ($new_rating2 <= 3.49)){ echo "<img src='images/3.png'>"; } if((($new_rating2 >= 3.50)||($new_rating2 == 3.50)) && ($new_rating2 <= 3.99)){ echo "<img src='images/35.png'>"; } if((($new_rating2 >= 4.00)||($new_rating2 == 4.00)) && ($new_rating2 <= 4.49)){ echo "<img src='images/4.png'>"; } if((($new_rating2 >= 4.50)||($new_rating2 == 4.50)) && ($new_rating2 <= 4.99)){ echo "<img src='images/45.png'>"; } if($new_rating2 == 5.0){ echo "<img src='images/5.png''>"; } </td>";[/code]How can i rewrite this so that it works in the 'print' part?Thanks, you guys have been great in the past too :)Peter. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 30, 2006 Share Posted September 30, 2006 Don't you get error messages with the code you had? If you do, they're normally pretty self-explanatory. Nonetheless, try this:[code]print "<td>";// edited line$getcount = mysql_query("SELECT * FROM site where id='$_GET[id];'"); while ($result=mysql_fetch_array($getcount)){ $rating = $result['rating']; } $new_rating2 = $rating;//gets the average rating from the database and puts it in a variable. if((($new_rating2 >= 0)||($new_rating2 == 0)) && ($new_rating2 <= 0.50)){ echo "<img src='images/0.png'>"; } if((($new_rating2 >= 0.50)||($new_rating2 == 0.50)) && ($new_rating2 <= .99)){ echo "<img src='images/05.png'>"; } if((($new_rating2 >= 1.00)||($new_rating2 == 1.50)) && ($new_rating2 <= 1.49)){ echo "<img src='images/1.png'>"; } if((($new_rating2 >= 1.50)||($new_rating2 == 1.50)) && ($new_rating2 <= 1.99)){ echo "<img src='images/15.png'>"; } if((($new_rating2 >= 2.00)||($new_rating2 == 2.00)) && ($new_rating2 <= 2.49)){ echo "<img src='images/2.png'>"; } if((($new_rating2 >= 2.50)||($new_rating2 == 2.50)) && ($new_rating2 <= 2.99)){ echo "<img src='images/25.png'>"; } if((($new_rating2 >= 3.00)||($new_rating2 == 3.00)) && ($new_rating2 <= 3.49)){ echo "<img src='images/3.png'>"; } if((($new_rating2 >= 3.50)||($new_rating2 == 3.50)) && ($new_rating2 <= 3.99)){ echo "<img src='images/35.png'>"; } if((($new_rating2 >= 4.00)||($new_rating2 == 4.00)) && ($new_rating2 <= 4.49)){ echo "<img src='images/4.png'>"; } if((($new_rating2 >= 4.50)||($new_rating2 == 4.50)) && ($new_rating2 <= 4.99)){ echo "<img src='images/45.png'>"; } if($new_rating2 == 5.0){ echo "<img src='images/5.png'>";// edited } echo "</td>";// edited line[/code] Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Thanks AndyB. Hmm, we don't get any errors now, but all the rating stars are at 0, although we have two records out of the 10 test ones where the rating should be 4 and 1 but they are showing the 0 rating stars.Heres the whole code, if anyone could help that would be great: :)[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><?php include("includes/dbconnect.php"); ?><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>View Sites</title></head><body><?php$result = mysql_query('SELECT * FROM site ORDER BY name DESC');$qry = mysql_fetch_array($result);print "<table border=1 class=list>";echo("<thead><tr><td>Site Name</td><td>Editor Rating</td><td>Visitor Rating</td><td>Date Added</td><td>Publisher</td></tr></thead><tbody>");while ($qry = mysql_fetch_array($result)){print "<tr>";print "<td><a href=site.php?id=$qry[id]>$qry[name]</td>";switch ($qry['editorrating']) { case 0: $image = "0.png"; break; case 1: $image = "1.png"; break; case 2: $image = "2.png"; break; case 3: $image = "3.png"; break; case 4: $image = "4.png"; break; case 5: $image = "5.png"; break; case 12: $image = "05.png"; break; case 15: $image = "15.png"; break; case 25: $image = "25.png"; break; case 35: $image = "35.png"; break; case 45: $image = "45.png"; break;}echo "<td><img src=\"images/{$image}\" /></td>";print "<td>";// edited line$getcount = mysql_query("SELECT * FROM site where id='$_GET[id];'"); while ($getrating=mysql_fetch_array($getcount)) { $rating = $getrating['rating']; } $new_rating2 = $rating;//gets the average rating from the database and puts it in a variable. if((($new_rating2 >= 0)||($new_rating2 == 0)) && ($new_rating2 <= 0.50)){ echo "<img src='images/0.png'>"; } if((($new_rating2 >= 0.50)||($new_rating2 == 0.50)) && ($new_rating2 <= .99)){ echo "<img src='images/05.png'>"; } if((($new_rating2 >= 1.00)||($new_rating2 == 1.50)) && ($new_rating2 <= 1.49)){ echo "<img src='images/1.png'>"; } if((($new_rating2 >= 1.50)||($new_rating2 == 1.50)) && ($new_rating2 <= 1.99)){ echo "<img src='images/15.png'>"; } if((($new_rating2 >= 2.00)||($new_rating2 == 2.00)) && ($new_rating2 <= 2.49)){ echo "<img src='images/2.png'>"; } if((($new_rating2 >= 2.50)||($new_rating2 == 2.50)) && ($new_rating2 <= 2.99)){ echo "<img src='images/25.png'>"; } if((($new_rating2 >= 3.00)||($new_rating2 == 3.00)) && ($new_rating2 <= 3.49)){ echo "<img src='images/3.png'>"; } if((($new_rating2 >= 3.50)||($new_rating2 == 3.50)) && ($new_rating2 <= 3.99)){ echo "<img src='images/35.png'>"; } if((($new_rating2 >= 4.00)||($new_rating2 == 4.00)) && ($new_rating2 <= 4.49)){ echo "<img src='images/4.png'>"; } if((($new_rating2 >= 4.50)||($new_rating2 == 4.50)) && ($new_rating2 <= 4.99)){ echo "<img src='images/45.png'>"; } if($new_rating2 == 5.0){ echo "<img src='images/5.png'>";// edited } echo "</td>";// edited lineprint "<td>$qry[dateadded]</td>";print "<td>$qry[publisher]</td>";print "</tr>";}print "</tbody></table>";?><p> </p><p> </p></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Anyone? Quote Link to comment Share on other sites More sharing options...
Orio Posted September 30, 2006 Share Posted September 30, 2006 if(([b]($new_rating2 [color=red]>=[/color] 0)[/b]||($new_rating2 == 0)) && ($new_rating2 <= 0.50)){$new_rating is always greater or equal to zero ;)Orio. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 But only if it's less that the next if statement isn't it? It works in my another page we have.Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
Orio Posted September 30, 2006 Share Posted September 30, 2006 Oh you are right....I am guessing the pics are showing the amount of stars they should, right?Orio. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 They are all showing 0 stars for some reason. We've made it so at least 2 of them should show stars.Peter. Quote Link to comment Share on other sites More sharing options...
Orio Posted September 30, 2006 Share Posted September 30, 2006 Try echoing the new_rating2 variable. It'll be eaiser after we know it's value so we will know on what to focus.Orio. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 Man, the coding is a mess.Instead of the image name being 45.png for a rating of 4.5 or 35.png being 3.5 why not use 4_5.png or 3_5.png? Much easier to see than being confused with a rating of 45! :oThe 3rd line below your body tag, why do you have "$qry = mysql_fetch_array($result);" out of nowhere? It's going to get executed again.That one long switch() statement can be condensed to 1 line assuming each rating corresponds to an image name like a rating of 4 = 4.pngThat "//gets the average rating from the database and puts it in a variable." Well, unlike the editorrating where you store it into an $image variable to output, this one you just output directly. How about storing it into an $image2 variable to do the same?A much improved verison.[code=php:0]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><?php //include("includes/dbconnect.php"); ?><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>View Sites</title></head><body><?php//$qry = mysql_fetch_array($result);print "<table border=1 class=list>";echo("<thead><tr><td>Site Name</td><td>Editor Rating</td><td>Visitor Rating</td><td>Date Added</td><td>Publisher</td></tr></thead><tbody>");$result = mysql_query('SELECT * FROM site ORDER BY name DESC');while ($qry = mysql_fetch_array($result)){print "<tr>";print "<td><a href=site.php?id=$qry[id]>$qry[name]</td>";//Assuming each editorrating correpsonding an image name w/ that rating$image = $qry['editorrating'] . '.png';//Display user's rating imageecho "<td><img src=\"images/{$image}\" /></td>";//Get User's ID rating and display rating image based on that rating$qry_rating = mysql_query("SELECT rating FROM site where id='$_GET[id]' LIMIT 1"); list($rating) = mysql_fetch_row($qry_rating);$new_rating2 = $rating; //gets the average rating from the database and put into an image variable$rateA = explode(".", $new_rating2);$rateA[1] = ((int)$rateA[1] <= 49) ? NULL : $rateA[1];$rateA[1] = ((int)$rateA[1] >= 50 && (int)$rateA[1] <= 99) ? "5" : $rateA[1];//store rating in image2 variable$image2 = $rateA[0] . $rateA[1] . '.png'; //Display user's rating image2echo "<td><img src=\"images/{$image2}\" /></td>";//Display dateaddedprint "<td>$qry[dateadded]</td>";//Display publisherprint "<td>$qry[publisher]</td>";print "</tr>";} print "</tbody></table>";?><p> </p><p> </p></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Wow, thanks.Although i am getting this error:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\public_html\RPG\list.php on line 17Line 17 being:while ($qry = mysql_fetch_array($result))Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 I commented on some line. Uncomment them.<?php //include("includes/dbconnect.php"); ?>Should be <?php include("includes/dbconnect.php"); ?>And the line below was:("<thead><tr><td>Site Name</td><td>Editor Rating</td><td>Visitor Rating</td><td>Date Added</td><td>Publisher</td></tr></thead><tbody>");//$result = mysql_query('SELECT * FROM site ORDER BY name DESC');Uncomment that $result = mysql_query('SELECT * FROM site ORDER BY name DESC'); Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Great. Although there are no images for the 'Visitor Rating'.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 Are you talking about this portion?[code=php:0]print "<tr>";print "<td><a href=site.php?id=$qry[id]>$qry[name]</td>";//Assuming each editorrating correpsonding an image name w/ that rating$image = $qry['editorrating'] . '.png';//Display user's rating imageecho "<td><img src=\"images/{$image}\" /></td>";[/code]Try echoing $image to show display if there's something or not. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 I'm talking about the visitor rating://Get User's ID rating and display rating image based on that rating$qry_rating = mysql_query("SELECT rating FROM site where id='$_GET[id]' LIMIT 1"); list($rating) = mysql_fetch_row($qry_rating);$new_rating2 = $rating; //gets the average rating from the database and put into an image variable$rateA = explode(".", $new_rating2);$rateA[1] = ((int)$rateA[1] <= 49) ? NULL : $rateA[1];$rateA[1] = ((int)$rateA[1] >= 50 && (int)$rateA[1] <= 99) ? "5" : $rateA[1];//store rating in image2 variable$image2 = $rateA[0] . $rateA[1] . '.png'; //Display user's rating image2echo "<td><img src=\"images/{$image2}\" /></td>";Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 Hmmm...all ID are unique right, with each ID containing only 1 rating? Like ID 23 has a rating of 2.57 once?Try this...//Get User's ID rating and display rating image based on that rating$id = $_GET['id'];$qry_rating = mysql_query("SELECT rating FROM site where id={$id}"); list($rating) = mysql_fetch_row($qry_rating);$new_rating2 = $rating;echo $new_rating2 and tell me if it's display something. If it does, the rest should work. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 I just had a look in IE instead of firefox and i can now see that it has broken images, all 'http://localhost/rpg/images/.png'.Yes, each record has a different ID and all have 1 rating.We tried doing echo $new_rating; but it didn't show anything.Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 K, it seems the query is not getting anything for the $rating variable.There's a `rating` column in your `site` table right?Would you mind doing a test script to see if the below code works?$id can be set any ID that's in your site table (I assume there's an ID 17).$id = $_GET['id'];//test$id = 17;$qry_rating = mysql_query("SELECT rating FROM site where id='$id'"); list($rating) = mysql_fetch_row($qry_rating);$new_rating2 = $rating;echo $new_rating2; Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Just shows a '0' :)Thanks for your help so far.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 But you have a rating column in your table right?Let's do a simple test script:[code]$qry_rating = mysql_query("SELECT rating FROM site"); while(list($rating) = mysql_fetch_row($qry_rating)){echo $rating . "<br/ >";}[/code]Try that. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Yup, got the rating column in the site table :)The test gives:4 0 0 0 0 0 0 0 1.25 0 0Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 I see the problem now.Below$new_rating2 = $rating;in the code I wrote:put$new_rating2 = (string)$rating; Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 :( still the same problemThanks.Peter. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 30, 2006 Share Posted September 30, 2006 What's the ID of someone with a rating of 1.25?Add this to the test script:[code=php:0]$id = $_GET['id'];$id = (ID that person w/ a rating of 1.25);$qry_rating = mysql_query("SELECT rating FROM site where id='$id'"); list($rating) = mysql_fetch_row($qry_rating);$new_rating2 = (string)$rating;print $new_rating2; //gets the average rating from the database and put into an image variable$rateA = explode(".", $new_rating2);$rateA[1] = ((int)$rateA[1] <= 49) ? NULL : $rateA[1];$rateA[1] = ((int)$rateA[1] >= 50 && (int)$rateA[1] <= 99) ? "5" : $rateA[1];//store rating in image2 variableprint $image2 = $rateA[0] . $rateA[1] . '.png'; [/code]And tell me if the rating an image displays. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 30, 2006 Author Share Posted September 30, 2006 Outputs:1.251.png(the ID is 10 :))Thanks.Peter. 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.