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

I have a problem when coding in PHP.
I have a data in mysql like this:

$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("
", $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>';

Now I want to show it in a table like this:

Table that I want to show

How can I do it?

I think first you need to create the table header using one for each.Then for each row another for each. Also you need to do nested for each and explode(). Please have a look on the below scripts. This may help you.

<?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("
", $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>';
?>

Here's what i'd suggest you try... Just copy the entire Code below and run it to see what you get: Here's what you'd see: 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("
", $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;

From what I see you are doing in your code, it doesn't really seem like the Data you are Dicing & Slicing is coming Directly from the Database. It seems more like you are working with a Chunk of Data (probably copied from a mysql Command-Line or so). If You are accessing a Database directly, then this is not a Solution for this scenario at all....