在php循环中显示两列html表 [英] Displaying two column html table while php loop

查看:82
本文介绍了在php循环中显示两列html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可以以表格形式显示新闻的时事通讯模板,但是我希望新闻每行显示在两列中。
请检查这个网址,看看我在说什么 http://www.ipaidabribenaija.com/ newsletter.php
Thanks。

Am creating a newsletter template that can display news in tabular form, but i want the news to be displayed in two column per row. Please check this url to see what i am talking about http://www.ipaidabribenaija.com/newsletter.php Thanks.

    <?php
    $conn = mysql_connect("localhost", "rppt", "peep") or die(mysql_error());

    mysql_select_db('news', $conn) or die(mysql_error());

    $query = mysql_query ("Select i.nid, LEFT(i.fulltext, 350), UPPER(i.title), 
LOWER(c.name) from nl i JOIN jos_k2_categories c ON c.id=i.catid ORDER BY i.id LIMIT 0, 16") or die ('Error');

    $href = "http://www.ipaidabribenaija.com/index.php";

?>



<table width="500px" align="center">
    <tbody>
        <tr>
            <td width="400px" height="344px" valign="top">
<table cellspacing="5">
        <tbody>


         <?php
            while(list($id, $fulltext, $title, $name)=mysql_fetch_array($query))
            {
                $i = 0;
         ?>
         <?php
            $replacename = eregi_replace(" ",  "-", $name);
         ?>
        <tr>
            <td height="34">&nbsp;</td>
        </tr>
        <?php
            if($i%2 == 0)
            {
        ?>
        <tr>
            <tr>
                <td height="34">
<font color="#FF0000" size="+2"><strong><?php echo $title; ?></strong></font>&nbsp;</td>
            </tr>
            <tr>
                <td height="34"><p><?php echo $fulltext; ?>...</p>

                    <p><a href="<?php echo $href ."/". $replacename ."/item/". $id; ?>">read more...</a></p>
                </td>
            </tr>
            <?php
                }
                else{
            ?>
                <tr>
                    <td height="34"><font color="#FF0000" size="+2"><strong><?php echo $title; ?></strong></font>&nbsp;</td>
                </tr>
                <tr>
                    <td height="34"><p><?php echo $fulltext; ?>...</p>
                        <p><a href="<?php echo $href ."/". $replacename ."/item/". $id; ?>">read more...</a></p></td>
                </tr>
                <?php
                    }
                        }
                ?>
        </tr>
        </tbody>
     </table>
    </td>
    </tr>
    </tbody>
</table>


推荐答案

这将为您带来大部分途径:

This will get you most of the way there:

echo '<tr>';
$i = 0;
while(...) {
    if($i > 0 and $i % 2 == 0) {
        echo '</tr><tr>';
    }
    echo '<td>My data</td>';
    $i++;
}
echo '</tr>;

这篇关于在php循环中显示两列html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆