循环遍历数组(创建动态表)

时间:2016-12-15 09:31:41

标签: php arrays foreach html-table tcpdf

我试图为TCPDF文档创建动态表,但循环查询结果非常不舒服。

This is what I'm trying to get

保存SQL查询的

$ arrResult 变量如下所示:

array(1) { 
    ["Dec"]=> array(3) { 
        [0]=> array(1) { 
            [0]=> array(2) { 
                ["name"]=> string(4) "Jane" 
                ["message"]=> string(0) "test" 
            } 
        } 
        [1]=> array(1) { 
            [0]=> array(2) { 
                ["name"]=> string(6) "Oliver" 
                ["message"]=> string(20) "test!" 
            } 
        } 
        [2]=> array(1) { 
            [0]=> array(2) { 
                ["name"]=> string(4) "John" 
                ["message"]=> string(4) "test" 
            } 
        } 
    } 
}

我设法循环大数组并获得每个月但是如何将其他数据放入表中。这是代码的一部分:

$content = '';
     $content .= '<table border="1" cellpadding="1">';

        foreach($arrResults as $key=>$value) {
            $content .= "<tr><th style='font-weight:bold;font-size:12px' colspan='8' align='center'>".$key."</th></tr>";
                foreach($arrResults[$key] as $key2=>value2) {

                }
        }  

      $content .= '</table>';

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

感谢您的回答!

现在可以使用这种方法:

$content = '';  
$content .= '<table border="1" cellpadding="1">';

foreach($arrResults as $key=>$value) {
     $content .= '<tr><th style="font-weight:bold;font-size:12px" colspan="8" align="center">'. $key .'</th></tr>';
     $content .= '<tr>
              <th style="font-weight:bold;font-size:12px">ID</th>
              <th style="font-weight:bold;font-size:12px">Nume</th>
              <th style="font-weight:bold;font-size:12px">Prenume</th>
              <th style="font-weight:bold;font-size:12px">Email</th>
              <th style="font-weight:bold;font-size:12px">Rol</th>
              <th style="font-weight:bold;font-size:12px">Data Programare</th>
              <th style="font-weight:bold;font-size:12px">Mesaj</th>
              <th style="font-weight:bold;font-size:12px">Status</th>
           </tr>';
         foreach($value as $k => $v) {
         //var_dump($v);die();
            $content .= '<tr>';
            $content .= '<td>'. $v[0]['id'] .'</td>';
            $content .= '<td>'. $v[0]['last_name'] .'</td>';
            $content .= '<td>'. $v[0]['first_name'] .'</td>';
            $content .= '<td>'. $v[0]['email'] .'</td>';
            $content .= '<td>'. $v[0]['role'] .'</td>';
            $content .= '<td>'. $v[0]['appointment_date'] .'</td>';
            $content .= '<td>'. $v[0]['message'] .'</td>';
            $content .= '<td>'. $v[0]['status'] .'</td>';
            $content .= '</tr>';
         }
}
$content .= '</table>';

答案 1 :(得分:0)

 $content = '';
 $content .= '<table border="1" cellpadding="1">';

    foreach($arrResults as $key=>$value) {
        $content .= "<tr><th style='font-weight:bold;font-size:12px' colspan='8' align='center'>".$key."</th></tr>";
        $content .= "<tr>
                <th>id</th>
                <th>num</th>
                <th>prenum</th>
                <th>email</th>
                <th>rol</th>
                <th>data</th>
                <th>mesaj</th>
                <th>statue</th>
                </tr>";
            foreach($value as $key2=>$value2) 
            {
                $content .= '<tr><td>'.$key2.'</td>';
                foreach($value2 as $key3=>$value3) 
                {
                    $content .= '<td>'.$value3['num'].'</td>';
                    $content .= '<td>'.$value3['prenum'].'</td>';
                    $content .= '<td>'.$value3['email'].'</td>';
                    $content .= '<td>'.$value3['rol'].'</td>';
                    $content .= '<td>'.$value3['data'].'</td>';
                    $content .= '<td>'.$value3['mesaj'].'</td>';
                    $content .= '<td>'.$value3['statue'].'</td>';
                }
            $content .= '</tr>';
            }
    }  

  $content .= '</table>';

我想你想要像他一样想,我肯定会犯一些错误。

答案 2 :(得分:0)

$.get(url, function(response,provider){ 

    // Your code ... 

});