如果由foreached数据变量调节,如何回显foreach()数据变量?

时间:2015-05-05 08:20:30

标签: php

我有一个表tbl_tmpdtr和tbl_employee并使用此查询来获取所需的数据:

SELECT 
    tbl_tmpdtr.id, 
    tbl_tmpdtr.userid, 
    date(tbl_tmpdtr.in_out) as in_out,
    time(tbl_tmpdtr.in_out) as dtr_time, 
    tbl_tmpdtr.status,
    tbl_employee.emp_id,
    tbl_employee.emp_fname,
    tbl_employee.emp_mname,
    tbl_employee.emp_lname
FROM 
    tbl_tmpdtr 
INNER JOIN 
    tbl_employee 
ON 
    tbl_tmpdtr.userid = tbl_employee.emp_id 
WHERE 
    tbl_tmpdtr.userid='(some id)'
GROUP BY 
    tbl_tmpdtr.userid

sample output query:
id | userid |  inp_out   | dtr_time | status
1  | 1002   | 2015-05-01 | 09:00:00 | In
2  | 1002   | 2015-05-01 | 18:30:00 | Out
  

我在我的视图中预测了这些数据,我在那里有两个单独的输入   标记为“In”和“Out”的类型,我的问题是放置dtr_time   09:00:00在输入类型中标记为“In”如果状态='在',则打开   另一方面,下一个dtr_time 18:30:00在输入类型中标记   如果状态='Out',则为“OUT”?

我试过,但即使在dtr_time,状态为OUT也会转到标记为In的输入类型。

这是我尝试过的。

  <?php if(!empty($dtr)){foreach ($dtr as $dtrs): ?>

      <h4>  
        <label for="exampleInputEmail1"> Full name: </label>
          <?php echo ucfirst($dtrs->emp_lname).", ".ucfirst($dtrs->emp_fname).", ".ucfirst($dtrs->emp_mname)."."; echo "</br>";?>              
        <label for="exampleInputEmail1"> EMP_ID: </label>
          <?php echo $dtrs->emp_id;?></br></br>
      </h4>
                  <div>              
                        <table class="display table table-bordered table-striped">
                        <thead>
                          <tr>
                            <th bgcolor="2ECC71"><center><font color="white">Date(s)</font></center></th>
                            <th bgcolor="2ECC71"><center><font color="white">In</font></center></th>
                            <th bgcolor="2ECC71"><center><font color="white">Out</font></center></th>
                          </tr>
                        </thead>  
                          <tr>
                          <center>
                            <td align="center"><input type="date" value="<?php echo $dtrs->in_out;?>"></td>
                            <?php if($dtrs->status == 'In'){?>
                            <td align="center"><input type="text" value="<?php echo $dtrs->dtr_time;} else if($dtrs->status == 'Out'){?>"></td>  
                            <td align="center"><input type="text" value="<?php echo $dtrs->dtr_time; } else { echo "do nothing";}?>"></td>
                          </tr>
                          </center>
                        </table>
                  </div>
  <?php endforeach; }?> 

2 个答案:

答案 0 :(得分:1)

如果按userid,tbl_tmpdtr.in_out排序,则可以迭代数组并检查下一个索引的用户ID。

<?php
$count = count($result);
for($i=0;$i<$count;$i++){
    if (($i+1) >= $count) {
        echo "User: " + $result[$i]["userid"];
        echo "Status:" +$result[$i]["status"];
        echo "Date: " + $result[$i]["inp_out"] + " - " + $result[$i]["dtr_time"];
    }

    if (
    $result[$i]["userid"] == $result[($i+1)]["userid"] &&
    $result[$i]["status"] == "In" && $result[($i+1)]["status"] == "Out" ) {
        echo "User: " + $result[$i]["userid"];
        echo "In: " + $result[$i]["inp_out"] + " - " + $result[$i]["dtr_time"];
        echo "Out: " + $result[$i+1]["inp_out"] + " - " + $result[$i+1]["dtr_time"];
        i++;
    }
    else {
        echo "User: " + $result[$i]["userid"];
        echo $result[$i]["status"]": " + $result[$i+1]["inp_out"] + " - " + $result[$i+1]["dtr_time"];
    }
}

但不应该更多#Out + 1&#39; Ins&#39;而不是&#39; Outs&#39;

答案 1 :(得分:0)

您可以通过这种方式完成它按需工作

    <tr>
        <td align="center"><input type="date" value="<?php echo $dtrs['inp_out']; ?>"></td>
        <td align="center"><input type="text" value="<?php echo (isset($dtrs['status']) && $dtrs['status'] === 'In') ? $dtrs['dtr_time'] : '----' ?>"></td>  
        <td align="center"><input type="text" value="<?php echo (isset($dtrs['status']) && $dtrs['status'] === 'Out') ? $dtrs['dtr_time'] : '----' ?>"></td>
    </tr>

您的代码in_out中的拼写错误必须为inp_out