On 4 August 2010 13:33, Carlton Whitehead <carlton.whiteh...@gmail.com> wrote:
> That is definitely calling out for a loop.
>
> When I scan down the lines of code, the only part I see changing is the x
> after $pic_x. You could make an array with all of the picture details and
> then replace all of that repeated code with a loop like so:
>
> $pics = array('my_first_picture.jpg', 'my_second_picture.jpg',
> 'etcetera.jpg');
> foreach ($pics as $pic)
> {
>     if (trim($pic) <> "") { echo "<div class='proddetailpics'><a href='#'
> class='color_thumb'> <img src='/imagedir/$pic' alt='$itemgroup
> $itemsubgroup' width='60' height='60' onclick=MM_swapImage('
> prodimage','','/imagedir/$pic',0) border='0' /></a></div>"; }
> }
>
> For the next step, you can just replace all those single quotes in your HTML
> with escaped double quotes like so:
>
> if (trim($pic) <> "") { echo "<div class=proddetailpics\"><a href=\"#\"
> class=\"color_thumb\"> <img src=\"/imagedir/$pic\" alt=\"$itemgroup
> $itemsubgroup\" width=\"60\" height=\"60\" onclick=MM_swapImage(\"
> prodimage\",\"\",\"/imagedir/$pic\",0) border=\"0\" /></a></div>"; }
>
> As others have pointed out, you might want to use single quotes for the PHP
> value being passed to echo. That would save you from having to escape the
> HTML double quotes, but then you can't include $variables inside of single
> quoted strings (you have to concatenate them separately).  Pick your poison.
>
> The PHP documentation on strings is pretty good, and it couldn't hurt to
> have a look: http://php.net/manual/en/language.types.string.php
>
> Carlton Whitehead

Maybe heredoc is an even easier option ...


if (trim($pic) <> "") {
 echo <<< END_DIV
<div class="proddetailpics">
  <a href="#" class="color_thumb">
    <img src="/imagedir/$pic" alt="$itemgroup $itemsubgroup"
width="60" height="60" onclick="MM_swapImage('prodimage',
'','/imagedir/$pic', 0);" border="0" />
  </a>
</div>
END_DIV;
}

No escaping of quotes, as long as you use " for html attributes and '
for javascript strings.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to