Multiply image x times
P粉315680565
P粉315680565 2023-11-22 23:08:40
0
2
495

Hello, I'm developing a website using expedias api. There are basically a lot of people in every room, and I wanted to echo the image of a little man for everyone. So for example, if I have 5 occupancies and I need to echo 5 tags with the little one as src. Any idea how to do this?

P粉315680565
P粉315680565

reply all(2)
P粉151466081

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;

Revisit this answer, a more efficient solution:

Assume the image is 12 pixels wide and 16 pixels tall - adjust to 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 generate a single element appropriately sized to display a $repeat copy of the image side by side.

P粉009186469

Suppose you store the number of people in a variable.

$occupancy = 5;

You can then insert that number into a for loop and have the program loop that number of times.

for($n = 0; $n < $occupancy; $n++) {
  // Disco
}

You can read more about control structures here.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!