DamienRoche Posted September 5, 2008 Share Posted September 5, 2008 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 More sharing options...
Vermillion Posted September 5, 2008 Share Posted September 5, 2008 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 Link to comment https://forums.phpfreaks.com/topic/122820-solved-help-needed-to-construct-a-loop/#findComment-634279 Share on other sites More sharing options...
DamienRoche Posted September 5, 2008 Author Share Posted September 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/122820-solved-help-needed-to-construct-a-loop/#findComment-634284 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.