For loop in a foreach?

I have

	foreach ($cb_result as $value) {
		echo '<image href="../images/circuit_breaker_icon.png" x="75" y="225"/>';
	}

which results in

<image href="../images/circuit_breaker_icon.png" x="75" y="225"/>
<image href="../images/circuit_breaker_icon.png" x="75" y="225"/>
<image href="../images/circuit_breaker_icon.png" x="75" y="225"/>
<image href="../images/circuit_breaker_icon.png" x="75" y="225"/>

The problem is that those images overlap each other and wanted to add 50 to each x value in the four images to avoid this like

<image href="../images/circuit_breaker_icon.png" x="75" y="225"/>
<image href="../images/circuit_breaker_icon.png" x="125" y="225"/>
<image href="../images/circuit_breaker_icon.png" x="175" y="225"/>
<image href="../images/circuit_breaker_icon.png" x="225" y="225"/>

Try this:

<?php declare(strict_types=1);
// notice how i've renamed $cb_results to a plural because there is more than one

//================================
  $cb_results = [
    'one', 
    'two', 
    'three', 
    'four', 
  ];
  $xxx = 75;
  foreach ($cb_results as $key => $value)
  {
    if( FALSE  ):
      echo $key .' ==> ' .$value;

    else:  
      $xxx += 50;
      $tmp = '<image href="../images/circuit_breaker_icon.png" '
             .  'width="' . $xxx .'"'
           .  ' height="225" >'
          ;
      echo htmlspecialchars($tmp);
    endif;  
    echo '<br>';
  }

Edit:

Added Output:

<image href="../images/circuit_breaker_icon.png" width="125" height="225" >
<image href="../images/circuit_breaker_icon.png" width="175" height="225" >
<image href="../images/circuit_breaker_icon.png" width="225" height="225" >
<image href="../images/circuit_breaker_icon.png" width="275" height="225" >

For context can you confirm? I’m guessing this is SVG, as one initially assumes HTML, but the tags and attributes seem wrong for that.

       $x = 75;
	foreach ($cb_result as $value) {
		echo '<image href="../images/circuit_breaker_icon.png" x="' . $x . '" y="225"/>';
                $x += 50;
	}

Though I’m also curious why you use the array, but not the values from it. Should all the images be the same, but with different positions?

A little off-topic, but I would also question the efficiency of using a PNG icon within an SVG, assuming it’s a graphic.

Just to throw an alternative in the mix:

$xs = range(75, 225, 50);
foreach ($cb_result as $value) {
    foreach ($xs as $x) {
        printf('<image href="../images/circuit_breaker_icon.png" x="%s" y="225"/>', $x);
    }
}

That is not how htmlspecialchars is used:

$tmp = '<image href=';
echo htmlspecialchars($tmp);

# results in
&lt;image href=

Not what you want.

In this case, since $xxx is an int there is no need to escape at all. But if you did have string data then you only escape the specific data.

$width = 75;
$alt = htmlspecialchars('Tom & Jerry');

$tmp = <<<EOT
<img alt="{$alt}" 
  href="../images/circuit_breaker_icon.png" 
  width="{$width}" height="225" >
EOT;
echo $tmp;

# results in
<img alt="Tom &amp; Jerry" 
  href="../images/circuit_breaker_icon.png" 
  width="75" height="225" >

The idea was to display the $tmp value to the screen so I could copy and paste the results in the example.

If htmlspecialchars(…) was not used then the $tmp value would be invisible.

Okay. You are working in a browser and did not want to use ctrl-u to look at the html source. Consider doing this sort of stuff from the command line. Eliminates quite a bit of refreshing.

1 Like

Yes, its for images inside a svg.
Lots of great ways to do this, thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.