Jump to content
  • 0

php foreach, generate a new div id


Wise

Question


  • Group:  Members
  • Topic Count:  46
  • Topics Per Day:  0.01
  • Content Count:  147
  • Reputation:   26
  • Joined:  11/19/11
  • Last Seen:  

Can someone help me out with this?

<?php foreach($news as $nrow):?>

<div id="header" class="accordion_headings" ><?php echo $nrow->title ?></div>
<div id="content">
<p><?php echo $nrow->body ?></p>
</div>
<?php endforeach;?>

My question is how to generate a specific div id for each $nrow?

could be for the first $nrow, name it header_1 so that I can put div id="$nrow->id"

 

halp D:

Link to comment
Share on other sites

1 answer to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  12
  • Topics Per Day:  0.00
  • Content Count:  88
  • Reputation:   23
  • Joined:  01/30/12
  • Last Seen:  

It can be:

<?php
$index = 0;
foreach($news as $nrow) {
	echo "<div id=\"header_".$index."\" class=\"accordion_headings\">".$nrow->title."</div>";
		echo "<div id=\"content_".$index."\">";
		echo "<p>".$nrow->body."</p>";
	echo "</div>";
	$index++;
}
?>
The output will be header_0:content_0, header_1:content_1, .... until it reach the foreach size.
Link to comment
Share on other sites

×
×
  • Create New...