Jump to content

Question is this possible


AE117

Recommended Posts

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.

Link to comment
Share on other sites

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>";
}

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.