 |
|
|
 |
 |
|
Code (PHP)
01. <?
02. $conn =mysql_connect( "localhost" , "root" , "111" );
03. $dbname =mysql_select_db( "polls" );
04.
05.
06. $sql =mysql_query( "SELECT COUNT(polls_id) AS Total1 FROM polls WHERE polls_id LIKE'1'" );
07. while ( $rs =mysql_fetch_assoc( $sql )) {
08.
09.
10. $sum = $rs [ 'Total1' ];
11. $total = $rs [ 'Total1' ];
12.
13. $percent = number_format(( $sum *100)/ $total ,2);
14. echo $percent . '%' ;
15.
16. }
17. ?>
ไม่ได้ครับ
|
ประวัติการแก้ไข 2013-04-05 23:43:52
 |
 |
 |
 |
Date :
2013-04-05 23:43:33 |
By :
โต้ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอ่อ สมมติว่า $rs['Total1'] มีค่าเท่ากับ 250
เอา (250*100)/250 มันก็ได้ 100 นะครับ
ต้องมีค่าอะไรอีกค่ามาหารหรือเปล่าครับ ?
|
 |
 |
 |
 |
Date :
2013-04-06 00:35:41 |
By :
BanK_z |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สูตรที่ใช้มีให้แล้ว แต่คำถามคือ คุณอยากได้เปอร์เซ็นของอะไร
เช่น เปอร์เซ็นของคนที่ตอบ ใช่ เป็นเท่าไหร่ และที่ตอบไม่เท่าไหร่ และที่เฉยๆ เท่าไหร่
Code (PHP)
01.
02. $sql = "SELECT COUNT(polls_id) AS Total FROM polls WHERE polls_id LIKE '1' " ;
03.
04. สมมติว่าได้ 65 เรคอร์ด หรือคนตอบ 65 ครั้ง
05.
06.
07.
08.
09. $sql = "SELECT answer, COUNT(polls_id) AS TotalAnswer FROM polls WHERE polls_id LIKE '1' " ;
10. $sql .= " GROUP by answer " ;
11. >> QUERY
12. >> WHILE FETCH array
13.
14. ถ้าโพลนี้มีคำตอบแค่ 3 ตัวเลือก ในลูปนี้ก็จะมีแค่ 3 แถว
15. $arrAnswer [ $row [ 'answer' ]] = $row [ 'TotalAnswer' ];
16.
17. >> END WHILE
18.
19. เช่น
20. แถวที่ 1 answer "yes" = 30
21. แถวที่ 2 answer "no" = 20
22. แถวที่ 3 answer " ignore" = 15
23.
24.
25. ข้อมูลที่ได้ทั้งหมด จะได้ตัวแปร ดังนี้
26.
27. $total = 65;
28.
29. $arrAnswer [ 'yes' ] = 30;
30. $arrAnswer [ 'no' ] = 20;
31. $arrAnswer [ 'ignore' ] = 15;
1. หาเปอร์เซ็นคนตอบ "ใช่"
1. $percent = ( $arrAnswer [ 'yes' ] * 100) / $total ;
2. หาเปอร์เซ็นคนตอบ "ไม่"
1. $percent = ( $arrAnswer [ 'no' ] * 100) / $total ;
3. หาเปอร์เซ็นคนตอบ "เฉยๆ"
1. $percent = ( $arrAnswer [ 'ignore' ] * 100) / $total ;
|
ประวัติการแก้ไข 2013-04-06 09:19:56
 |
 |
 |
 |
Date :
2013-04-06 09:17:48 |
By :
Naizan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
/*mysql_select_db($database_moneydb, $moneydb);
$query_sum = "SELECT sum(payamount) as sumpay,paydate FROM dailypay WHERE 1 $paycond and createby = '1' GROUP BY paydate";
//echo $query_sum;
$sum = mysql_query($query_sum, $moneydb) or die(mysql_error());
while ($row_sum = mysql_fetch_assoc($sum)){
$datay[substr($row_sum['paydate'],8)-1] = $row_sum['sumpay'] ;
}
*/
$conn=mysql_connect("localhost","root","55");
$dbname=mysql_select_db("polls");
//LIKE 1 ใช่
$count_rs1=mysql_query("SELECT COUNT(polls_id) AS Total1 FROM polls WHERE polls_id LIKE'1'");
while($row_count1=mysql_fetch_assoc($count_rs1)) {
//$datay2[substr($row_count1['Total1'],8)-1] ;
//$datay = array[];
//$datay[]=$row_count1['Total1'];
$datay[]=$row_count1['Total1'];
}
//LIKE ไม่ใช่
$count_rs2=mysql_query("SELECT COUNT(polls_id) AS Total2 FROM polls WHERE polls_id LIKE'2'");
while($row_count2=mysql_fetch_assoc($count_rs2)) {
$datay2[]=$row_count2['Total2'];
}
//LIKE เฉยๆ
$count_rs3=mysql_query("SELECT COUNT(polls_id) AS Total3 FROM polls WHERE polls_id LIKE'3'");
while($row_count3=mysql_fetch_assoc($count_rs3)) {
$datay3[]=$row_count3['Total3'];
}
//$datay = array(12,8);//
/*$query_sum = "SELECT sum(payamount) as sumpay,paydate FROM dailypay WHERE 1 $paycond and createby = '2' GROUP BY paydate";
$sum = mysql_query($query_sum, $moneydb) or die(mysql_error());
while ($row_sum = mysql_fetch_assoc($sum)){
$datay2[substr($row_sum['paydate'],8)-1] = $row_sum['sumpay'] ;
}
*/
//$datay2 = array(12,8);//
$graphtitle = "test ".$monthname[intval($currentmonth)];
?>
<?php
include ("graph/jpgraph.php");
include ("graph/jpgraph_bar.php");
$data1y=$datay;//
$data2y=$datay2;//
$data3y=$datay3;//
//print_r($data2y);
// Create the graph. These two calls are always required
$graph = new Graph(1048,550); // กำหนดขนาด
$graph->SetScale("textlin");
$graph->legend->SetFont(FF_ANGSA,FS_BOLD,16); //SetFont(FF_FONT1,FS_BOLD);//
$graph->SetShadow();
$graph->img->SetMargin(40,30,20,40);
// Create the bar plots
$b1plot = new BarPlot($data1y);
$b1plot->SetFillColor("orange");
$b1plot->value->Show();
$b1plot->SetLegend('ชอบ');
$b2plot = new BarPlot($data2y);
$b2plot->SetFillColor("blue");
$b2plot->value->Show();
$b2plot->SetLegend('ไม่ชอบ');
$b3plot = new BarPlot($data3y);
$b3plot->SetFillColor("orange");
$b3plot->value->Show();
$b3plot->SetLegend('เฉยๆ');
// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
// ...and add it to the graPH
$graph->Add($gbplot);
$graph->title->SetFont(FF_ANGSA,FS_BOLD,20); // กำหนด font ไทย
$graph->title->Set("$graphtitle");
//$graph->title->Set("CSIM with popup windows");
$graph->xaxis->title->SetFont(FF_ANGSA,FS_NORMAL,12); // กำหนด font ไทย
$graph->xaxis->title->Set("");
$graph->yaxis->title->SetFont(FF_ANGSA,FS_NORMAL,12); // กำหนด font ไทย
$graph->yaxis->title->Set("จำนวน (บาท)");
//$graph->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
// Display the graph
$graph->Stroke(); /**/
?>
อยากแสดงเป็นกราฟ เป็นเปอร์ซนนะครบ
|
 |
 |
 |
 |
Date :
2013-04-06 12:26:00 |
By :
โต้ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|