AE117 Posted December 9, 2010 Share Posted December 9, 2010 Im not new to php but I am trying to think if this possible. I want to write a php script that creates a div inside a div until lets say 10 divs are created. I have been trying do while statements but them seem to place them underneth each other not within. I want <div id=one"> <div id=two"> <div id=three"> </div> </div> </div> Only thing I can create is <div id=one"></div> <div id=two"></div> <div id=three"></div> Like I said I dont even know if this is possible or if I am just retarded and can't think of a way to do it but it would be nice to see what others have to say about it. Quote Link to comment https://forums.phpfreaks.com/topic/221097-question-is-this-possible/ Share on other sites More sharing options...
devilinc Posted December 9, 2010 Share Posted December 9, 2010 try this:: <?php echo "here is ur result"; for($i=1;$i<=10;$i++){ switch($i){ case 1: echo "<div id = one>"; break; case 2: echo "<div id = two>"; break; case 3: echo "<div id = three>"; break; case 4: echo "<div id = four>"; break; case 5: echo "<div id = five>"; break; case 6: echo "<div id = six>"; break; case 7: echo "<div id = seven>"; break; case 8: echo "<div id = eight>"; break; case 9: echo "<div id = nine>"; break; case 10: echo "<div id = ten>"; break; } } for($i=1;$i<10;$i++){ echo "</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/221097-question-is-this-possible/#findComment-1144884 Share on other sites More sharing options...
Anti-Moronic Posted December 9, 2010 Share Posted December 9, 2010 A simple function like this would work: function getNestedDivs($num){ $html = ''; for($n = 1; $n <= $num; $n++){ $html .= '<div id="div'.$n.'">'; } for($n = 1; $n <= $num; $n++){ $html .= '</div>'; } return $html; } //usage: echo getNestedDivs(10); //returns: <div id="div1"><div id="div2"><div id="div3"><div id="div4"><div id="div5"><div id="div6"><div id="div7"><div id="div8"><div id="div9"><div id="div10"></div></div></div></div></div></div></div></div></div></div> You could even improve and add whitespace or tabs to account for how nested a div is and also options to configure the id, class, styles etc hope that helps. ..and to explain why you might have had trouble. You were probably thinking of a way to do this with a single loop, but that is far too complicated. You probably could by using multiple arrays and inserting one array within another, then converting to string..in fact, I'd be interested to see if someone can produce an elegant solution to that. Anyway, the simple way is to create 2 loops. One for producing the open tags, and one for producing the close tags. Quote Link to comment https://forums.phpfreaks.com/topic/221097-question-is-this-possible/#findComment-1144904 Share on other sites More sharing options...
AE117 Posted December 13, 2010 Author Share Posted December 13, 2010 Thanks that last functions worked great, i hadnt even thought about doing it that way. Quote Link to comment https://forums.phpfreaks.com/topic/221097-question-is-this-possible/#findComment-1146727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.