|
|
|
ติดปัญหาเรื่องการวนลูป แสดงในตารางแบบมีเงื่อนไขครับ |
|
|
|
|
|
|
|
นี่ครับโค๊ดที่ผมทำ
Code (PHP)
<?php
// $today = date("Ymd");
$today = "20150317";
$strDate[1] = date("Ymd",strtotime("-2 day",strtotime($today)));
$strDate[2] = date("Ymd",strtotime("-1 day",strtotime($today)));
$strDate[3] = date("Ymd",strtotime("now",strtotime($today)));
$strDate[4] = date("Ymd",strtotime("+1 day",strtotime($today)));
$strDate[5] = date("Ymd",strtotime("+2 day",strtotime($today)));
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
$sql = "SELECT * FROM item GROUP BY item_name";
$result = $conn->query($sql);
if($result->num_rows > 0){
echo "<table border=1><tr><th rowspan=2>Name</th><th colspan=2>".$strDate[1]."</th><th colspan=2>".$strDate[2]."</th><th colspan=2>".$strDate[3]."</th><th colspan=2>".$strDate[4]."</th><th colspan=2>".$strDate[5]."</th></tr>";
echo "<tr><th >time</th><th>giver</th><th>time</th><th>giver</th><th>time</th><th>giver</th><th>time</th><th>giver</th><th>time</th><th>giver</th></tr>";
while($row = $result->fetch_assoc()){
$item_meal = $row["item_meal"];
$item_date = $row["item_date"];
$item_take = $row["item_take"];
$item_name = $row["item_name"];
$item_giver = $row["item_giver"];
$item_status = $row["item_status"];
echo "<tr><td rowspan=4>".$item_name."</td>";
echo "<td id=hi>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "BF")||($item_meal == "S"))){
echo $item_giver;
}"</td></tr>";
echo "<tr><td id=hi>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "LN")||($item_meal == "S"))){
echo $item_giver;
}"</td></tr>";
echo "<tr><td id=hi>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "DN")||($item_meal == "S"))){
echo $item_giver;
}"</td></tr>";
echo "<tr><td id=hi>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150315"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150316"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150317"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150318"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_giver;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_take;
}"</td>";
echo "<td>"?><?php
if((($item_status == "Y")&&($item_date == "20150319"))&&(($item_meal == "BD")||($item_meal == "S"))){
echo $item_giver;
}"</td></tr>";
}
echo "</table>";
}
else{
echo "result empty";
}
}
$conn->close();
?>
มันแสดงออกมาผิดครับ
|
ประวัติการแก้ไข 2015-03-23 11:16:09 2015-03-23 11:17:00
|
|
|
|
Date :
2015-03-23 11:08:38 |
By :
jojojoestar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีใคร มีแนวทางบ้างไหมครับ
ลองค้นหาดูแล้ว ไม่เจอเลยครับ
|
|
|
|
|
Date :
2015-03-23 13:23:19 |
By :
jojojoestar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูครับ ไม่ได้เทสต์
Code (PHP)
<?php
$new = array(); $cap_date = array(); $caption=''; $sub_cap='';
////////////////////////////////////////////////////////////////
$sql = "SELECT item_date FROM item group by item_date ";
$result = $conn->query($sql);
while( $row=$result->fetch_assoc()){
$cap_date[$row['item_date']]=array( 'data'=>array(), 'count'=>0 );
}
$js_buff = json_encode( $cap_date ); // สร้าง json string เก็บค่า array
////////////////////////////////////////////////////////////////
$sql = "SELECT * FROM item order by item_name, item_date, item_take ";
$result = $conn->query($sql);
$old = '';
while( $row=$result->fetch_assoc()){
$nm=$row['item_name'];
if( $old!=$nm){
if($old) $new[$old]['mx']=$mx;
$old=$nm; $mx = 0;
$new[$nm]=array( 'mx'=>0, 'dte'=>json_decode( $js_buff));
// ใช้ json แปลงค่า ป้องกัน การอ้างอิง แบบ pointer address
}
$new[$nm]['dte'][ $row['item_date'] ][] = $row;
if( $mx<($x = count( $new[$nm]['dte'][ $row['item_date'] ]) ) ) $mx = $x;
}
if($old) $new[$old]['mx']=$mx;
////////////////////////////////////////////////////////////////
foreach($cap_date as $dte=>$vl) {
$caption .='<th colspan=2>', $dte, '</th>';
$sub_cap .="<th>time</th><th>giver</th>";
}
////////////////////////////////////////////////////////////////
$tr = ''; $tmp_y = array('item_take'=>'', 'item_giver'=>'');
foreach($new as $nm=>$dte){
$ln=$new[$nm]['mx'];
for($i=0; $<$ln; $i++){
$tr .= ($i? '<tr>' : "<tr><td rowspan=$ln >$nm</td>");
foreach( $cap_date as $dte=>$vl){
$y = isset( $new[$nm]['dte'][$dte][$i]) ? $new[$nm]['dte'][$dte][$i] : $tmp_y;
$tr .= '<td>'.$y['item_take'].'</td><td>'.$y['item_giver'].'</td>';
} $tr.='</tr>';
}
}
///////////////////////////////////////////////////////////////
?>
<table border=2>
<tr><th rowspan=2>Name</th><?=$caption?></tr>
<tr><?=$sub_cap?></tr>
<?=$tr?>
</table>
|
|
|
|
|
Date :
2015-03-23 15:50:47 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|