如何在PHP表中显示查询

时间:2013-03-29 22:31:16

标签: php html-table

我的代码

    <?php
    include('ConnectToDb.php');

    $query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
    $arrCount = -1;
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $ID=$row['ID'];
        $PostDate = $row['PostDate'];
        $NewsHeader = stripslashes($row['NewsHeader'])
;
        $NewsStart = stripslashes($row['NewsStart'])
;
            echo "<hr>";
            echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
            echo "<p>";
                $news_id = strval(sprintf("%1$04d",$ID));
                $array = scanImageFolder("newsImages/newsThumbs",$news_id);
                if(count($array)>0) {
                    echo "<img src='". $array[0]. "' alt='' />";
                }
            echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
            echo "<div style='text-align:left'><h3>";
            echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
            echo "<div style='text-align:center'>";
            echo "</div>";
            echo "</h3></div>";
    }
?>

很好地在四行上显示我的数据,日期在顶部,然后是图片,标题和描述。

但是,我想将数据显示为像这样的表

    <table style="width: 100%">
    <tr>
        <td colspan="2">postDate here</td>
    </tr>
    <tr>
        <td rowspan="2">picture here</td>
        <td>newsHeader here</td>
    </tr>
    <tr>
        <td>newsStart here</td>
    </tr>
</table>

我不确定如何正确回显表格单元格,到目前为止我的所有尝试都产生了白页。有人可以赐教我吗?

1 个答案:

答案 0 :(得分:1)

我建议你将你的逻辑与你的可呈现部分分开。准备好提取结果,分配var等后关闭PHP标记,然后:

<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>


 <tr>
    <td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
 </tr>

 <tr>
   <?php
 if(count($array)>0) {
                ?>
    <td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
 <?php } ?>
    <td><?= $NewsHeader ?></td>
 </tr>
 <tr>
    <td><?= $NewsStart ?> </td>
 </tr>

<?php } ?>
</table>

然而,它再次不那么清楚,我建议使用模板引擎。如果你愿意我可以发布一个代码,将代码分配给Smarty,并在Smarty模板中输出你的演示文稿