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

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

MOSTAGHASSIAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 DeveloperCommented:
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.
MOSTAGHASSIAuthor 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.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Olaf DoschkeSoftware DeveloperCommented:
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.
MOSTAGHASSIAuthor Commented:
Ok, but how can solve it?
Olaf DoschkeSoftware DeveloperCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MOSTAGHASSIAuthor Commented:
No,by you codes,i lose the 6divs.
Olaf DoschkeSoftware DeveloperCommented:
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.
MOSTAGHASSIAuthor 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 DeveloperCommented:
Sorry, can you rephrase that, please? I don't understand what you mean.

Bye, Olaf.
MOSTAGHASSIAuthor 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 DeveloperCommented:
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.
MOSTAGHASSIAuthor 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 DeveloperCommented:
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.
MOSTAGHASSIAuthor 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 DeveloperCommented:
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.
MOSTAGHASSIAuthor 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 DeveloperCommented:
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 DeveloperCommented:
>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.
MOSTAGHASSIAuthor Commented:
Thanks a lot for your help,i got my answer.
MOSTAGHASSIAuthor Commented:
Appreciate for your help.
MOSTAGHASSIAuthor 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 DeveloperCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.