Call a php custom function that return a <div> inside another <div>

MOSTAGHASSI
MOSTAGHASSI used Ask the Experts™
on
Hello;
With these codes below i create 6 <div> that arrange in 2 columns and 3 rows,and then call custom function writeMsg() that return a div inside each of 6 <div>,but the output has problem,and return <div> doesn't appear inside each one of created div.

Thanks for your help.

php codes

  <?php
 function writeMsg() {
 $tab ="<div class='functiontest'>";
 echo "test";
 $tab .="</div>";
 return $tab;
 }
 $test = writeMsg();
  echo "<div class='wrapper'>";
   for( $i=0; $i < 6; $i++ ){
    echo "<div>" . $test . "</div>";
   }
  echo "</div>";
  ?>

Open in new window


css
 .functiontest{
    width:200px;
    height:200px;
    display:block;
    background-color:#F4F5BD;
    float:left;
   }
   .wrapper{
    background-color:#F7ABAC;
    display:block;
    width:1000px;
    min-height:1000px;
    float:none;
    clear:both;
    box-sizing:border-box;
    }
   .wrapper div{
    width:330px;
    height:300px;
    border:1px solid black;
    float:left;
    margin-left:7px;
    margin-bottom:10px;
    display: block;
    background:#cccccc;
    text-align: center;
    }
   .wrapper div:nth-child(2n+1){
    clear:left;
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
I presume the "echo test" is in the wrong place... and it should be like this
this should work "better", and provide you the divs with the test X inside

  <?php
 function writeMsg( $msg ) {
 $tab ="<div class='functiontest'>";
 $tab .= $msg;
 $tab .="</div>";
 return $tab;
 }

  echo "<div class='wrapper'>";
   for( $i=0; $i < 6; $i++ ){
    echo "<div>" . writeMsg("test " . $i) . "</div>";
   }
  echo "</div>";
  ?>

Open in new window

Olaf DoschkeSoftware Developer

Commented:
You're doing a double nesting here. writeMsg() crerates a div and then you wrap that inside echo "<div>" . $test . "</div>";

Your class='functiontest' dis become children of children of the wrapper div.

Simply look at the HTML output. What you have done is:

<div class='wrapper'>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
</div>

Open in new window


Did you include the css?
To me this shows up (zoomed out, shrinked view):

Divs
You get the table layout, but all the inner divs are double, because they are two nested divs each.

Only do:
   for( $i=0; $i < 6; $i++ ){
    echo $test;
   }

Open in new window


Bye, Olaf.

Author

Commented:
Thanks,no the problem still exist,you can delete the echo test the <div class='functiontest'> does not appear inside the each one of 6 divs.
Olaf DoschkeSoftware Developer

Commented:
Yes it does, but your CSS .wrapper div{...background:#cccccc;...} overrides the styling of the functiontest class, most prominently it makes the divs grey. The .wrapper div styling is applied to any div inside a wrapper div.

Bye, Olaf.

Author

Commented:
Ok, but how can solve it?
Software Developer
Commented:
For example simpler, straight forward:
<!doctype html>
<head>
<style>
   .functiontest{
    width:200px;
    height:200px;
    display:block;
    background-color:#F4F5BD;
    float:left;
   }
   .wrapper{
    background-color:#F7ABAC;
    display:block;
    width:1000px;
    min-height:1000px;
    float:none;
    clear:both;
    box-sizing:border-box;
    }
   .wrapper2{
    width:330px;
    height:300px;
    border:1px solid black;
    float:left;
    margin-left:7px;
    margin-bottom:10px;
    display: block;
    background:#cccccc;
    text-align: center;
    }
   .wrapper div:nth-child(2n+1){
    clear:left;
    }
</style>	
</head>
<body>  
<div class='wrapper'>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
</div>
</body>
</html>

Open in new window


Bye, Olaf.

Author

Commented:
No,by you codes,i lose the 6divs.
Olaf DoschkeSoftware Developer

Commented:
To produce this:
<div class='wrapper'>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
<div class='wrapper2'><div class='functiontest'>test</div></div>
</div>

Open in new window


you obviously need this:

 for( $i=0; $i < 6; $i++ ){
    echo "<div class='wrapper2'>" . $test . "</div>";
   }

Open in new window


Yes, I first removed the divs, as I thought they were unneccessary.

Bye, Olaf.

Author

Commented:
If is possible that arrange the return div from function and arrange in instead of 6divs gary inside the .wrapper it is ok also.
Olaf DoschkeSoftware Developer

Commented:
Sorry, can you rephrase that, please? I don't understand what you mean.

Bye, Olaf.

Author

Commented:
My mean is this that suppose i have .wrapper how can call the function in this wrapper and arrange the output of fuction(which is a yellow div) inside the .wrapper 6 times in 2 columns and 3 rows and delete the 6div gray ?
Olaf DoschkeSoftware Developer

Commented:
call a function inside the wrapper? I don't see how a html tag calls a function.

Could you just show the html you want?

Bye, Olaf.

Author

Commented:
<div class='wrapper'>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
</div>

Open in new window


in 2 columns and 3rows
Olaf DoschkeSoftware Developer

Commented:
We had that and just in gray instead of yellow, though you complained about that solution. Then simply remove the .wrapper div part:

<!doctype html>
<head>
<style>
   .functiontest{
    width:200px;
    height:200px;
    display:block;
    background-color:#F4F5BD;
    float:left;
   }
   .wrapper{
    background-color:#F7ABAC;
    display:block;
    width:1000px;
    min-height:1000px;
    float:none;
    clear:both;
    box-sizing:border-box;
    }
   .wrapper div:nth-child(2n+1){
    clear:left;
    }
</style>	
</head>
<body>  
<div class='wrapper'>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
</div>
</body>
</html>

Open in new window


CSS:
.functiontest{
    width:200px;
    height:200px;
    display:block;
    background-color:#F4F5BD;
    float:left;
   }
   .wrapper{
    background-color:#F7ABAC;
    display:block;
    width:1000px;
    min-height:1000px;
    float:none;
    clear:both;
    box-sizing:border-box;
    }
   .wrapper div:nth-child(2n+1){
    clear:left;
    }

Open in new window


PHP (as already given):
<?php
 function writeMsg() {
 $tab ="<div class='functiontest'>";
 echo "test";
 $tab .="</div>";
 return $tab;
 }
 $test = writeMsg();
  echo "<div class='wrapper'>";
   for( $i=0; $i < 6; $i++ ){
    echo  $test; // <--no additional div here
   }
  echo "</div>";
  ?>

Open in new window


Bye, Olaf.

Author

Commented:
Olaf,these version does not create 6divs,it just show a big yellow div inside the wrapper,but your above codes that contain wrapper2 and was regarding the question work well.
Olaf DoschkeSoftware Developer

Commented:
It is just looking like one big div, You can style this further with either border, margin, and/or padding to see the 6 divs. That it's 6 and not just one div is starting at you already, because it shows 6x "test" and it has the size of 2x3 divs.

Are you really Ok? You lack a lot of HTML and CSS knowledge it seems.

Bye, Olaf.

Author

Commented:
i think your early css codes must be:

css

.functiontest{
    width:200px;
    height:200px;
    display:block;
    background-color:#F4F5BD;
    float:left;
   }
   .wrapper{
    background-color:#F7ABAC;
    display:block;
    width:1000px;
    min-height:1000px;
    float:none;
    clear:both;
    box-sizing:border-box;
    }
   .wrapper div:nth-child(2n+1){
    clear:left;
    }

Open in new window


this bring the 6 yellow divs but its arrangement is not ok,i think the php codes also must modify.
Olaf DoschkeSoftware Developer

Commented:
I made the yellow work as per your specification, I removed the necessity of the grey divs, also per you demand. What now?

Bye, Olaf.
Olaf DoschkeSoftware Developer

Commented:
>its arrangement is not ok

As I already said, work with the styling inside the functiontest CSS class, add padding or margin or border to separate these 6 divs.

See http://www.w3schools.com/css/css_boxmodel.asp on how margin, border and padding are cvontributing to the box model of HTML, this is very basic and fundamental HTML knowledge you should already have, time to learn that.

Bye, Olaf.

Author

Commented:
Thanks a lot for your help,i got my answer.

Author

Commented:
Appreciate for your help.

Author

Commented:
@Olaf,before your comment :
Are you really Ok? You lack a lot of HTML and CSS knowledge it seems.

and just above it i had accepted your answer,yes i'm ok,and my knowledge is enough that i set forth a question and consider the right answer,you know that on EE there is not obligated for a person that answer a question so if you think that my question are false,you can don't participate.

In addition your suggestion is not a good solution (while it works as my request in question)because you changed the php codes and basic my css,but i accepted because the output of your codes was the things that i wanted.

The best solution that i could find is:add a small codes above the css without changing the php codes,as below:
.wrapper div.functiontest{
    width:200px;
    height:200px;
    display:block;
    background-color:#F4F5BD;
    float:left;
   }

Open in new window


Thanks
Olaf DoschkeSoftware Developer

Commented:
I have to assume you are able to change all you post, not only the CSS. You can only expect a solution fulfilling all your needs and limitations, if you also specify them, as simple as that. How should anyone know you can't change the PHP? It could also be vice versa, if the CSS is as given and not under your control, so what assumptions should one make before answering?

And on topic of the HTML: It seems to me your intermediate classless divs are just there to layout the whole HTML and that could also be done using margin and or padding of the functiontest divs. Besides that, you also asked for how it could work without these divs as you specified the HTML outcome you wished to have in your post ID: 41766441

Bye, Olaf.

To put it together for striaght comparison:

1. Your original and still same PHP code outputs this:
<div class='wrapper'>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
<div><div class='functiontest'>test</div></div>
</div>

Open in new window


In your post D: 41766441 you  said you wished HTML like this:
<div class='wrapper'>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
<div class='functiontest'>test</div>
</div>

Open in new window


There is an additional layer on DIVs you removed here, as I also did by changing the PHP.

So from my point of view you twice changed your mind of what HTML you want to look like a 2x3 grid in the course of this thread. That was causing some annoyance to me, as it's hard to hit a moving target. Besides that, there are lots of ways leading to rome, lots of solutions, fine you found one.

Bye, Olaf.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial