Jump to content

Print code problem PHP


wwfc_barmy_army

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.
Link to comment
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]
Link to comment
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]
Link to comment
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]




Link to comment
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');
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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;

Link to comment
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.
   
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.