在添加新用户时,苦苦挣扎(我正在学习PHP)来创建表行。我已经尝试重新安排我的代码,但仍然没有工作。
所以这是我的代码:
<?php
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
foreach ($committee as $user) {
echo '
<table>
<tr>
<th style="padding: 10px;">Job Title</th>
<th style="padding: 10px;">Members Name</th>
<th style="padding: 10px;">Email Address</th>
<th style="padding: 10px;">telephone Number</th>
</tr>';
echo '
<tr>
<td style="padding: 10px;">' .$user->job_title .'</td>
<td style="padding: 10px;">' . $user->display_name .'</td>
<td style="padding: 10px;">'.$user->user_email . '</td>
<td style="padding: 10px;">'.$user->tel_number . '</td>
</tr>
</table>';
}
echo '</ul>';
?>
我面临的问题是,当添加新用户时,它还会创建表头,但我只需要第二行?
答案 0 :(得分:0)
将你的第一个回声移出foreach:
<?php
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
echo '
<table>
<tr>
<th style="padding: 10px;">Job Title</th>
<th style="padding: 10px;">Members Name</th>
<th style="padding: 10px;">Email Address</th>
<th style="padding: 10px;">telephone Number</th>
</tr>';
foreach ($committee as $user) {
echo '
<tr>
<td style="padding: 10px;">' .$user->job_title .'</td>
<td style="padding: 10px;">' . $user->display_name .'</td>
<td style="padding: 10px;">'.$user->user_email . '</td>
<td style="padding: 10px;">'.$user->tel_number . '</td>
</tr>';
}
echo '</table></ul>';
?>
答案 1 :(得分:-1)
所以在循环之前回显<table>
。
echo "<table>";
foreach () {
echo "<tr>...</tr>";
}
echo "</table>";
答案 2 :(得分:-1)
将echo '<table....
从内部移到外部foreach
<?php
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
echo '
<table>
<tr>
<th style="padding: 10px;">Job Title</th>
<th style="padding: 10px;">Members Name</th>
<th style="padding: 10px;">Email Address</th>
<th style="padding: 10px;">telephone Number</th>
</tr>';
foreach ($committee as $user) {
echo '
<tr>
<td style="padding: 10px;">' .$user->job_title .'</td>
<td style="padding: 10px;">' . $user->display_name .'</td>
<td style="padding: 10px;">'.$user->user_email . '</td>
<td style="padding: 10px;">'.$user->tel_number . '</td>
</tr>
</table>';
}
echo '</ul>';
?>