-1
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”。

4

3 回答 3

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] : "&nbsp;" ; ?></td>
                <td><?php echo (isset($wordsk[$i]) && !empty($wordsk[$i])) ? $wordsk[$i] : "&nbsp;" ; ?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>
于 2013-06-07T09:22:26.887 回答
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>';
于 2013-06-07T08:15:12.143 回答
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>";
    }
于 2013-06-07T08:18:24.830 回答