此代码适用于我需要它做的事情,但(在我看来)它看起来很糟糕所以我希望有人知道更清洁或更有效的方法来做同样的事情。我从数据库中提取了几个条目,我希望它们的样式相同。只有徽标和链接名称最终会更改我将添加说明。这是代码:
<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>
答案 0 :(得分:4)
通过切换到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;
请注意,那里没有转义,允许在标记属性周围使用正确的引号,并在嵌入变量上使用{}
表示法。
答案 1 :(得分:1)
不要使用额外的变量名称。相反,使用原始。
另外,不要使用PHP输出每一行。使用纯HTML并稍后在其中添加变量:
<div class="Links">
<a href="Profile.php?id="<?=$row['id']?>"><?=$row['name']?></a>
或者只是回显1行,不需要连接
echo "<div class=\"linkImages\">";
或
echo '<div class="linkImages">';
答案 2 :(得分:1)
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>';
答案 3 :(得分:1)
以下是我的写作方式:
<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完成。
答案 4 :(得分:0)
简短的回答是肯定的。几乎总有一种更清洁或更有效的方法。
这样的事情怎么样?
<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>
答案 5 :(得分:0)
这里有很多错误的符号等,它来自练习,但这样的事情可能值得尝试
<?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;
}
?>
答案 6 :(得分:0)
我建议您学习如何使用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);