PHP爆炸数据并显示在表格中

时间:2016-05-03 13:23:49

标签: php mysql

用PHP编码时遇到问题 我在mysql中有这样的数据:

$data_room = '2010,2011|Math-9,Informatic-8,History-6,Moresingle-5,Other-8
2011,2012|Math-6,Informatic-7,History-5,Moresingle-3,Other-7';
echo '<table><tbody>';
$data = explode("\n", $data_room);
foreach($data as $row){
    echo '<tr>';
    $row = explode('|',$row);
    echo '<td>';
    foreach($row as $cell){
        $row2 = explode(',',$cell);
        echo $row2[0].'->'.$row2[0].'</td></tr><tr>';
        foreach($row2 as $cell2){
            echo '<td>';
            echo $cell2;
            echo '</td>';
        }
        echo '</tr>';
    }
}
echo '</tbody></table>';

现在我想在这样的表中显示它:

Table that I want to show

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我认为首先你需要为每个表创建一个表头。然后为每一行创建另一行。你还需要为每个嵌套做一个并爆炸()。 请查看以下脚本。这可能会对你有帮助。

<?php
$data_room = '2010,2011|Math-9,Informatic-8,History-6,Moresingle-5,Other-8
2011,2012|Math-6,Informatic-7,History-5,Moresingle-3,Other-7
2012,2013|Math-65,Informatic-17,History-15,Moresingle-13,Other-17';
echo '<table border="1" ><tbody>';
$data = explode("\n", $data_room);
$header = explode('|',$data[0]);
$header_row = explode(',',$header[1]);
/*Table header*/

 echo "<tr>";
 echo "<td>&nbsp;</td>";
 foreach($header_row as $subject_mark)
 {
    $subject = explode('-',$subject_mark);
    echo "<td>".$subject[0]."</td>";

 }
 echo "</tr>";

 foreach($data as $row){
 $head_contents = explode('|',$row);

 /*Table contents*/
 echo "<tr>";
 echo "<td>".str_replace(',','-',$head_contents[0])."</td>";
 $subject_marks = explode(',',$head_contents[1]);
 foreach($subject_marks as $subject_mark)
 {
    $mark = explode('-',$subject_mark);
    echo "<td>".$mark[1]."</td>";

 }
 echo "</tr>";

}
echo '</tbody></table>';
?>

答案 1 :(得分:0)

以下是我建议您尝试的内容...只需复制下面的整个代码并运行它以查看您获得的内容: 以下是您所看到的内容: enter image description here

    <?php
        $data_room  = '2010,2011|Math-9,Informatic-8,History-6,Moresingle-5,Other-8
        2011,2012|Math-6,Informatic-7,History-5,Moresingle-3,Other-7';


        $data       = array_map('trim', explode("\n", $data_room));

        //EXTRACT THE HEADER OUT OF THE FIRST DATA-ROW... DISCARDING THE DATE-RANGE PART
        $headerData = explode(",", preg_replace("&(\-\d{1,})|(^[0-9,]+\|)&", "", $data[0]));

        //CSS STYLES - JUST FOR TESTING
        $tblStyle       = "font-family: Helvetica, Arial, sans-serif;font-size: 13px;border-left:solid 1px #8A8A8A;";
        $thhStyle       = "";
        $trrStyle       = "background: #aeaeae; padding:10px; text-align:left;";
        $tr1Style       = "background: rgba(174, 174, 174, 0.70); padding:10px; text-align:left;";
        $tr2Style       = "background: rgba(174, 174, 174, 0.40); padding:10px; text-align:left;";
        $thStyle        = "padding:10px 20px; text-align:left; vertical-align:top;border:solid 1px #8A8A8A;border-left:none;";
        $tdStyle        = "padding:10px 20px; text-align:left; vertical-align:top;border-right:solid 1px #8A8A8A;border-bottom:solid 1px #8A8A8A;";

        //BUILD THE TABLE HEADER
        $htmlDisplay    = "<table  cellpadding='0' cellspacing='0' class='' id='' style='{$tblStyle}'>";
        $htmlDisplay   .= "<thead class='' id='' style='{$thhStyle}'>";
        $htmlDisplay   .= "<tr class='' id='' style='{$trrStyle}'>";
        $htmlDisplay   .= "<th class='' style='border:solid 1px #8A8A8A;border-left:none;'></th>";

        foreach($headerData as $key=>$val){
            $htmlDisplay   .= "<th class='' style='{$thStyle}'>{$val}</th>";
        }
        $htmlDisplay   .= "</tr>";
        $htmlDisplay   .= "</thead>";
        $htmlDisplay   .= "<tbody class='' id=''>";


        //BUILD THE CELLS (SLOTS FOR EACH UNIQUE DATA)... BUILDING THE TABLE BODY...
        foreach($data as $intKey=>$strVal){
            list($dateRange, $strData)      = explode("|", $strVal);
            $dateRangeVal                   = str_replace(",", " - ", $dateRange);
            $rowData                        = explode(",", preg_replace("&([^0-9,]*)&", "", $strData));
            $trCSS                          = ($intKey%2 == 0)? $tr1Style : $tr2Style;

            $htmlDisplay                   .= "<tr class='' id='' style='{$trCSS}'>";
            $htmlDisplay                   .= "<td class='' style='{$tdStyle}'>{$dateRangeVal}</td>";

            foreach($rowData as $intK=>$strV){
                $htmlDisplay               .= "<td class='' style='{$tdStyle}' >{$strV}</td>";
            }
            $htmlDisplay                   .= "</tr>";

        }
        $htmlDisplay   .= "</tbody>";
        $htmlDisplay   .= "</table>";

        // DISPLAY THE NICELY BUILT TABLE ;-) ... 
        // HMM! THE TABLE ALSO HAS A HEADER TOO: AIN'T THAT COOL?
        echo $htmlDisplay;

从我看到你在你的代码中所做的事情来看,它看起来并不像你正在切割的数据&amp;切片直接来自数据库。 看起来你更像是在处理一个数据块(可能是从mysql命令行复制的)。如果您直接访问数据库,那么这根本不是这种情况的解决方案....