Jump to content

[SOLVED] help needed to construct a loop..


DamienRoche

Recommended Posts

Hi guys. I am trying to construct a loop..not sure how simple this is though.

 

Here is what I want it do.

 

-store variables from a form ($var1, $var2 etc)

-then for each $var, produce a little bit of html

 

here's some laymen php I came up with:

 


count variables

foreach ($var) {
<div>$var</div> ////must be the unique var, not repeated.
}

 

so if there were two vars, the output would be:

 

<div>variable 1</div>

<div>variable 2</div>

 

I have no idea to achieve this with valid php. Can anyone help please?

Link to comment
https://forums.phpfreaks.com/topic/122820-solved-help-needed-to-construct-a-loop/
Share on other sites

You use a foreach loop to loop through arrays. I don't see an array there, so probably you WILL be better using a for, while, or do-while loop.

 

Although, you can just make an array there.

 

<?php

$var[0] = "Username";
$var[1] = "Password";
$var[3] = "Email";

//Start looping through each element of the array.

foreach ($var as $key => $value){

   echo "<div>".$value."</div>";

}

?>

 

That will print:

 

Username

Password

Email

Thank you very much!! That helped. I would prefer to use the while, but I think I'll stick with this for now. It works, and that's all that matters to me.

 

Thanks again,

 

Damien.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.