繁体   English   中英

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

[英]Is there a better or more efficient way to do this?

这段代码可以满足我的需要,但是(我认为)它看起来很糟糕,所以我希望有人知道一种更干净或更有效的方法来做同样的事情。 我有几个从数据库中提取的条目,我希望它们的样式相同。 最终只有徽标和链接名称会更改,我将添加描述。 这是代码:

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

您可以通过切换到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;

请注意,此处缺少转义符,可以在标记属性周围使用正确的引号,并在嵌入式变量上使用{}表示法。

不要使用额外的变量名。 而是使用原件。

另外,不要用PHP输出每一行。 使用纯HTML并稍后在其中添加变量:

<div class="Links">
<a href="Profile.php?id="<?=$row['id']?>"><?=$row['name']?></a>

或仅回显为1行,无需级联

echo "<div class=\"linkImages\">";                        

要么

echo '<div class="linkImages">';                        
echo '<div class="content">';

while($row = $stmt->fetch()){
    $name = $row['name'];
    $id = $row['id'];
    $logo = $row['logo'];
    $username = $row['username'];
    echo '<div class="Links">
            <div class="linkImages">
                <br><a href="Profile.php?id='.$id .'"><img src="users/'.$username.'/images/'. $logo .'" width="200" height="auto" border="0"></a>
            </div>
            <div class="linkName">
                <a href=Profile.php?id='.$id .'">'.$name.'</a>
            </div>
        </div>';
}

echo '</div>';

这是我的写法:

<div class="content">
    <?php
    while ($row = $stmt->fetch()){
        echo '<div class="Links">';
        echo '<div class="linkImages">';
        echo '<br /><a href="Profile.php?id='. $row['id'] .'"><img src="users/'. $row['username'] .'/images/'. $row['logo'] .'" width="200" /></a>';
        echo '</div>';
        echo '<div class="linkName">';
        echo '<a href="Profile.php?id='. $row['id'] .'">'. $row['name'] .'</a>';
        echo '</div>';
    }
    ?>
</div>

请注意,我删除了img标签的border =“ 0”-应该使用CSS来完成。

简短的答案是肯定的。 几乎总是有一种更清洁或更有效的方法来执行此操作。

这样的事情怎么样?

        <div class="content">

        <?PHP while($row = $stmt->fetch()) { ?>

            <div class="Links">
                <div class="linkImages">
                    <br><a href="Profile.php?id="<?=$row['id'] ?>"><img src="users/<?=$row['username'] ?>/images/<?=$row['logo'] ?> width="200" height="auto" border="0" /></a>
                </div>
                <div class="linkName">
                <a href="Profile.php?id="<?=$row['id'] ?>><?=$row['name'] ?></a>
                </div>
            </div>

        <?PHP } ?>

    </div>

在实践中有很多错误的符号等,但是这种做法可能值得尝试

<?php
while($row = $stmt->fetch()) {
  $string = "";

  $string .= "<div class=\"Links\">\n";
  $string .= "<div class=\"linkImages\">\n";                        
  $string .= "<br />\n";
  $string .= "<a href=\"Profile.php?id=\"".$row['id'] . "><img src=\"users/". $row['name'] ."/images/" . $row['logo'] . "\" width=\"200\" height=\"auto\" border=\"0\" /></a>\n";
  $string .= "</div>\n";

  $string .= "<div class=\"linkName\">\n";
  $string .= "<a href=\"Profile.php?id=". $row['id'] .">". $row['name'] ."</a>\n";
  $string .= "</div>\n";
  $string .= "</div>\n";

  echo $string;
}
?>

我建议学习如何使用printf()系列函数。

$frame = '<a href="Profile.php?id=%s"><img src="users/%s/images/%s" width="200" height="auto" border="0"/></a>';
printf($frame, $id, $username, $logo);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM