将10个查询合并到一个PHP中?

时间:2013-05-12 10:29:45

标签: php mysql

我是一个完整的PHP新手(就像我在两周前写的第一行一样,所以我可能看不到我面前的情况)。

我正在为我的一个项目做条形图。我有一个表(show_score),得分为10(列:得分)和show_id(列show_id)。

我需要show_id的分数:

$conn = new PDO('mysql:host=localhost;dbname=mytvbox', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
$stmt = $conn->prepare('SELECT * FROM show_score WHERE show_id = 40');
$stmt->execute(array('show_id' => $show_id));


$count = $stmt->rowCount();
echo $count. "number of scores<br>"; 

然后我需要每个数字的分数和它在分数总数上的百分比(这里是8)

$stmt = $conn->prepare('SELECT * FROM show_score WHERE show_id = 40 AND score = 8');
$stmt->execute(array('show_id' => $show_id));


$countscore_8 = $stmt->rowCount();
echo $countscore_8 . "number of 8<br>";   //nombre total de 8 sur la serie 


if ($countscore_8 == 0)  //if no score then no pixels for you
{ echo "0px"; }
else {


$division = $countscore_8 / $count; //finding ration
$percentage = $division * 100;  //finding percentage of score 8
$pixels = 180;   //base pixels
$finalpixels = ($percentage / 100) * $pixels; 
echo $finalpixels . "px"; //number of pixels in the bar
}

我的问题是我必须查询每个分数才能找到图表中条形图的像素数。有没有办法在单个查询中输出每个分数的百分比?

为了使它更清楚,让我们假设我的表有这个:

show_id = 40, score = 5
show_id = 40, score = 5
show_id = 40, score = 10
show_id = 40, score = 10

我需要

score 1:  0% / 0px
score 2:  0% / 0px
score 3:  0% / 0px
score 4:  0% / 0px
score 5: 50% / 90px
score 6:  0% / 0px
score 7:  0% / 0px
score 8:  0% / 0px
score 9:  0% / 0px
score 10:50% / 90px

希望这有点道理。

0 个答案:

没有答案
相关问题