Lotto x PHP 統計

2022/12/01閱讀時間約 7 分鐘
本範例為 PHP 載入 CSV檔,進行數據統計,並計算出各號碼開出次數。
載入CSV檔的作法
$file = "00.csv";
$openfile = fopen($file, "r");
$cont = fread($openfile, filesize($file));
echo $cont;
將資料進行統計整理
$row = 1;
$datax=["0"];
$handle = fopen("00.csv","r");
while ($data = fgetcsv($handle, 1000, ",")) {
	$num = count($data);
    $row++; //跑每行
    for ($c=0; $c < $num; $c++) { //跑單筆
		array_push($datax,$data[$c]);
		}
		console_log($datax[$row]);
}
fclose($handle);
將資料放於陣列的方式
array_push($datax,$data[$c]);
自定義函式
console_log($datax[$row]);
function console_log($output, $with_script_tags = true) {
    $js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . 
');';
    if ($with_script_tags) {
        $js_code = '<script>' . $js_code . '</script>';
    }
    echo $js_code;
}
完整原碼
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 30px;
  padding-right: 40px;
}
</style>
<?php
$row = 1;
$datax=["0"];
$handle = fopen("00.csv","r");
while ($data = fgetcsv($handle, 1000, ",")) {
	$num = count($data);
    $row++; //跑每行
    for ($c=0; $c < $num; $c++) { //跑單筆
		array_push($datax,$data[$c]);
		}
		console_log($datax[$row]);
}
fclose($handle);
function console_log($output, $with_script_tags = true) {
    $js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . 
');';
    if ($with_script_tags) {
        $js_code = '<script>' . $js_code . '</script>';
    }
    echo $js_code;
}
echo "<br>";
echo "<hr>";
echo "<center>目前各號出現次數</center>";
echo "<hr>";
echo "<br>";
echo "<table>";
$answer = array_count_values($datax);
for ($c=1; $c < 50; $c++) { 
if($c==1)
echo "<tr>";
else if ($c==11)
echo "<tr>";
else if ($c==21)
echo "<tr>";
else if ($c==31)
echo "<tr>";
else if ($c==41)
echo "<tr>";
	
	echo "<td>";
	echo $c."號:".$answer[$c]. "次<br>\n";;
	echo "</td>";
if($c==10)
echo "</tr>";
else if ($c==20)
echo "</tr>";
else if ($c==30)
echo "</tr>";
else if ($c==40)
echo "</tr>";
else if ($c==49)
echo "</tr>";
        }
echo "</table>";
echo "<br>";
echo "<hr>";
echo "<center>歷史紀錄</center>";
echo "<hr>";
echo "<br>";
echo "<table>";
$row = 1;
$datax=["0"];
while ($data = fgetcsv($handle, 1000, ",")) {
	$num = count($data);
    $row++; //跑每行
	echo "<tr>";
    for ($c=0; $c < $num; $c++) { //跑單筆
		echo "<td>";	
		array_push($datax,$data[$c]);
		echo $data[$c];
		echo "</td>";
		}
		console_log($datax[$row]);
	echo "</tr>";
}
fclose($handle);
echo "</table>";
?>
為什麼會看到廣告
illustration
贊助支持創作者,成為他繼續創作的動力吧!
38會員
127內容數
獨立遊戲開發紀錄
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容