Jump to content

count question?


pouncer

Recommended Posts

I got a table called item_rating,

 

fields:

 

item_id | user_who_rated | rating

 

e.g data:

 

7GD | John | 5

7GD | Suzie | 4

7GD | Tim | 5

 

Ratings go from 1-5..

 

how do i get a total number of people who rated 1, who rated 2.. etc etcc

in the above case for item 7GD it should be like this

 

rating 1: 0 people

rating 2: 0 people

rating 3: 0 people

rating 4: 1 people

rating 5: 2 people

 

SELECT * FROM item_rating WHERE item_id='7GD'

..what next guys?

Link to comment
Share on other sites

 

$res1=SELECT * FROM item_rating WHERE item_id='7GD'

while($row1=mysql_fetch_array($res1))

{

  for($i=1;$i<=5;$i++)

  {

    $res2=select * from item_rating where rating='$i' and item_id='$row1['item_id']

    while($row2=mysql_fetch_array($res2))

    {

          $num=count($row2);

          }

}

}

 

kinda indirect but wud work!!!

 

Link to comment
Share on other sites

For Example

 

$sql ="select count(*) AS tot_peple_per_rating,rating FROM item_rating GROUP BY rating";
$result = mysql_query($sql);

while(list($tot_rating,$rating) = mysql_fetch_row($result)){
         echo "For Rating ".$rating ." - ".$tot_rating." Peoples";
         echo " <br> ";

}

Link to comment
Share on other sites

thanks alot jitesh, i got this

 

$item_id = $_GET['item_id'];

$sql ="select count(*) AS tot_peple_per_rating,rating FROM item_rating WHERE item_id='$item_id' GROUP BY rating";
$result = mysql_query($sql);

while(list($tot_rating,$rating) = mysql_fetch_row($result)){
         echo "For Rating ".$rating ." - ".$tot_rating." Peoples";
         echo " <br> ";
}

 

and it echos

 

For Rating 5 - 2 Peoples

 

is there a way to also make echo like

 

For Rating 4 - 0 Peoples

..

..

For Rating 1 - 0 Peoples

Link to comment
Share on other sites

$item_id = $_GET['item_id'];

$sql ="select count(*) AS tot_peple_per_rating,rating FROM item_rating WHERE item_id='$item_id' GROUP BY rating";
$result = mysql_query($sql);
             $list_all = array(1,2,3,4,5);
              $list_sel = array();
while(list($tot_rating,$rating) = mysql_fetch_row($result)){
             echo "For Rating ".$rating ." - ".$tot_rating." Peoples";
             echo " <br> ";
             $list_sel[] = $rating;
}
            $arr_diff = array_diff($list_all,$list_sel);
           for($i=0;$i<count($arr_diff);$i++){
              echo "For Rating ".$arr_diff[$i] ." - 0 Peoples";
              echo " <br> ";
        }


Link to comment
Share on other sites

thanks jitesh. almost there bit a slight error

 

For Rating 1 - 1 Peoples

For Rating 5 - 2 Peoples

For Rating - 0 Peoples

For Rating 2 - 0 Peoples

For Rating 3 - 0 Peoples

 

 

not sure why it does this:

For Rating - 0 Peoples

 

:s, should be '4'?

Link to comment
Share on other sites

I do not know exactly why the troubles is.

By the way debug like this

 

$arr_diff = array_diff($list_all,$list_sel);

 

// check,Is this displaying a arrays of ratings which has zero peoples

echo "<pre>";
print_r($arr_diff);

           for($i=0;$i<count($arr_diff);$i++){
              echo "For Rating ".$arr_diff[$i] ." - 0 Peoples";
              echo " <br> ";
        }

Link to comment
Share on other sites

require_once("../php_classes/class_login.php");
$item_id = $_GET['item_id'];

echo "<pre>";
print_r($arr_diff);

$sql ="select count(*) AS tot_peple_per_rating,rating FROM item_rating WHERE item_id='$item_id' GROUP BY rating";
$result = mysql_query($sql);

    $list_all = array(1,2,3,4,5);
    $list_sel = array();

while(list($tot_rating,$rating) = mysql_fetch_row($result)){
   		//echo "For Rating ".$rating ." - ".$tot_rating." Peoples";
        //echo " <br> ";
        //$list_sel[] = $rating;
}
            
$arr_diff = array_diff($list_all,$list_sel);
    for($i=0;$i<count($arr_diff);$i++){
	   echo "For Rating ".$arr_diff[$i] ." - 0 Peoples";
       echo " <br> ";
    }

 

that way, it echos

For Rating 1 - 0 Peoples 

For Rating 2 - 0 Peoples 

For Rating 3 - 0 Peoples 

For Rating 4 - 0 Peoples 

For Rating 5 - 0 Peoples

 

when i uncomment the lines it echos

 

For Rating 1 - 1 Peoples

For Rating 5 - 2 Peoples 

For Rating  - 0 Peoples 

For Rating 2 - 0 Peoples

For Rating 3 - 0 Peoples

Link to comment
Share on other sites

ok i tried this

 

$item_id = $_GET['item_id'];

$sql ="select count(*) AS tot_peple_per_rating,rating FROM item_rating WHERE item_id='$item_id' GROUP BY rating";
$result = mysql_query($sql);

    $list_all = array(1,2,3,4,5);
    $list_sel = array();

while(list($tot_rating,$rating) = mysql_fetch_row($result)){
   		echo "For Rating ".$rating ." - ".$tot_rating." Peoples";
        echo " <br> ";
        $list_sel[] = $rating;
}

    

$arr_diff = array_diff($list_all,$list_sel);

echo "<pre>";
print_r($arr_diff);

    for($i=0;$i<count($arr_diff);$i++){
	   echo "For Rating ".$arr_diff[$i] ." - 0 Peoples";
       echo " <br> ";
    }

 

and it echos

 

For Rating 1 - 1 Peoples

For Rating 5 - 2 Peoples

 

Array

(

    [1] => 2

    [2] => 3

    [3] => 4

)

For Rating  - 0 Peoples 

For Rating 2 - 0 Peoples 

For Rating 3 - 0 Peoples

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.