如何处理此数组以获得此结果?

时间:2013-10-26 22:02:32

标签: php arrays

我有这个数组,

      array (size=6)
      0 => 
      array (size=6)
      'id' => string '2' (length=1)
      'member_ext' => string '1' (length=1)
      'queue_id' => string '1' (length=1)
      'LastUpdate' => string '2013-10-24' (length=10)
      'member_name' => string 'John Smith' (length=10)
      'queue_name' => string 'queue 1' (length=7)
      1 => 
      array (size=6)
      'id' => string '3' (length=1)
      'member_ext' => string '2' (length=1)
      'queue_id' => string '1' (length=1)
      'LastUpdate' => string '2013-10-24' (length=10)
      'member_name' => string 'Jose Jones' (length=10)
      'queue_name' => string 'queue 1' (length=7)
      2 => 
      array (size=6)
      'id' => string '5' (length=1)
      'member_ext' => string '1' (length=1)
      'queue_id' => string '1' (length=1)
      'LastUpdate' => string '2013-10-24' (length=10)
      'member_name' => string 'John Smith' (length=10)
      'queue_name' => string 'queue 1' (length=7)
      3 => 
      array (size=6)
      'id' => string '4' (length=1)
      'member_ext' => string '3' (length=1)
      'queue_id' => string '2' (length=1)
      'LastUpdate' => string '2013-10-24' (length=10)
      'member_name' => string 'Mary Chase' (length=10)
            'queue_name' => string 'queue 2' (length=7)
      4 => 
      array (size=6)
      'id' => string '7' (length=1)
      'member_ext' => string '3' (length=1)
      'queue_id' => string '3' (length=1)
      'LastUpdate' => string '2013-10-24' (length=10)
      'member_name' => string 'Mary Chase' (length=10)
      'queue_name' => string 'queue 3' (length=7)
      5 => 
      array (size=6)
      'id' => string '6' (length=1)
      'member_ext' => string '4' (length=1)
      'queue_id' => string '4' (length=1)
      'LastUpdate' => string '2013-10-24' (length=10)
      'member_name' => string 'Fred Allen' (length=10)
      'queue_name' => string 'queue 4' (length=7)

我怎样才能使用php将它变成这样的html表? “登录”意味着它们在数组中被发现为beining登录到特定队列,“已注销”意味着在该特定队列中找不到该技术。

| techs     | queue 1    | queue 2    | queue 3    | queue 4    | queue 5    |
------------------------------------------------------------------------------
|John Smith | logged in  | logged in  | logged out | logged out | logged out |
|Jose Jones | logged in  | logged out | logged out | logged out | logged out |
|Mary Case  | logged out | logged out | logged in  | logged out | logged out |
|Fred Allen | logged out | logged out | logged out | logged in  | logged out |

编辑 - 这就是我所做的事情。

    <?php
    try {
$username = "admin";
//$password = "TrojanF32";
    $dbh = new PDO('mysql:host=localhost;dbname=test', $username);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "connected<p>";
    } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    }


$sth = $dbh->prepare("SELECT DISTINCT queue_name
FROM `log`
WHERE LastUpdate = '2013-10-24'
ORDER BY queue_name ASC
");
$sth->execute();
$sql1 = $sth->fetchAll(PDO::FETCH_ASSOC);



$sth = $dbh->prepare("SELECT * 
FROM `log`
WHERE LastUpdate = '2013-10-24'
ORDER BY queue_name ASC
");
$sth->execute();
$sql2 =$sth->fetchAll(PDO::FETCH_ASSOC);

?><div class="container"><?php

echo "<table border=1>";
echo  "<tr>";
echo "<td>" . "Technician" . "</td>";
foreach($sql1 as $header){
echo "<td>" . $header['queue_name'] . "</td>";  
}
echo "</tr>";

foreach($sql2 as $queue){
    echo "<td>" . $queue['member_name'] . "</td>";
    //echo "<td>";
    foreach($queue as $que){

        if($queue['queue_id'] == '1'){
            echo "<td>" . "logged in" . "</td>";
        }else{
            echo "<td>" . "logged out" . "</td>";
            }
        if($queue['queue_id'] == '2'){
            echo "<td>" . "logged in" . "</td>";
        }else{
            echo "<td>" . "logged out" . "</td>";
            }
        if($queue['queue_id'] == '3'){
            echo "<td>" . "logged in" . "</td>";
        }else{
            echo "<td>" . "logged out" . "</td>";
            }
        if($queue['queue_id'] == '4'){
            echo "<td>" . "logged in" . "</td>";
        }else{
            echo "<td>" . "logged out" . "</td>";
            }
        if($queue['queue_id'] == '5'){
            echo "<td>" . "logged in" . "</td>";
        }else{
            echo "<td>" . "logged out" . "</td>";
        }
        echo "</tr>";
    }
}

echo "<pre>";
var_dump($sql2);
echo "</pre>";




?>
</table>

</div>

1 个答案:

答案 0 :(得分:1)

由于你只需要队列和技术名称,我会更改你的代码,所以看看这对你有帮助。

我没有要查看的php环境,所以,也许你可以找到一些语法错误。真实的是添加第三个查询来获取每个队列中的每个队列,就像我在这里SQLFiddle

一样

所以,让我们看看:

<?php
$sth = $dbh->prepare("SELECT DISTINCT queue_name
                      FROM `log`
                      WHERE LastUpdate = '2013-10-24'
                      ORDER BY queue_name ASC");
$sth->execute();
$sql1 = $sth->fetchAll(PDO::FETCH_ASSOC);

$sth = $dbh->prepare("SELECT DISTINCT member_name
                      FROM `log`
                      WHERE LastUpdate = '2013-10-24'
                      ORDER BY member_name ASC");
$sth->execute();
$sql2 = $sth->fetchAll(PDO::FETCH_ASSOC);

$sth = $dbh->prepare("select ml.member_name, ml.queue_name,
        case ifnull(l.id,-1) when -1 then 'Logging Out' else 'Logging In' end as logstatus
   from (select l1.member_name, l2.queue_name 
           from (select distinct member_name from `log` where lastupdate='2013-10-24') l1,
                (select distinct queue_name from `log` where lastupdate='2013-10-24') l2
         ) ml 
         left join 
         (select * from `log` where LastUpdate='2013-10-24') as l 
         on ( ml.member_name = l.member_name and ml.queue_name = l.queue_name )
 order by ml.member_name, ml.queue_name");
$sth->execute();
$sql3 = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="container">
<?php
echo "<table border=1>";
echo  "<tr>";
echo "<td>" . "Technician" . "</td>";
foreach($sql1 as $header){
    echo "<td>" . $header['queue_name'] . "</td>";  
}
echo "</tr>";

foreach($sql2 as $names){
    echo "<tr>";
    echo "<td>" . $names['member_name'] . "</td>";
    foreach($sql3 as $queues){
        if ( $names['member_name'] == $queues['member_name'] ){
            echo "<td>" . $queues['logstatus'] . "</td>";
        }
    }
    echo "</tr>";
}
echo "</table>";
?>
</div>

如果您遇到此代码的任何问题,请告诉我。