|
|
|
พี่ๆ ช่วยด้วยครับ เรื่อง ลูปตาราง php array บวกกัน |
|
|
|
|
|
|
|
พี่ๆครับผมอยากให้ตรงที่วงไว้มันบวกกันอะครับ Code (PHP)
[size=10pt]โค้ดครับ
Code (PHP)
<?php
$con=mysqli_connect("localhost","root","","dbproduct");
$report = array();
$sql = "SELECT `out_p_id`, day(`out_date`) as `out_date`,`out_p_name`, SUM(`out_total`)as`out_total` ,MIN(`stocktotal`)as`stocktotal` ,MIN(`aptotal`)as`aptotal`
FROM product_out WHERE `out_date` LIKE'2022-10%' GROUP BY `out_p_id` ,`out_date` ORDER BY `out_p_id` ASC";
$result = mysqli_query($con , $sql) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($result)){
$report[$row['out_p_id']][$row['out_date']] = $row['out_total'];
$report2[$row['out_p_id']][$row['out_date']] = $row['stocktotal'];
$report3[$row['out_p_id']][$row['out_date']] = $row['aptotal'];
}
$emid = array();
$sqlid = "SELECT `id`, `id_name`,`name`,`stockmonth`,`applan` FROM product ORDER BY `id_name`";
$qry = mysqli_query($con , $sqlid) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($qry)){
$emid[$row['id_name']] = array ('namecode'=> $row['name'],'stock'=>$row['stockmonth'],'ap'=>$row['applan']);
/* $emid[$row['id_name']] = $row['name'];
$emid2[$row['id_name']] = $row['stockmonth'];
*/
}
$array_in = array();
$sqlproduct = "SELECT `id_c` , day(`date_c`) as `date_c`,`total_c` FROM product_in ORDER BY `id_c`";
$result = mysqli_query($con , $sqlproduct) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($result)){
$array_in[$row['id_c']][$row['date_c']] = $row['total_c'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
echo "<table border=1>";
foreach($emid as $empCode=>$array){
$nameC = $array['namecode'];
$stock = $array['stock'];
$ap = $array['ap'];
echo '<tr>';
echo '<th>รหัสสินค้า</th>';
echo '<th>ชื่อสินค้า</th>';
echo '<th>STOCK</th>';
echo '<th>AP/PLAN</th>';
echo '<th>รายการ</th>';
$year ="2022";
$month="10";
$timeDate = strtotime($year.'-'.$month."-01");
$lastDay = date("t", $timeDate);
for($day=1;$day<=$lastDay;$day++){
echo '<th>' . substr("0".$day, -02) . '</th>';
}
echo "</tr>";
echo '<tr>';
echo '<td>'. $empCode .'</td>';
echo '<td>'. $nameC .'</td>';
echo '<td>'. $stock .'</td>';
echo '<td>'. $ap .'</td>';
echo '<td>'. 'รับ' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$r4 = isset($array_in[$empCode][$j]) ? '<div>'.$array_in[$empCode][$j].'</div>' : '';
echo "<td class='number'>", $r4, "</td>" ;
}
echo "</tr>";
echo '<tr>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. ''.'</td>';
echo '<td>'. 'จ่าย' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$rr = isset($report[$empCode][$j]) ? '<div>'.$report[$empCode][$j].'</div>' : '';
echo "<td class='number'>", $rr, "</td>";
}
echo "</tr>";
echo '<tr>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. 'AP/PLAN คงเหลือ' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$rr2 = isset($report2[$empCode][$j]) ? '<div>'.$report2[$empCode][$j].'</div>' : '';
echo "<td class='number'>", $rr2, "</td>";
}
echo "</tr>";
echo '<tr>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. '' .'</td>';
echo '<td>'. 'STOCK คงเหลือ' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$rr3 = isset($report3[$empCode][$j]) ? '<div>'.$report3[$empCode][$j].'</div>' : '';
echo "<td class='number'>", $rr3, "</td>";
}
echo "</tr>";
echo '<tr>';
echo '<td colspan="36" align="center">'.'-----------------'. '</td>';
echo '</tr>';
}
mysqli_close($con);
echo "</table>";
?>
</body>
</html>
ขอบคุณครับ
Tag : PHP, HTML
|
|
|
|
|
|
Date :
2022-10-11 19:31:10 |
By :
Orikano |
View :
550 |
Reply :
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอเสริม จากกระทู้เก่าๆ ที่ผมต้องการคือ sample data ดีสุดและจะได้คำตอบเร็ว คือ โพสต์มาแบบนี้ คนอื่นจะได้ลองได้เมื่อสะดวก
Code (SQL)
CREATE TABLE "CUSTOMER"
( "CUST_CODE" VARCHAR2(6) NOT NULL PRIMARY KEY,
"CUST_NAME" VARCHAR2(40) NOT NULL,
"CUST_CITY" CHAR(35),
"WORKING_AREA" VARCHAR2(35) NOT NULL,
"CUST_COUNTRY" VARCHAR2(20) NOT NULL,
"GRADE" NUMBER,
"OPENING_AMT" NUMBER(12,2) NOT NULL,
"RECEIVE_AMT" NUMBER(12,2) NOT NULL,
"PAYMENT_AMT" NUMBER(12,2) NOT NULL,
"OUTSTANDING_AMT" NUMBER(12,2) NOT NULL,
"PHONE_NO" VARCHAR2(17) NOT NULL,
"AGENT_CODE" CHAR(6) NOT NULL REFERENCES AGENTS
);
INSERT INTO CUSTOMER VALUES ('C00013', 'Holmes', 'London', 'London', 'UK', '2', '6000.00', '5000.00', '7000.00', '4000.00', 'BBBBBBB', 'A003');
INSERT INTO CUSTOMER VALUES ('C00001', 'Micheal', 'New York', 'New York', 'USA', '2', '3000.00', '5000.00', '2000.00', '6000.00', 'CCCCCCC', 'A008');
INSERT INTO CUSTOMER VALUES ('C00020', 'Albert', 'New York', 'New York', 'USA', '3', '5000.00', '7000.00', '6000.00', '6000.00', 'BBBBSBB', 'A008');
INSERT INTO CUSTOMER VALUES ('C00025', 'Ravindran', 'Bangalore', 'Bangalore', 'India', '2', '5000.00', '7000.00', '4000.00', '8000.00', 'AVAVAVA', 'A011');
INSERT INTO CUSTOMER VALUES ('C00024', 'Cook', 'London', 'London', 'UK', '2', '4000.00', '9000.00', '7000.00', '6000.00', 'FSDDSDF', 'A006');
INSERT INTO CUSTOMER VALUES ('C00015', 'Stuart', 'London', 'London', 'UK', '1', '6000.00', '8000.00', '3000.00', '11000.00', 'GFSGERS', 'A003');
INSERT INTO CUSTOMER VALUES ('C00002', 'Bolt', 'New York', 'New York', 'USA', '3', '5000.00', '7000.00', '9000.00', '3000.00', 'DDNRDRH', 'A008');
INSERT INTO CUSTOMER VALUES ('C00018', 'Fleming', 'Brisban', 'Brisban', 'Australia', '2', '7000.00', '7000.00', '9000.00', '5000.00', 'NHBGVFC', 'A005');
INSERT INTO CUSTOMER VALUES ('C00021', 'Jacks', 'Brisban', 'Brisban', 'Australia', '1', '7000.00', '7000.00', '7000.00', '7000.00', 'WERTGDF', 'A005');
INSERT INTO CUSTOMER VALUES ('C00019', 'Yearannaidu', 'Chennai', 'Chennai', 'India', '1', '8000.00', '7000.00', '7000.00', '8000.00', 'ZZZZBFV', 'A010');
INSERT INTO CUSTOMER VALUES ('C00005', 'Sasikant', 'Mumbai', 'Mumbai', 'India', '1', '7000.00', '11000.00', '7000.00', '11000.00', '147-25896312', 'A002');
INSERT INTO CUSTOMER VALUES ('C00007', 'Ramanathan', 'Chennai', 'Chennai', 'India', '1', '7000.00', '11000.00', '9000.00', '9000.00', 'GHRDWSD', 'A010');
INSERT INTO CUSTOMER VALUES ('C00022', 'Avinash', 'Mumbai', 'Mumbai', 'India', '2', '7000.00', '11000.00', '9000.00', '9000.00', '113-12345678','A002');
INSERT INTO CUSTOMER VALUES ('C00004', 'Winston', 'Brisban', 'Brisban', 'Australia', '1', '5000.00', '8000.00', '7000.00', '6000.00', 'AAAAAAA', 'A005');
INSERT INTO CUSTOMER VALUES ('C00023', 'Karl', 'London', 'London', 'UK', '0', '4000.00', '6000.00', '7000.00', '3000.00', 'AAAABAA', 'A006');
INSERT INTO CUSTOMER VALUES ('C00006', 'Shilton', 'Torento', 'Torento', 'Canada', '1', '10000.00', '7000.00', '6000.00', '11000.00', 'DDDDDDD', 'A004');
INSERT INTO CUSTOMER VALUES ('C00010', 'Charles', 'Hampshair', 'Hampshair', 'UK', '3', '6000.00', '4000.00', '5000.00', '5000.00', 'MMMMMMM', 'A009');
INSERT INTO CUSTOMER VALUES ('C00017', 'Srinivas', 'Bangalore', 'Bangalore', 'India', '2', '8000.00', '4000.00', '3000.00', '9000.00', 'AAAAAAB', 'A007');
INSERT INTO CUSTOMER VALUES ('C00012', 'Steven', 'San Jose', 'San Jose', 'USA', '1', '5000.00', '7000.00', '9000.00', '3000.00', 'KRFYGJK', 'A012');
INSERT INTO CUSTOMER VALUES ('C00008', 'Karolina', 'Torento', 'Torento', 'Canada', '1', '7000.00', '7000.00', '9000.00', '5000.00', 'HJKORED', 'A004');
INSERT INTO CUSTOMER VALUES ('C00003', 'Martin', 'Torento', 'Torento', 'Canada', '2', '8000.00', '7000.00', '7000.00', '8000.00', 'MJYURFD', 'A004');
INSERT INTO CUSTOMER VALUES ('C00009', 'Ramesh', 'Mumbai', 'Mumbai', 'India', '3', '8000.00', '7000.00', '3000.00', '12000.00', 'Phone No', 'A002');
INSERT INTO CUSTOMER VALUES ('C00014', 'Rangarappa', 'Bangalore', 'Bangalore', 'India', '2', '8000.00', '11000.00', '7000.00', '12000.00', 'AAAATGF', 'A001');
INSERT INTO CUSTOMER VALUES ('C00016', 'Venkatpati', 'Bangalore', 'Bangalore', 'India', '2', '8000.00', '11000.00', '7000.00', '12000.00', 'JRTVFDD', 'A007');
INSERT INTO CUSTOMER VALUES ('C00011', 'Sundariya', 'Chennai', 'Chennai', 'India', '3', '7000.00', '11000.00', '7000.00', '11000.00', 'PPHGRTS', 'A010');
จะเห็นว่ามีข้อมูลบางส่วนเป็นข้อมูลส่วนตัวไม่สามารถเปิดเผยได้
ให้ทำการสมมติ เช่น ชื่อลูกค้า ก็อาจจะทำเป็น นาย ก. นาง A,. ... แบบนี้
โดย export จา่ก DB เป็น sql หรือ csv ก็ได้
Quote:
ด้านบนคือ sample data แบบ DDL (Data Definition Language) และ DML(Data Manipulation Language)
แบบเข้าใจง่ายๆ คือ มี CREATE TABLE และ INSERT DATA
หรือ ถ้าไม่สะดวก อย่างน้อยทำมาเป็น txt + delimiter(ตัวคั่นระหว่างคอลัมน์) เช่น
MemberTable
คนอื่นก็จะสามารถนำ sample data เป็น test และช่วยแก้ปัญหาได้สะดวก
|
|
|
|
|
Date :
2022-10-11 20:09:48 |
By :
009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เนต ยังไงก็ต้องใช้ คนละเครื่อง เชื่อมต่อกัน ก็คือ เนตเวิร์ค
อยู่ที่ว่าจะใช้ intra net (local web) หรือ internet (world wide web)
ส่วนอยากได้ผล รวม ก็ทำการบวก เอาเลย สร้างตัวแปรมารับ
Code (PHP)
<table>
<thead>
<tr><td>head ......</td></tr>
</thead>
<tbody>
<?php
$ttl_rcv = 0; // buffer รวมรับ
$ttl_stk = 0; // buffer รวม stock
while( $row = $result->fetch_xxxx ) {
$ttl_rcv += $row->rcv;
$ttl_stk += $row->stk;
?><tr><td>detail .......</td></tr>
<?php } ?>
</tbody>
<tfoot>
<tr><td>Total ....<?=$ttl_rcv?><br><?=$ttl_stk?></td></tr>
</tfoot>
</table>
|
ประวัติการแก้ไข 2022-10-11 21:19:55
|
|
|
|
Date :
2022-10-11 21:15:01 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดูจากลักษณะตารางคงต้องสร้างตัวแปรอาร์เรย์ไว้ก่อน แล้วเก็บค่าในการ รับ-จ่าย มาคำนวณจะง่ายกว่า query แล้ว sum นะครับ
total = ยกมา + รับ - จ่าย
total = $array['balance'][ วัน - 1] + $array_in[$empCode][$j] - $array_out[$empCode][$j]
|
|
|
|
|
Date :
2022-10-12 10:20:13 |
By :
{Cyberman} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 17 เขียนโดย : Orikano เมื่อวันที่ 2022-10-12 06:27:43
รายละเอียดของการตอบ ::
ที่บ้านมีงานบวช เสียงดังมาก งานผมไม่เดิน เลยมีเวลาดูให้
...แต่คงลักษณะการเขียนเดิมไว้ ไม่ได้ปัดขยะให้
ยังไง ดูคำแนะนำของทั้งสองท่านเอานะครับ
Code (PHP)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
// เปิดแสดง error (production อย่าลืมเอาออก)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// ======== Sample Data (ข้อมูลตัวอย่าง จำลองขึ้นแทน DB เพื่อความสะดวก) =======
$pOut = [
["out_p_id"=>"3063800-0091", "out_date"=>7, "out_p_name"=>"NH-303M", "out_total"=>17, "stocktotal"=>83, "aptotal"=>83],
["out_p_id"=>"3063800-0091", "out_date"=>8, "out_p_name"=>"NH-303M", "out_total"=>10, "stocktotal"=>73, "aptotal"=>73],
["out_p_id"=>"3063800-0091", "out_date"=>9, "out_p_name"=>"NH-303M", "out_total"=>9, "stocktotal"=>64, "aptotal"=>64],
["out_p_id"=>"3063800-0091", "out_date"=>10, "out_p_name"=>"NH-303M", "out_total"=>10, "stocktotal"=>54, "aptotal"=>54],
["out_p_id"=>"3063800-0195", "out_date"=>7, "out_p_name"=>"NH-1", "out_total"=>14, "stocktotal"=>66, "aptotal"=>66],
["out_p_id"=>"3063800-0195", "out_date"=>8, "out_p_name"=>"NH-1", "out_total"=>2, "stocktotal"=>64, "aptotal"=>64],
["out_p_id"=>"3063800-0195", "out_date"=>9, "out_p_name"=>"NH-1", "out_total"=>13, "stocktotal"=>51, "aptotal"=>51],
["out_p_id"=>"3063800-0195", "out_date"=>10, "out_p_name"=>"NH-1", "out_total"=>6, "stocktotal"=>45, "aptotal"=>45],
["out_p_id"=>"3064000-0010", "out_date"=>7, "out_p_name"=>"HISOLID", "out_total"=>16, "stocktotal"=>74, "aptotal"=>74],
["out_p_id"=>"3064000-0010", "out_date"=>8, "out_p_name"=>"HISOLID", "out_total"=>9, "stocktotal"=>65, "aptotal"=>65],
["out_p_id"=>"3064000-0010", "out_date"=>9, "out_p_name"=>"HISOLID", "out_total"=>9, "stocktotal"=>56, "aptotal"=>56],
["out_p_id"=>"3064000-0010", "out_date"=>10, "out_p_name"=>"HISOLID", "out_total"=>6, "stocktotal"=>50, "aptotal"=>50]
];
$prod = [
["id"=>1, "id_name"=>"3063800-0091", "name"=>"NH-303M", "stockmonth"=>100, "applan"=>100],
["id"=>3, "id_name"=>"3063800-0195", "name"=>"NH-1", "stockmonth"=>80, "applan"=>80],
["id"=>2, "id_name"=>"3064000-0010", "name"=>"HISOLID", "stockmonth"=>90, "applan"=>90]
];
$pIn = [
["id_c"=>"3063800-0091", "date_c"=>10, "total_c"=>10],
["id_c"=>"3063800-0195", "date_c"=>10, "total_c"=>20],
["id_c"=>"3064000-0010", "date_c"=>10, "total_c"=>30]
];
foreach($pOut as $o){
$report[$o['out_p_id']][$o['out_date']] = $o['out_total'];
$report2[$o['out_p_id']][$o['out_date']] = $o['stocktotal'];
$report3[$o['out_p_id']][$o['out_date']] = $o['aptotal'];
}
foreach($prod as $p){
$emid[$p['id_name']] = array ('namecode'=> $p['name'],'stock'=>$p['stockmonth'],'ap'=>$p['applan']);
}
foreach($pIn as $in){
$array_in[$in['id_c']][$in['date_c']] = $in['total_c'];
}
// ====================================================
echo "<table border=1>";
foreach($emid as $empCode=>$array){
$nameC = $array['namecode'];
$stock = $array['stock'];
/**** 1. เก็บค่าสุดท้ายของ "รับ และ stock" ****/
// income
$ap = $array['ap'];
$x = $array_in[$empCode];
$y = array_keys($x);
$z = $y[count($y)-1];
$income = (int)$x[$z];
// stock
$ap = $array['ap'];
$x = $report3[$empCode];
$y = array_keys($x);
$z = $y[count($y)-1];
$stock = (int)$x[$z];
/*****************************/
echo '<tr>';
echo '<th>รหัสสินค้า</th>';
echo '<th>ชื่อสินค้า</th>';
echo '<th>STOCK</th>';
echo '<th>AP/PLAN</th>';
echo '<th>รายการ</th>';
$year ="2022";
$month="10";
$timeDate = strtotime($year.'-'.$month."-01");
$lastDay = date("t", $timeDate);
for($day=1;$day<=$lastDay;$day++){
echo '<th>' . substr("0".$day, -02) . '</th>' ;
}
echo "</tr>" ;
echo '<tr>' ;
echo '<td>' . $empCode .'</td>';
echo '<td>' . $nameC .'</td>';
echo '<td>' . $stock .'</td>';
echo '<td>' . $ap .'</td>';
echo '<td>' . 'รับ' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$r4=isset($array_in[$empCode][$j]) ? '<div>' .$array_in[$empCode][$j].'</div>' : '' ;
echo "<td class='number'>" , $r4, "</td>" ;
}
echo "</tr>" ;
echo '<tr>' ;
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . 'จ่าย' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$rr=isset($report[$empCode][$j]) ? '<div>' .$report[$empCode][$j].'</div>' : '' ;
echo "<td class='number'>" , $rr, "</td>" ;
}
echo "</tr>" ;
echo '<tr>' ;
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . 'AP/PLAN คงเหลือ' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$rr2=isset($report2[$empCode][$j]) ? '<div>' .$report2[$empCode][$j].'</div>' : '' ;
echo "<td class='number'>" , $rr2, "</td>" ;
}
echo "</tr>" ;
echo '<tr>' ;
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . '' .'</td>';
echo '<td>' . 'STOCK คงเหลือ' .'</td>';
for($j=1;$j<=$lastDay;$j++){
$rr3=isset($report3[$empCode][$j]) ? '<div>' .$report3[$empCode][$j].'</div>' : '' ;
echo "<td class='number'>" , $rr3, "</td>" ;
}
echo "</tr>" ;
/**** 2. ผลรวม (วางส่วนไหนก็ได้) ****/
echo '<tr>' ;
echo '<td colspan="36" align="center">' .'รวม ' . ($income + $stock) . '</td>' ;
echo '</tr>' ;
/*****************************/
echo '<tr>' ;
echo '<td colspan="36" align="center">' .'-----------------'. '</td>' ;
echo '</tr>' ;
}
echo "</table>" ;
?>
</body>
</html>
|
|
|
|
|
Date :
2022-10-12 11:38:02 |
By :
009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|