使用foreach创建HTML表


Creating HTML table with foreach

我正在尝试用数据库中的结果构建表。

global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_prijavljeniupis ',OBJECT);

然后我试着用这种方式制作表格

echo '<table>';
    echo '<thead>';
    echo  '<tr>';
    echo '<th>UID<th>';
    echo '<th>ID<th>';
    echo '<th>Ime<th>';
    echo '<th>Prezime<th>';
    echo '<th>JMBG<th>';
    echo '<th>Telefon<th>';
    echo '<th>Adresa<th>';
    echo '<th>BrojUlice<th>';
    echo '<th>PttBroj<th>';
    echo '<th>Grad<th>';
    echo '<th>Drzava<th>';
    echo '<th>Grad<th>';
    echo '<th>Odsek<th>';
    echo '<th>NacinPlacanja<th>';
    echo '<th>NacinSkolovanja<th>';
    echo '<th>kampanja<th>';
    echo  '</tr>';
    echo '<thead>';
    echo '<tbody>';
    foreach ( $results as $result ) 
    {    
        echo '<td>'.$result->UID.'</td>';
        echo '<td>'.$result->ID.'</td>';
        echo '<td>'.$result->Ime.'</td>';
        echo '<td>'.$result->Prezime.'</td>';
        echo '<td>'.$result->JMBG.'</td>';
        echo '<td>'.$result->Telefon.'</td>';
        echo '<td>'.$result->Adresa.'</td>';
        echo '<td>'.$result->BrojUlice.'</td>';
        echo '<td>'.$result->PttBroj.'</td>';
        echo '<td>'.$result->Grad.'</td>';
        echo '<td>'.$result->Drzava.'</td>';
        echo '<td>'.$result->Odsek.'</td>';
        echo '<td>'.$result->NacinPlacanja.'</td>';
        echo '<td>'.$result->NacinSkolovanja.'</td>';
        echo '<td>'.$result->Kampanja.'</td>';

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

我不知道如何在我的tbody部分实现tr,因为我有循环,我尝试了不同的选项,但没有成功

您在循环开始时忘记了<tr>,在结束时忘了</tr>

echo '<table>';
echo '<thead>';
echo  '<tr>';
echo '<th>UID<th>';
echo '<th>ID<th>';
echo '<th>Ime<th>';
echo '<th>Prezime<th>';
echo '<th>JMBG<th>';
echo '<th>Telefon<th>';
echo '<th>Adresa<th>';
echo '<th>BrojUlice<th>';
echo '<th>PttBroj<th>';
echo '<th>Grad<th>';
echo '<th>Drzava<th>';
echo '<th>Grad<th>';
echo '<th>Odsek<th>';
echo '<th>NacinPlacanja<th>';
echo '<th>NacinSkolovanja<th>';
echo '<th>kampanja<th>';
echo  '</tr>';
echo '<thead>';
echo '<tbody>';

foreach ( $results as $result ) 
{    
    echo '<tr>';
    echo '<td>'.$result->UID.'</td>';
    echo '<td>'.$result->ID.'</td>';
    echo '<td>'.$result->Ime.'</td>';
    echo '<td>'.$result->Prezime.'</td>';
    echo '<td>'.$result->JMBG.'</td>';
    echo '<td>'.$result->Telefon.'</td>';
    echo '<td>'.$result->Adresa.'</td>';
    echo '<td>'.$result->BrojUlice.'</td>';
    echo '<td>'.$result->PttBroj.'</td>';
    echo '<td>'.$result->Grad.'</td>';
    echo '<td>'.$result->Drzava.'</td>';
    echo '<td>'.$result->Odsek.'</td>';
    echo '<td>'.$result->NacinPlacanja.'</td>';
    echo '<td>'.$result->NacinSkolovanja.'</td>';
    echo '<td>'.$result->Kampanja.'</td>';
    echo '</tr>';

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

您在表头中添加了额外的列echo"Grad";表头(thead)和表体(tbody)标签的列数应该相等,这样只有您的表才能很好地对齐。

试试这个代码

echo '<thead>';
echo  '<tr>';
echo '<th>UID</th>';
echo '<th>ID</th>';
echo '<th>Ime</th>';
echo '<th>Prezime</th>';
echo '<th>JMBG</th>';
echo '<th>Telefon</th>';
echo '<th>Adresa</th>';
echo '<th>BrojUlice</th>';
echo '<th>PttBroj</th>';
echo '<th>Grad</th>';
echo '<th>Drzava</th>';
echo '<th>Odsek</th>';
echo '<th>NacinPlacanja</th>';
echo '<th>NacinSkolovanja</th>';
echo '<th>kampanja</th>';
echo  '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ( $results as $result ) 
{    
    echo "<tr>";
    echo '<td>'.$result->UID.'</td>';
    echo '<td>'.$result->ID.'</td>';
    echo '<td>'.$result->Ime.'</td>';
    echo '<td>'.$result->Prezime.'</td>';
    echo '<td>'.$result->JMBG.'</td>';
    echo '<td>'.$result->Telefon.'</td>';
    echo '<td>'.$result->Adresa.'</td>';
    echo '<td>'.$result->BrojUlice.'</td>';
    echo '<td>'.$result->PttBroj.'</td>';
    echo '<td>'.$result->Grad.'</td>';
    echo '<td>'.$result->Drzava.'</td>';
    echo '<td>'.$result->Odsek.'</td>';
    echo '<td>'.$result->NacinPlacanja.'</td>';
    echo '<td>'.$result->NacinSkolovanja.'</td>';
    echo '<td>'.$result->Kampanja.'</td>';
    echo "</tr>"

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