[英]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.