 |
|
|
 |
 |
|
Code ตัวอย่าง (PHP)
$sum = 580;
$total = 2500;
// คิดเปอร์เซ็นต์ของ 580 จาก 2500
$percent = number_format(($sum*100)/$total,2);
echo $percent.'%';
|
 |
 |
 |
 |
Date :
2013-04-05 22:38:08 |
By :
mangkunzo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?
$conn=mysql_connect("localhost","root","111");
$dbname=mysql_select_db("polls");
//LIKE 1
//$sql=mysql_query("SELECT month(date_time) as full_date ,polls_id FROM polls WHERE polls_id LIKE '1'");
$sql=mysql_query("SELECT COUNT(polls_id) AS Total1 FROM polls WHERE polls_id LIKE'1'");
while($rs=mysql_fetch_assoc($sql)) {
$sum = $rs['Total1'];
$total = $rs['Total1'];
// คิดเปอร์เซ็นต์ของ 580 จาก 2500
$percent = number_format(($sum*100)/$total,2);
echo $percent.'%';
}
?>
ไม่ได้ครับ
|
ประวัติการแก้ไข 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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สูตรอัตราส่วนง่ายๆ ครับ
x / y * 100
x คือจำนวนที่ต้องการหา %
y คือจำนวนสูงสุด (100%)
เช่น
480 / 640 จะได้ 0.75
แล้วเอาไป * ด้วย 100 ก็จะได้ 75
Code (PHP)
$x = 480;
$y = 640;
$p = $x / $y * 100;
echo number_format($p, 2);
|
 |
 |
 |
 |
Date :
2013-04-06 01:54:00 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สูตรที่ใช้มีให้แล้ว แต่คำถามคือ คุณอยากได้เปอร์เซ็นของอะไร
เช่น เปอร์เซ็นของคนที่ตอบ ใช่ เป็นเท่าไหร่ และที่ตอบไม่เท่าไหร่ และที่เฉยๆ เท่าไหร่
Code (PHP)
//ยอดทั้งหมดที่มีคนตอบหัวข้อนี้
$sql = "SELECT COUNT(polls_id) AS Total FROM polls WHERE polls_id LIKE '1' ";
สมมติว่าได้ 65 เรคอร์ด หรือคนตอบ 65 ครั้ง
//หาจำนวนคำตอบของแต่ละตัวเลือก
$sql = "SELECT answer, COUNT(polls_id) AS TotalAnswer FROM polls WHERE polls_id LIKE '1' ";
$sql .= " GROUP by answer ";
>> QUERY
>> WHILE FETCH array
ถ้าโพลนี้มีคำตอบแค่ 3 ตัวเลือก ในลูปนี้ก็จะมีแค่ 3 แถว
$arrAnswer[$row['answer']] = $row['TotalAnswer'];
>> END WHILE
เช่น
แถวที่ 1 answer "yes" = 30
แถวที่ 2 answer "no" = 20
แถวที่ 3 answer " ignore" = 15
ข้อมูลที่ได้ทั้งหมด จะได้ตัวแปร ดังนี้
$total = 65;
$arrAnswer['yes'] = 30;
$arrAnswer['no'] = 20;
$arrAnswer['ignore'] = 15;
1. หาเปอร์เซ็นคนตอบ "ใช่"
$percent = ($arrAnswer['yes'] * 100) / $total ;
2. หาเปอร์เซ็นคนตอบ "ไม่"
$percent = ($arrAnswer['no'] * 100) / $total ;
3. หาเปอร์เซ็นคนตอบ "เฉยๆ"
$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 :
โต้ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|