简体   繁体   中英

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

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>

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.

Don't use extra variable names. Instead, use the original.

Also, don't output every row with PHP. Use plain HTML and add the variable in it later:

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

Or just echo as 1 line, no need for concatenation

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

or

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

Here's how I would write it:

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

Note that I removed the border="0" for the img tag - that should be done with CSS.

The short answer is yes. There is almost always a cleaner or more efficient way to do it.

How about something like this?

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

There are a lot of erroneous symbols etc in this, it comes with practice, but something like this might be worth trying

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

I'd recommend learning how to use the printf() family of functions.

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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