【问题标题】:Displaying two column html table while php loop [closed]在php循环时显示两列html表[关闭]
【发布时间】:2012-06-05 17:54:22
【问题描述】:

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

    <?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>

【问题讨论】:

  • 是什么阻碍了你?过于本地化
  • 1) 停止使用表格,它们用于数据,而不是布局。 2)停止使用FONT语句并学习CSS。
  • &lt;font&gt; 标签?在2012年? 90 年代的人打电话并希望他们的 HTML 回来。
  • 我知道我使用的是字体标签,它将被更正为 css

标签: php mysql html loops


【解决方案1】:

这将使您大部分时间到达那里:

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

【讨论】:

  • 我不明白你的问题。 $i 将仅等于第一个单元格。之后它会继续变大。
  • 我尝试了代码,但还是一样
【解决方案2】:

使用rowspan和colspan html属性

 <tr>
  <td>A</td>
  <td colspan="2">&nbsp;</td>
 </tr>

【讨论】:

  • 这不是解决方案
【解决方案3】:

完成查询后,您可以这样写:

<table>
<?php
$rows = 1;
$while($news = @mysql_fetch_array($query)){
  if($rows % 2 != 0){
    echo "<tr><td>" . $news['details'] . "</td>";
  }
  else {
   echo "<td>" . $news['details'] . "</td></tr>";
  }
  $row++;
}

if($rows % 2 != 0){echo "<td>&nbsp;</td></tr>"; }
?>
</table>

使用此代码,您可以创建一个表并使用 1 初始化一个变量,仅用于检查行数。

如果您有 3 条新闻,脚本将完成该行和一个空单元格。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-12-02
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 2017-07-27
  • 1970-01-01
相关资源
最近更新 更多