echo '<tr>';
if((strpos($tr_code,'B')!== false)){ //print out anything contain "B"
echo "<td> $tr_code <br /> </td>";
echo "<td> $tr_name <br /> </td>";
}
else
echo "<td> $tr_code <br /> </td>"; //print out anything contain other than "B"
echo "<td> $tr_name <br /> </td>";
echo '</tr>' ;
如何在两列中打印它们?我希望左栏中的单词包含“B”,右栏中的单词包含“K”。
答案 0 :(得分:1)
我假设单词是在数组或嵌套数组中。我有这个建议:
<?php
$words = array("wordb 1", "wordb 2", "wordk 1", "wordk 2", "wordb 3", "wordk 3", "wordk 4", "wordk 5");
$wordsb = '';
$wordsk = '';
foreach($words as $word) {
if(strpos($word,'b')!== false) {
$wordsb[] = $word;
}
if(strpos($word,'k')!== false) {
$wordsk[] = $word;
}
}
$length = (count($wordsb) > count($wordsk)) ? count($wordsb) : count($wordsk);
?>
<table border="1" cellpadding="5">
<thead>
<tr>
<th>Words With b On Left</th>
<th>Words With k On Right</th>
</tr>
</thead>
<tbody>
<?php for($i = 0; $i < $length; $i++) { ?>
<tr>
<td><?php echo (isset($wordsb[$i]) && !empty($wordsb[$i])) ? $wordsb[$i] : " " ; ?></td>
<td><?php echo (isset($wordsk[$i]) && !empty($wordsk[$i])) ? $wordsk[$i] : " " ; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
答案 1 :(得分:0)
试试这个解决方案:
(你的桌子必须有4列)
echo '<tr>';
if ((strpos($tr_code, 'B') !== false)) { //print out anything contain "B"
echo "<td>{$tr_code}</td>";
echo "<td>{$tr_name}</td>";
echo "<td></td>";
echo "<td></td>";
} else { //print out anything contain other than "B"
echo "<td></td>";
echo "<td></td>";
echo "<td>{$tr_code}</td>";
echo "<td>{$tr_name}</td>";
}
echo '</tr>';
答案 2 :(得分:0)
如果要使用嵌套表,请尝试以下方法:
if((strpos($tr_code,'B')!== false)){ //print out anything contain "B"
echo '<tr><td>';
echo "<table><tr><td> $tr_code <br /> </td>";
echo "<td> $tr_name <br /> </td></tr></table>";
echo '</td><td></td></tr>';
}
else {
echo '<tr><td></td><td>';
echo "<table><tr><td> $tr_code <br /> </td>"; //print out anything contain other than "B"
echo "<td> $tr_name <br /> </td></tr></table>";
}