Home  >  Q&A  >  body text

Multiply image x times

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粉315680565P粉315680565355 days ago503

reply all(2)I'll reply

  • P粉151466081

    P粉1514660812023-11-23 19:36:21

    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.

    reply
    0
  • P粉009186469

    P粉0091864692023-11-23 10:18:58

    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.

    reply
    0
  • Cancelreply