I'm trying to formulate some dummy data so that I can developed the front end of a html data table, to do this im writing the data out once and using str_repeat
to out put it multiple times.
One of the features I'm incorporating in the front end is the ability to sort columns but to do that I need different data in each row. I planned to use rand()
to assist with this part, but when I use it with the code block below, it generates a random number and the str_repeat
outputs that 3 times..
Is there a simple way to get it to run the rand()
func on each iteration of the str_repeat()
func ?
$html = "
<tr>
<!-- Unit Price -->
<td class='table-input input-number'>
<input class='input-center' value='" . rand(10,200) . "'>
</td>
<!-- QTY -->
<td class='table-input input-number'>
<input class='input-center' value='" . rand(1,5) . "'>
</td>
</tr>";
echo str_repeat($html,3);
The approach i ended up using is this below based on @Christoph's answer, with this approach it lets you input the html and the rand()
values in a more straight forward fashion.
for($i = 0; $i < 3; $i++) {
echo "<tr>
<!-- Unit Price -->
<td class='table-input input-number'>
<input class='input-center'" . rand(1,5) . "'>
</td>
<!-- QTY -->
<td class='table-input input-number'>
<input class='input-center'" . rand(1,5) . "'>
</td>
</tr>";
}
No, you can't do it with str_repeat
, because you have a fixed string, which is repeated. Use e.g.
$tmpl = "
<tr>
<!-- Unit Price -->
<td class='table-input input-number'>
<input class='input-center' value='%d'>
</td>
<!-- QTY -->
<td class='table-input input-number'>
<input class='input-center' value='%d'>
</td>
</tr>";
for($i = 0; $i < 3; $i++) {
printf($tmpl, rand(10, 200), rand(1, 5));
}