Hello I am working on a site that uses expedias api. Basically I get a number of people per room, and I want to echo out a little man image for each person. So if I have occupancy of 5 for example and I need to echo 5 tags with the little man as a src. Any idea how to do this??
Well let's say you have the amount of people stored in a variable.
$occupancy = 5;
You can then plug in that number into a for loop
, and have the program cycle through that many times.
for($n = 0; $n < $occupancy; $n++) {
// Disco
}
You can read more about control structures here.
You should be interested in str_repeat()
.
Something like this should work:
$img_multi = str_repeat('<img src="man.png" alt="man"/>', $repeat);
echo $img_multi;
Revisiting this answer, a much more efficient solution:
Assuming the image is 12px wide by 16px high - adjust for your needs.
$width = 12 * $repeat;
$height = 16;
echo '<span style="'
.'display: inline-block;'
.'width: '.$width.'px;'
.'height: '.$height.'px;'
.'background-image: url(man.png);'
.'"></span>';
This will produce a single element of the appropriate size to show $repeat
copies of the image, side-by-side.
First result googling "php loops" Might be worth trying.