开发者

Is there a way for php to make a file name match an id

here is my code

<div id="lara"><a href="http://anywhere.com/work/lara.jpg">some crap</a></div>

<div id="jerk"><a href="http://anywhere.com/work/jerk.jpg">some crap</a></div>

<div id="yessir"><a href="http://anywhere.com/work/yessir.jpg">some crap</a></div>

basically i'm wondering if there is a way to replace the filename (of the image) with some php that will dynamically add the id of the parent div in its place.

why you ask? i have several links like this where the id matches the filename, and i feel as though i should not have to manually type each file name in. I believe i've seen this done in ASP, but am not sure what the 开发者_如何学编程method is called, or if it's possible in php.

help!


I hope I understood you:

$arr = array("foo", "foo1", "foo2");
foreach($foo as $item){
  echo "<div id='$item'><a href='www.something.com/$item'>$item</a></div>";
}


The quickest way I could think if would be to make it into a function..

function printDiv($name) {
  echo "<div id='$name'><a href='http://anywhere.com/work/$name.jpg'>some crap</a></div>";
}

To call it, do the following:

printDiv('lara');

You might want to modify this to return a string instead of echoing depending on what you're doing..


The only way you can do this in PHP is if you have the ids in a variable in PHP.

<div id="<?=$name?>"><a href="http://anywhere.com/work/<?=$name?>.jpg">some crap</a>

Otherwise you will have to do it in Javascript, maybe with something like jQuery: http://jquery.com/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜