doukenqiong0588 2013-03-29 22:31
浏览 42
已采纳

如何在PHP表中显示查询

My code

    <?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>";
    }
?>

displays my data nicely on four lines with date at the top, then a picture, title and then description.

However, I want to display the data as a table like this

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

I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?

  • 写回答

1条回答 默认 最新

  • douque9815 2013-03-29 22:51
    关注

    I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:

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

    However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面