Reputation: 117
Example php like this
for ($i=1; $i<=6; $i++){
$i = str_pad($i, 2, "0", STR_PAD_LEFT);
$url = $i."<br />";
echo $url;
}
I want output use table :
<table>
<tr>
<td>
01<br>
02<br>
</td>
<td>
03<br>
04<br>
</td>
<td>
05<br>
06<br>
<td>
</tr>
</table>
Thanks for everybody who can help me :D
Upvotes: 0
Views: 225
Reputation: 12433
Try something like this. Using modulus %
, you can check if the value is even/odd, and open/close the table cell.
//open the table
echo '<table><tr>';
for ($i=1; $i<=6; $i++){
// if odd start cell
if($i % 2 != 0) echo '<td>';
$i = str_pad($i, 2, "0", STR_PAD_LEFT);
$url = $i."<br />";
echo $url;
//if even close the cell
if($i % 2 == 0) echo '</td>';
}
// Close the table
echo '</tr></table>';
edit
If you want the cell break to occur after 50, then you would can the modulus to 50
using $i % 50 == 1
and $i % 50 == 0
//open the table
echo "<table><tr>";
for ($i=1; $i<=100; $i++){
// if odd start cell
if($i % 50 == 1) echo "<td>";
$i = str_pad($i, 2, "0", STR_PAD_LEFT);
$url = $i."<br />";
echo $url;
//if even close the cell
if($i % 50 == 0) echo "</td>";
}
// Close the table
echo "</tr></table>";
Upvotes: 2
Reputation: 86525
If you know you'll always be outputting in pairs...
# i personally prefer sprintf over str_pad. But if you don't,
# you only have one place to have to change it.
$format = function($x) { return sprintf('%02d', $x); };
# write out each pair. Note the $i+=2.
for ($i=1; $i<=6; $i+=2) {
$first = $format($i);
$second = $format($i+1);
echo "<td>{$first}<br>{$second}<br></td>";
}
Upvotes: 0