php - Get each array item from foreach loop as table data -
i'm trying foreach loop on array 20 names. should derive table 4 columns , 5 rows, each cell(table data) having unique name. code below snapshot of output table. hasn't worked yet. how can fix this?
<?php $names = array("patrick","raymond","george","hosea","samuel","alan","june","edwin","yvonne","john","paul","ruto","uhuru","raila","kalonzo","sonko","joho","wetangula","mudavadi","matiang'i"); echo "<table width='200' border='1' >"; foreach($names $name){ echo "<tr>"; for($cols=1;$cols<5;$cols++){ echo "<td>".$name."</td>"; } echo "<tr>"; } echo "<table>"; ?>
1st : remove for loop
2nd : apply limit using $i
note 1 : looping single name 5 times .that should not .
note 2 : more detail read comment lines .
<?php $names = array("patrick","raymond","george","hosea","samuel","alan","june","edwin","yvonne","john","paul","ruto","uhuru","raila","kalonzo","sonko","joho","wetangula","mudavadi","matiang'i"); echo "<table width='200' border='1' >"; $i=0; foreach($names $name){ if($i==0){ //open new tr if $i 0 echo "<tr>"; } echo "<td>".$name."</td>"; if($i==3){ //close tr if $i reached 3 . echo "</tr>"; $i=-1; //why setting -1 means i'm incrementing after set -1 } $i++; } echo "<table>"; ?> 
Comments
Post a Comment