简体   繁体   中英

Nice HTML Formatting in PHP for-loop Output

I'm pretty sure I found no answer here or elsewhere or maybe because it's my fault not being able to ask the right question, but anyway, say I have a for-loop in my PHP that goes something like this:

for($i=0; $i<3; $i++){
echo "<a href=\"\">$i</a> | ";
}

How do I make the last call for " | " to not appear in order to have a nicely output:

<a href="">0</a> | <a href="">1</a> | <a href="">2</a>

INSTEAD OF

<a href="">0</a> | <a href="">1</a> | <a href="">2</a> | <-- remove this! :(

You guys are fabulous! Thanks!!!

What if $i < $undefinedNumber ?

Keep the flow, you only need that if it's not the first:

for ($i = 0; $i < 3; $i++) {
    echo $i ? " | " : '', "<a href=\"\">$i</a>";
}

And for the first $i is 0 which means it's false . Otherwise $i is a positive integer, which means true .

Sometimes finding an alternative way to describe the problem helps to formulate a simpler answer. Instead of:

<a href="">0</a> | <a href="">1</a> | <a href="">2</a> |  /* <-- remove this! :( */

It's that problem:

/* :( remove this! --> */ | <a href="">0</a> | <a href="">1</a> | <a href="">2</a>

The same form of solution works for CSS, too:

a:not(:first-child):before {content: ' | '}

sometimes that's easier to integrate than fiddling with PHP and HTML ( Example ) - however that adds the | to the link-text or style (at least in chrome where I tested it, looks buggy).

for($i=0; $i<3; $i++){
   echo "<a href=\"\">$i</a>"
   if ($i != 2) {
      echo " | ";
   }
}

or

echo "<a href=\"\">0</a>";
for($i=1; $i<3; $i++){
   echo " | <a href=\"\">$i</a>"
}
for($i=0; $i<3; $i++) {
    if ($i == 0) {
       echo "<a href=\"\">$i</a>;
    }
    else {
       echo " | <a href=\"\">$i</a>"
    }
}

So you just need to remove the last bar from the output? Why not skip it as you loop? Something like this:

for($i=0; $i<3; $i++)
{
    echo "<a href=\"\">$i</a>";

     if($i < 2)
         echo "|";
}

This would ensure that the bar only shows after the first two links, and not after the third.

$output=array();
for($i=0; $i<3; $i++){
    $output[]="<a href=\"\">$i</a>";
}
$ouput=implode(' | ', $ouput);
$arr = array();

for ($i=0; i<3; i++)
    $arr[] = "<a href=''>$i</a>";

echo implode(" | ",$arr);

More compact version:

echo implode(" | ", array_map(function($x){return "<a href=''>$x</a>";},range(1,3)));

You can also do this:

for($i=0; $i<3; $i++) {
    $arr[] = "<a href=\"\">$i</a>";
}
echo implode(' | ',$arr);

This is a commonly found situation in programming known as the Special Case pattern. There is no built in way of solving this other than using an if statement or the ternary operator

for($i=0; $i<3; $i++){
    echo "<a href=\"\">$i</a>" . ($i != 2) ? " | " : "";

Don't do it in the php, do it in the stylesheet.

First build your HTML

<ul class="navlinks">
     <?php foreach($links as $linkName => $linkUrl): ?>
     <li><a href="<?php echo $linkUrl; ?>"><?php echo $linkName; ?></a></li>
     <?php endforeach; ?>
</ul>

Apply the CSS.

.navlinks > li:after
{ 
    content:"<li>|</li>";
}

.navlinks > li:last-child:after
{ 
    content:"";
}

You should never style your HTML in php, this is the correct wayt to do it.

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