PedroSoares Posted October 16, 2013 Share Posted October 16, 2013 Hello guys, I'm having trouble making a loop to display the data in a table, I need the loop to display a table with the following data: example: 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 My code: <?php echo '<table width="100%" border="1" cellspacing="0" cellpadding="0">'; $contador = 1; while ($contador <= 10) { if($contador % 2){ for($x=0;$x<2;$x++){ echo "<tr><td>Registro $contador</td>"; } }else{ echo "<td>Registro $contador</td></tr>"; } ++$contador; } echo '</table>'; ?> Help me! Link to comment https://forums.phpfreaks.com/topic/283032-how-do-loop-in-vertical-table/ Share on other sites More sharing options...
PedroSoares Posted October 16, 2013 Author Share Posted October 16, 2013 My loop has to repeat two lines and move to the next loop, the data of an array .. Link to comment https://forums.phpfreaks.com/topic/283032-how-do-loop-in-vertical-table/#findComment-1454200 Share on other sites More sharing options...
Psycho Posted October 17, 2013 Share Posted October 17, 2013 If each line is to be duplicated, just create the line one as a variable and output it twice. <?php $contadorHTML = ''; //Variable to hold the total output $contadorMax = 9; //Max value of records $contadorStep = 3; //Max value for each step, i.e. row for($contador=1; $contador<=$contadorMax; $contador+=$contadorStep) { //Create the output for a single step (row) $contadorLine = ''; for($step=0; $step<$contadorStep; $step++) { $value = $contador + $step; $contadorLine .= "<td>{$value}</td>\n"; } $contadorLine .= "<tr>\n"; //Add the content for the line to the output twice $contadorHTML .= $contadorLine . $contadorLine; } ?> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <?php echo $contadorHTML; ?> </table> Link to comment https://forums.phpfreaks.com/topic/283032-how-do-loop-in-vertical-table/#findComment-1454210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.