douyin7829 2012-11-19 19:03
浏览 40
已采纳

有没有更好或更有效的方法来做到这一点?

This code is working for what I need it to do, but (in my opinion) it looks bad so I am hoping someone knows of a cleaner or more efficient way to do the same thing. I have several entries being pulled from the database and I want them to be styled identically. Only the logo and the the link name will change eventually I will add a description. Here is the code:

<div class="content">

    <?PHP
        while($row = $stmt->fetch())
        {
            $name = $row['name'];
            $id = $row['id'];
            $logo = $row['logo'];
            $username = $row['username'];
            echo "<div class=" . "Links" . ">";

            echo "<div class=" . "linkImages" . ">";                        
            echo "<br>" . "<a href=" . "Profile.php?id=".$id . ">" . "<img src=" . "users/" . $username . "/images/" . $logo . " " . "width=" . "200" . " " . "height=" . "auto" . " " . "border=" . "0" . "/>" . "</a>";
            echo  "</div>";

            echo "<div class=" . "linkName" . ">";
            echo "<a href=" ."Profile.php?id=".$id .">" . $name ."</a>";
            echo "</div>";

            echo "</div>";

        }
    ?>

</div>
  • 写回答

7条回答 默认 最新

  • dongle7637 2012-11-19 19:09
    关注

    You can trivially remove most of the echoes and string concatenation by switching to a HEREDOC:

    while($row = $stmt->fetch()) {
       echo <<<EOL
    <div class="links">
    yadayada
    <br><a href="Profile.php?id={$row['id']}"><img src="users/{$row['username']}" etc....
    yada yada yada
    EOL;
    

    Note that the lack of escapes in there, allowing for proper quotes around the tag attributes, and the {} notation on the embedded variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端