Jump to content

Archived

This topic is now archived and is closed to further replies.

wwfc_barmy_army

Print code problem PHP

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 line
print "<td>$qry[dateadded]</td>";
print "<td>$qry[publisher]</td>";
print "</tr>";
}

print "</tbody></table>";
?>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Oh you are right....

I am guessing the pics are showing the amount of stars they should, right?

Orio.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!  :o

The 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.png

That "//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 image
echo "<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 image2
echo "<td><img src=\"images/{$image2}\" /></td>";

//Display dateadded
print "<td>$qry[dateadded]</td>";
//Display publisher
print "<td>$qry[publisher]</td>";
print "</tr>";
}

print "</tbody></table>";
?>

<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
[/code]




Share this post


Link to post
Share on other sites
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 17
Line 17 being:
while ($qry = mysql_fetch_array($result))

Thanks.

Peter.

Share this post


Link to post
Share on other sites
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');

Share this post


Link to post
Share on other sites
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 image
echo "<td><img src=\"images/{$image}\" /></td>";
[/code]


Try echoing $image to show display if there's something or not.

Share this post


Link to post
Share on other sites
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 image2
echo "<td><img src=\"images/{$image2}\" /></td>";


Thanks.

Peter.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 variable
print $image2 = $rateA[0] . $rateA[1] . '.png';
[/code]

And tell me if the rating an image displays.
   

Share this post


Link to post
Share on other sites

×

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.