Jump to content


Photo

Print code problem PHP


  • Please log in to reply
40 replies to this topic

#1 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 10:14 AM

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:
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>";

How can i rewrite this so that it works in the 'print' part?

Thanks, you guys have been great in the past too :)

Peter.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 September 2006 - 10:22 AM

Don't you get error messages with the code you had? If you do, they're normally pretty self-explanatory.  Nonetheless, try this:

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

Legend has it that reading the manual never killed anyone.
My site

#3 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 10:34 AM

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

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


#4 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 05:38 PM

Anyone?

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 September 2006 - 06:00 PM

if((($new_rating2 >= 0)||($new_rating2 == 0)) && ($new_rating2 <= 0.50)){

$new_rating is always greater or equal to zero ;)

Orio.
Think you're smarty?

(Gone until 20 to November)

#6 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 06:18 PM

But only if it's less that the next if statement isn't it? It works in my another page we have.

Thanks.

Peter.

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 September 2006 - 06:23 PM

Oh you are right....

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

Orio.
Think you're smarty?

(Gone until 20 to November)

#8 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 06:26 PM

They are all showing 0 stars for some reason. We've made it so at least 2 of them should show stars.

Peter.

#9 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 September 2006 - 06:36 PM

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.
Think you're smarty?

(Gone until 20 to November)

#10 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 September 2006 - 06:38 PM

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.
<!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>






#11 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 06:45 PM

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.

#12 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 September 2006 - 06:48 PM

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');


#13 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 06:52 PM

Great. Although there are no images for the 'Visitor Rating'.

Peter.

#14 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 September 2006 - 06:56 PM

Are you talking about this portion?

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>";


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


#15 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 07:05 PM

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.

#16 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 September 2006 - 07:13 PM

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.

#17 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 07:18 PM

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.

#18 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 September 2006 - 07:25 PM

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;



#19 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 30 September 2006 - 07:29 PM

Just shows a '0' :)

Thanks for your help so far.

Peter.

#20 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 September 2006 - 07:32 PM

But you have a rating column in your table right?

Let's do a simple test script:

$qry_rating = mysql_query("SELECT rating FROM site");   
        
while(list($rating) = mysql_fetch_row($qry_rating))
{
echo $rating . "<br/ >";
}

Try that.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users