0

This is my count query:

  $query="SELECT count(rating) as number_of_reviews FROM product_reviews WHERE product_id='$pid' AND status='approved'";
  list($number_of_reviews)=mysql_fetch_array(mysql_query($query));

I also check to see if the surfer already has a review for this item...

  $query="SELECT id FROM product_reviews WHERE product_id='$pid' AND userid='$UID' LIMIT 1";
  list($SurferReviewed)=mysql_fetch_array(mysql_query($query));

Based on these 2 results, I want to display something specific...

if ($SurferReviewed=='')
{
  $WriteReview="Write a review";
}
elseif ($SurferReviewed=='' && $number_of_reviews=='0')
{
  $Write_a_Review="Be the first to write a review";
}
else
{
  $Write_a_Review="You've already reviewed the item";
}

So basically I want it to check if user has already reviewed, and if so, display "You've already reviewed".

If they have not reviewed and there are 0 reviews, write "Be the first...".

If user has not reviewed but he would not be the first, write "Write a review".

Questions is: "Be the first to write a review" never shows up. Even if the condition exists.

Mike
  • 105
  • 12
  • 2
    What's the question? In what way is this not working? Is there an error? – David Jan 17 '14 at 14:23
  • 4
    You're never going to hit your second condition. Switch the order of the first and second. – Patrick Q Jan 17 '14 at 14:23
  • 4
    [I love the smell of a SQL injection attack in the morning...](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Alnitak Jan 17 '14 at 14:24
  • @david, edited with actual questions. Sorry. – Mike Jan 17 '14 at 14:25
  • The mysql_* functions are deprecated. It isn't a good practice to use them. http://php.net/manual/en/intro.mysql.php – Michas Jan 17 '14 at 14:26
  • Use **PDO** with prepared statements instead of old, unhealthy and deprecated connectors to your database. Also use [PHP sanitize filrs](http://www.php.net/manual/en/filter.filters.sanitize.php) previous to touching your database. If not, expect a **SQL INJECTION**. – digitai Jan 17 '14 at 14:30
  • Thank you all for the suggestions. I will be sure to read up on all of this before I release the script. I am a beginner and this is all new to me. – Mike Jan 17 '14 at 14:46

3 Answers3

1
if ($SurferReviewed=='' && $number_of_reviews=='0')
{
  $Write_a_Review="Be the first to write a review";
}
elseif ($SurferReviewed=='')
{
  $Write_a_Review="Write a review";
}

else
{
  $Write_a_Review="You've already reviewed the item";
}
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
  • Thanks Patrick. This is still showing "Be the first..." even though $number_of_reviews = 4. Maybe I should add && $number_of_reviews!='0' to the second IF ? – Mike Jan 17 '14 at 14:34
  • Perfect. Thank you. I really appreciate your time, and everyone else that chimed in. Thanks a lot! – Mike Jan 17 '14 at 14:45
0

The order of your if-clauses is not correct. When the surfer has not written a review the first if-clause is used. So you've to change it like this:

if ($number_of_reviews == 0) {
  $Write_a_Review="Be the first to write a review";
} elseif ( $SurferReviewed=='' ) {
  $WriteReview="Write a review";
} else {
  $Write_a_Review="You've already reviewed the item";
}
0

Your first condition precludes the second, because if the first is true the second is never evaluated.

Of course if the number of reviews is zero then the given user cannot have submitted a review either.

In these circumstances I believe it would be better to nest the second set of tests. You therefore end up with two mutually exclusive possibilities in each test, instead of a three-way if/else if/else tree:

if ($number_of_reviews == '0') {
    $Write_a_Review = "Be the first to write a review";
} else {
    if ($SurverReviewed == '') {
        $Write_a_Review = "Write a review";
    } else {
        $Write_a_Review="You've already reviewed the item";
    }
}

NB: please use === 0 instead of == '0'

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Could you please explain why exactly it is "better" to have the nested conditions? I'm not arguing that it is or isn't, I'm just curious as to your reasoning for doing it this was as opposed to simply putting the if/elseif/else in the correct order. – Patrick Q Jan 17 '14 at 14:32
  • @PatrickQ well, I did presume that the OP's test for `= ''` in the second test was actually required. If it's not then the nesting makes no difference. – Alnitak Jan 17 '14 at 14:34