--- In [email protected], James Keeline <[EMAIL PROTECTED]> wrote:
>
> --- whoisquilty <[EMAIL PROTECTED]> wrote:
> 
> > I'm trying to use a rating system on a site. This is the query and PHP
> > that I've got:
> > 
> > $ratingquery = "SELECT *, AVG(rating) as avgrating
> >                             FROM rate
> >                             WHERE videono = $vidvar
> >                             GROUP BY videono";
> > $ratingresult = mysql_query($ratingquery) or die(mysql_error());
> > 
> > while ($rating = mysql_fetch_array($ratingresult))
> > {
> > if($rating["avgrating"]>1)
> > {
> > print '<img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14" />';
> > }
> > if($rating["avgrating"]<1)
> > {
> > print '<img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14" />';
> > }
> > if($rating["avgrating"]>1&$rating["avgrating"]<2)
> > {
> > print '<img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14" />';
> > }
> > if($rating["avgrating"]>2&$rating["avgrating"]<3)
> > {
> > print '<img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14" />';
> > }
> > if($rating["avgrating"]>3&$rating["avgrating"]<4)
> > {
> > print '<img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_off.gif" alt="1 Star" width="15" height="14" />';
> > }
> > if($rating["avgrating"]>4&$rating["avgrating"]<5)
> > {
> > print '<img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14"
> > /><img src="img/star_on.gif" alt="1 Star" width="15" height="14" />';
> > }
> > }
> > 
> > 
> > 
> > 
> > The problem is that it doesn't show anything where the 'print' occurs.
> > It shows nothing. But, when I remove the if statements, I get a
> > number. So, the query seems to be working. What am I doing wrong with
> > the <>?
> 
> I can think of a few areas which could cause problems and some
suggestions to
> clean up your code.
> 
> In your query, I would use single quotes around your variable value.
 This way,
> if the value is not set, it becomes a zero rather than breaking your
query and
> displaying ugly SQL error messages to your end users.
> 
> You may find that mysql_fetch_assoc() works better than
mysql_fetch_array()
> though it should work.  The _assoc() function keys the array by the
field names
> where _array() will get the results twice by default:  numeric and
field names.
> 
> Have you printed out the value numerically to see what you get? 
Update: 
> looking back I see that you indicate that you can print the value if
the if()
> statements are gone.
> 
> In your cascading if() statement you have a single ampersand (&)
between the
> two conditionals.  This won't work and is likely your problem.  In
PHP a single
> ampersand before a variable name is used to refer to a variable by
reference. 
> If you must use the ampersand, you must use a double ampersand (&&)
for "and"
> (which also works and is easier to read).
> 
> You also have some gaps if the average is equal to an integer value
such as 4
> becaues you are looking for a range <4 and another one >4 but not
one exactly
> equal to 4.  The single ampersand will kill it first, however.
> 
> It seems as if your star display could be made much simpler with
something like
> this:
> 
> $ratingquery  = "SELECT *, AVG(rating) as avgrating
>                FROM rate
>                WHERE videono = '$vidvar'
>                GROUP BY videono";
> $ratingresult = mysql_query($ratingquery) or die(mysql_error());
>  
> while ($rating = mysql_fetch_assoc($ratingresult))
> {
>  $num = floor($rating['avgrating']);
>  print show_star($num);
> }
> 
> function show_star($rating)
> {
>  $on   = "<img src='img/star_on.gif'  width='15' height='14' />";
>  $off  = "<img src='img/star_off.gif' width='15' height='14' />";
>  $show = str_repeat($on, $rating) . str_repeat($off, 5-$rating);
> }
> 
> James
>


I'll need to convert this to a form on the next page in the site. So,
I'd like to keep the if statements.

I took your advice and used mysql_fetch_assoc and &&. But, now it's
displaying the first if statement for if($rating["avgrating"]>1). Why
would this happen?

Jeremy

Reply via email to