开发者

How to increase 1 for each image with php

I have the following array output.

Array
(
    [0] => Training centers
    [22] => Training center ABC
    [16] => Training center CCF
    [17] => Training center LLI
    [23] => Training center BBC

 ...
)

Using this I want to echo out like this.

<ul>
<li><a href="center/22"><img src="center1.jpg" alt="Training center ABC" /></a></li>
<li><a href="center/16"><img src="center2.jpg" alt="Training center CCF" /></a></li>
...

And so on. I am using foreach($centers as $key=>$center), but not sure how to increase the image number. I already have these images ready. But the number of center won't be known.

I tried the following but it does not show what I want.

echo "<ul>\n";
foreach($centres as $key=>$centre){
    echo '<li><a href="mycentre/admin/manage_mycentre/'.$key.'">';
    for ($i = 1; $i <开发者_JAVA技巧= 10; $i++) {

        echo "<img src=\"assets/images/centre/centre$i.jpg\" />";
    }
    echo "</a></li>\n";
}
echo "</ul>\n";

Thanks in advance.


It seems a little fragile to be depending on the array order to determine which image to load, but if you're confident that this is safe, you can do away with the inner loop and use just the one foreach loop to also update a counter:

echo "<ul>\n";
$i = 1;
foreach($centres as $key=>$centre){
    echo '<li><a href="mycentre/admin/manage_mycentre/'.$key.'">';
    echo "<img src=\"assets/images/centre/centre$i.jpg\" alt=\"$centre\"/>";
    echo "</a></li>\n";
    $i++;
}
echo "</ul>\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜