hi i would like to create nested divs dynamically, preferably without javascript
so if you have n divs, div1 contains div2 which contains div3 which etc ...
how could I code that in php ?
Tom
hi i would like to create nested divs dynamically, preferably without javascript
so if you have n divs, div1 contains div2 which contains div3 which etc ...
how could I code that in php ?
Tom
Here is a simple loop example using $n = 3. You can change $n to any number and it will nest div tags for you. I'm not entirely sure why you would want to do this, but here it is.
$openingTags = '';
$closingTags = '';
$n = 3;
for ($i = 0; $i < $n; ++$i) {
$openingTags .= '<div id="div' . $i . '">';
$closingTags .= '</div>';
}
echo $openingTags . 'Hello World' . $closingTags;
function recursiveDiv($num)
$html = '<div id="div'.$num.'">%s</div>';
for($i = $num - 1; $i >= 1; $i--) {
$html = '<div id="div'.$i.'">'.$html.'</div>';
}
return $html;
}
echo sprintf(recursiveDiv(5), 'Hello World');
Untested but should give you want you want.
This code should allow you to create the nested divs and also populate them with content. Replaced orginal code with below, this should work but its untested
<?php
$result = mysql_query("SELECT * FROM table");
$count = mysql_num_rows($result);
$html = '';
$end_html = '';
while($row = mysql_fetch_object($result)){
$html .= '<div id="div'.$count.'">'.$row->textfield; # any database column
$end_html .= '</div>';
$count--;
}
echo $html . $end_html;