I need help counting the number of stars

Discussion in 'PHP' started by co.ador, Jul 26, 2009.

  1. #1
     <?php
    $query ="SELECT rating , COUNT(*) AS rating_counts FROM rating
     WHERE item_name = 'Jordan' GROUP BY rating";
    
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    
    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) { 
    $ratings = array_fill(1,5,0);
    $rowsCount = count($result); // assume $rows is the result array
    
    for($i=0;$i<$rowsCount;$i++) {
        $ratings[$result[$i]['rating']]++;
    
    
    echo '<h3>1 Star Active</h3>
    <ul class="rating onestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[',$result[2],']</li>
    </ul>
    <h3>2 Stars Active</h3>
    <ul class="rating twostar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[40]</li>
    </ul>
    <h3>3 Stars Active</h3>
    <ul class="rating threestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[60]</li>
    </ul>
    <h3>4 Stars Active</h3>
    <ul class="rating fourstar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[80]</li>
    </ul>
    <h3>5 Stars Active</h3>
    <ul class="rating fivestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[100]</li>
    </ul>';
    }
    } 
    ?>
    PHP:
    Hey guys I want to give a little hand here I have an sql injection at the top and a for loop to print the total count of the 1 stars, 2 stars , 3 stars , 4 stars, and five stars of item_name= x but it is not working i think the $rows variable in the for loop is coming empty and the item_name= x in the sql injection need some type of value but i don't have any idea how to direct the rows and the item_name so it can properly work... Just give some knowledge been working in a little bit of what the guys has given me.

    I think the item_name need to be placed auto variable not manually one so every time an x user click on a item the sql injection bring the variable value in the item_name value and then the for loop get in charge of counting and printing the value of star 1, stars 2, etc of that item...

    drop some of your understanding of this thanks
     
    co.ador, Jul 26, 2009 IP
  2. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Well honestly, I don't understand exactly how your rating table in the database's like.

    I assume it as below:
    . item_name: varchar, store item name.
    . rating: tiny int, store "star", ie: 1, 2, 3, 4, 5.
    . id: primary, int.

    And you want to count the number of 1 star (2 stars, 3 stars,...) for Jordan item? If its the case, php code should be like
    
    $sql="SELECT rating, COUNT(rating) FROM rating WHERE (item_name='Jordan') GROUP BY rating";
    $result=mysql_query($sql);
    
    if (mysql_num_rows($result)  > 0) {
    	while ($row= mysql_fetch_assoc($result)) {
    		$star= $row[rating];
    		switch($star) {
    			case '1':
    				$num_1_star= $row['COUNT(rating)'];
    				break 1;
    			case '2':
    				$num_2_star= $row['COUNT(rating)'];
    				break 1;
    			case '3':
    				$num_3_star= $row['COUNT(rating)'];
    				break 1;
    			case '4':
    				$num_4_star= $row['COUNT(rating)'];
    				break 1;
    			case '5':
    				$num_5_star= $row['COUNT(rating)'];
    				break 1;
    		}
    	}
    }
    
    PHP:
    The number of 1 star rating will be stored at $num_1_star, 2 star rating will be stored at $num_2_star, etc. You can echo them out in html code.

    Hope that's the answer you're seeking.
     
    zandigo, Jul 26, 2009 IP
  3. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I will test it and try it and implement it into the code

    to be exact the database dump is



    you were exactly right the only thing missint was ip_address which it will rate only if ip address has rate that item
     
    co.ador, Jul 26, 2009 IP
  4. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Zandigo I implemented to the file but now i am getting this error

    Notice: Undefined variable: num_1_star in C:\wamp\www\shoes\stores\itemdetails2.php on line 484

    <?php 
    $sql="SELECT rating, COUNT(rating) FROM rating WHERE (item_name='$platename') GROUP BY rating";
    $result=mysql_query($sql);
    if (mysql_num_rows($result)  > 0) {
        while ($row= mysql_fetch_assoc($result)) {
            $star= $row['rating'];
            switch($star) {
                case '1':
                    $num_1_star= $row['COUNT(rating)'];
                    break 1;
                case '2':
                    $num_2_star= $row['COUNT(rating)'];
                    break 1;
                case '3':
                    $num_3_star= $row['COUNT(rating)'];
                    break 1;
                case '4':
                    $num_4_star= $row['COUNT(rating)'];
                    break 1;
                case '5':
                    $num_5_star= $row['COUNT(rating)'];
                    break 1;
    				}
    
    
    echo '<h3>1 Star Active</h3>
    <ul class="rating onestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">['. $num_1_star['rating'].'</li>  // line 484
    </ul>
    <h3>2 Stars Active</h3>
    <ul class="rating twostar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">['. $row['rating'].'</li> // When I put it $row['rating'] then it prints the index number location which is 2, the rating field is in the index number 2
    </ul>
    <h3>3 Stars Active</h3>
    <ul class="rating threestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[60]</li>
    </ul>
    <h3>4 Stars Active</h3>
    <ul class="rating fourstar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[80]</li>
    </ul>
    <h3>5 Stars Active</h3>
    <ul class="rating fivestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[100]</li>
    </ul>';
    }
    } 
    ?>
    PHP:
    help please
     
    co.ador, Jul 26, 2009 IP
  5. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Well, alright, let's try the complete code. But I still don't understand what you want to echo out.

    
    $sql="SELECT rating, COUNT(rating) FROM rating WHERE (item_name='Jordan') GROUP BY rating";
    $result=mysql_query($sql);
    
    $num=mysql_num_rows($result);
    if ($num  > 0) {
        while ($row= mysql_fetch_assoc($result)) {
            $star= $row[rating];
            switch($star) {
                case '1':
                    $num_1_star= $row['COUNT(rating)'];
                    break 1;
                case '2':
                    $num_2_star= $row['COUNT(rating)'];
                    break 1;
                case '3':
                    $num_3_star= $row['COUNT(rating)'];
                    break 1;
                case '4':
                    $num_4_star= $row['COUNT(rating)'];
                    break 1;
                case '5':
                    $num_5_star= $row['COUNT(rating)'];
                    break 1;
            }
        }
    }
    echo "<h3>Rating statistic</h3>
    <ul class='rating'>
        <li class='one'><a href='#' title='1 Star'>Number of one-star rating is $num_1_star </a></li>
        <li class='two'><a href='#' title='2 Stars'>Number of two-star rating is $num_2_star </a></li>
        <li class='three'><a href='#' title='3 Stars'>Number of three-star rating is $num_3_star </a></li>
        <li class='four'><a href='#' title='4 Stars'>Number of four-star rating is $num_4_star </a></li>
        <li class='five'><a href='#' title='5 Stars'>Number of five-star rating is $num_5_star </a></li>
        <li class='total'>Total rating is $num </li> 
    </ul>";
    
    PHP:
    If it's still not what you want, just state what you need a little more clearly, I will try to help you out :).
     
    Last edited: Jul 27, 2009
    zandigo, Jul 27, 2009 IP
  6. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What i want to echo is the ul html code below the php script which will repeat 5 times. I want to say that solved the problem with this script try is a modification of your....


    but without the switch function....

     <?php 
    $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$platename') GROUP BY rating";
    $result=mysql_query($sql);
    $number = array( "one","two","three","four","five");
    $total = array_fill(1, 5, 0);
    if (mysql_num_rows($result)  >=0) {
        while ($row= mysql_fetch_assoc($result)) {
       $total[$row['rating']]= $row['total'];
       }
       foreach($number as $K =>$num)
       {
          echo '<h3>'.($K+1).' Star Active</h3>
          <ul class="rating '.$num.'star">
    	  <li class="one">1</li>
          <li class="two">2</li>
          <li class="three">3</li>
          <li class="four">4</li>
          <li class="five">5</li>
          <li class="total">['.$total[$K+1].']</li>
          </ul>';
       }
    } 
    ?>
    PHP:
    Thank you Zandigo
     
    co.ador, Jul 27, 2009 IP
  7. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Alright, that's fine. Any command is good enough as long as It gives what you need. You are welcomed :D.
     
    zandigo, Jul 27, 2009 IP