rating stars....having issues

Discussion in 'PHP' started by jaxterpromotions, Sep 27, 2009.

  1. #1
    Hey there,

    So I am having issues with this statement, I thought it was a good one but I am a novice at PHP programming. Could someone help me?

     	 if ($row['rating'] > 0) $rating = '<img src="images/1-star.jpg">';
         elseif ($row['rating'] < 2) $rating = '<img src="images/1-star.jpg">';
    
     	 if ($row['rating'] > 1) $rating = '<img src="images/2-star.jpg">';
         elseif ($row['rating'] < 3) $rating = '<img src="images/2-star.jpg">';
    
     	 if ($row['rating'] > 2) $rating = '<img src="images/3-star.jpg">';
         elseif ($row['rating'] < 4) $rating = '<img src="images/3-star.jpg">';
    
     	 if ($row['rating'] > 3) $rating = '<img src="images/4-star.jpg">';
         elseif ($row['rating'] < 5) $rating = '<img src="images/4-star.jpg">';
    
     	 if ($row['rating'] > 4) $rating = '<img src="images/5-star.jpg">';
         elseif ($row['rating'] < 6) $rating = '<img src="images/5-star.jpg">';
    
    PHP:

    What am I doing wrong? I want an image to show according to the number that was given. (1-5)
     
    jaxterpromotions, Sep 27, 2009 IP
  2. sunchiqua

    sunchiqua Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Enjoy :)

    switch ($row['rating']) {
    	case 1:
    		$rating = '<img src="images/1-star.jpg">';
    		break;
    	case 2:
    		$rating = '<img src="images/2-star.jpg">';
    		break;
    	case 3:
    		$rating = '<img src="images/3-star.jpg">';
    		break;
    	case 4:
    		$rating = '<img src="images/4-star.jpg">';
    		break;
    	case 5:
    		$rating = '<img src="images/5-star.jpg">';
    		break;
    	default:
    		$rating = '<img src="images/0-star.jpg">';
    }
    PHP:
     
    sunchiqua, Sep 27, 2009 IP
  3. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #3
    This works for me:

    Result "i equals 4"
    
    <?php
    
    $i = 4.6;
    
    switch ($i) {
        case ($i < 2  AND $i > 0):
            echo "i equals 1";
            break;
        case ($i < 3  AND $i > 1):
            echo "i equals 2";
            break;
        case ($i < 4  AND $i > 2):
            echo "i equals 3";
            break;
         case ($i < 5  AND $i > 3):
               echo "i equals 4";
               break;
         case ($i < 6  AND $i > 4):
            echo "i equals 5";
            break;
    
    
    
    }
    ?>
    
    
    
    PHP:
     
    dweebsonduty, Sep 27, 2009 IP
  4. jaxterpromotions

    jaxterpromotions Guest

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    dudddeeee. you are a savior. Thank you for saving me two hours of taking Excedrin and hoping I would find out what was wrong.
     
    jaxterpromotions, Sep 27, 2009 IP
  5. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #5
    Believe me, I have been there many times.
     
    dweebsonduty, Sep 27, 2009 IP