From: Rick Dwyer on

On Aug 3, 2010, at 3:36 PM, Ashley Sheridan wrote:

> On Tue, 2010-08-03 at 15:32 -0400, Rick Dwyer wrote:
>>
>> On Aug 3, 2010, at 3:15 PM, Sebastian Ewert wrote:
>>
>> > Ashley Sheridan wrote:
>> >> On Tue, 2010-08-03 at 15:00 -0400, Rick Dwyer wrote:
>> >>
>> >>> On Aug 3, 2010, at 2:47 PM, Sebastian Ewert wrote:
>> >>>
>> >>>> Rick Dwyer wrote:
>> >>>>> Hello List.
>> >>>>>
>> >>>>> In the Alt section of the IMG tag below, the variable $myitem has a value of "Who's There".
>> >>>>>
>> >>>>> echo "<div class='myclass'><a href='#' class='color_thumb'> <img src='/itemimages/$mypic' alt='$myitem' width='60' ....
>> >>>>>
>> >>>>> When running through W3C validator, the line errors out because of the " ' " in "Who's".
>> >>>>> I tried:
>> >>>>> $myitem=(htmlentities($myitem));
>> >>>>>
>> >>>>> But this has no affect on "Who's".
>> >>>>>
>> >>>>> What's the best way to code this portion so the apostrophe is handled correctly?
>> >>>>>
>> >>>>>
>> >>>>> TIA,
>> >>>>>
>> >>>>> --Rick
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>> Use it
>> >>>>
>> >>>>
>> >>>> echo '<div class="myclass"><a href="#" class="color_thumb"> <img
>> >>>> src="/itemimages/'.$mypic.'" alt="'.$myitem.'" width="60" ....'
>> >>>
>> >>> Thanks Sebastian.
>> >>>
>> >>> In the above, what is the function of the period in front of $myitem?
>> >>>
>> >>> --Rick
>> >>>
>> >>>
>> >>
>> >>
>> >> It is a string concatenation in PHP. But, as my last email on this
>> >> thread shows, you only need to add ENT_QUOTES to your htmlentities()
>> >> call and everything will work.
>> >>
>> >> Thanks,
>> >> Ash
>> >> http://www.ashleysheridan.co.uk
>> >>
>> >>
>> >>
>> >
>> >
>> > If you use single quotes you can't use variables inside the string. You
>> > have to combine strings and variables or two strings with a dot.
>> >
>> > echo 'foo'.$bar;
>> >
>> > http://www.php.net/manual/de/language.types.string.php
>> >
>> > I'm not shure but I think to validate xhtml you need the double quotes.
>>
>> Problem I'm having is I've inherited a PHP page that contains sections of PHP/Javascript/HTML/CSS all inside of a PHP echo tag. My PHP and JS skill are rudimentary at best so when it comes to a block of code 40 to 50 lines in length, it becomes daunting to reverse the ' with ". Each echo block starts with a " and the html inside uses a '. JS also uses '.
>>
>> Below is an actual block in more detail with JS in it. Is it still recommended to switch with " with ' and ' with "?
>>
>> --Rick
>>
>>
>> echo "
>> <p>Click on a picture to view that color:</p>";
>> If (trim($pic_1) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_1' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_1',0) border='0' /></a></div>";}
>> If (trim($pic_2) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_2' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_2',0) border='0' /></a></div>";}
>> If (trim($pic_3) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_3' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_3',0) border='0' /></a></div>";}
>> If (trim($pic_4) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_4' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_4',0) border='0' /></a></div>";}
>> If (trim($pic_5) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_5' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_5',0) border='0' /></a></div>";}
>> If (trim($pic_6) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_6' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_6',0) border='0' /></a></div>";}
>> If (trim($pic_7) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_7' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_7',0) border='0' /></a></div>";}
>> If (trim($pic_8) <> "") {echo "<div class='proddetailpics'><a href='#' class='color_thumb'> <img src='/imagedir/$pic_8' alt='$itemgroup $itemsubgroup' width='60' height='60' onclick=MM_swapImage('prodimage','','/imagedir/$pic_8',0) border='0' /></a></div>";}
>> }
>>
>>
>
> That's some damn ugly code that I'd consider putting in a loop right away!

The whole page is like this.... that's why I said it was daunting : (
From: Carlton Whitehead on
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



On Tue, Aug 3, 2010 at 3:44 PM, Rick Dwyer <rpdwyer(a)earthlink.net> wrote:

>
> On Aug 3, 2010, at 3:36 PM, Ashley Sheridan wrote:
>
> > On Tue, 2010-08-03 at 15:32 -0400, Rick Dwyer wrote:
> >>
> >> On Aug 3, 2010, at 3:15 PM, Sebastian Ewert wrote:
> >>
> >> > Ashley Sheridan wrote:
> >> >> On Tue, 2010-08-03 at 15:00 -0400, Rick Dwyer wrote:
> >> >>
> >> >>> On Aug 3, 2010, at 2:47 PM, Sebastian Ewert wrote:
> >> >>>
> >> >>>> Rick Dwyer wrote:
> >> >>>>> Hello List.
> >> >>>>>
> >> >>>>> In the Alt section of the IMG tag below, the variable $myitem has
> a value of "Who's There".
> >> >>>>>
> >> >>>>> echo "<div class='myclass'><a href='#' class='color_thumb'> <img
> src='/itemimages/$mypic' alt='$myitem' width='60' ....
> >> >>>>>
> >> >>>>> When running through W3C validator, the line errors out because of
> the " ' " in "Who's".
> >> >>>>> I tried:
> >> >>>>> $myitem=(htmlentities($myitem));
> >> >>>>>
> >> >>>>> But this has no affect on "Who's".
> >> >>>>>
> >> >>>>> What's the best way to code this portion so the apostrophe is
> handled correctly?
> >> >>>>>
> >> >>>>>
> >> >>>>> TIA,
> >> >>>>>
> >> >>>>> --Rick
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>> Use it
> >> >>>>
> >> >>>>
> >> >>>> echo '<div class="myclass"><a href="#" class="color_thumb"> <img
> >> >>>> src="/itemimages/'.$mypic.'" alt="'.$myitem.'" width="60" ...'
> >> >>>
> >> >>> Thanks Sebastian.
> >> >>>
> >> >>> In the above, what is the function of the period in front of
> $myitem?
> >> >>>
> >> >>> --Rick
> >> >>>
> >> >>>
> >> >>
> >> >>
> >> >> It is a string concatenation in PHP. But, as my last email on this
> >> >> thread shows, you only need to add ENT_QUOTES to your htmlentities()
> >> >> call and everything will work.
> >> >>
> >> >> Thanks,
> >> >> Ash
> >> >> http://www.ashleysheridan.co.uk
> >> >>
> >> >>
> >> >>
> >> >
> >> >
> >> > If you use single quotes you can't use variables inside the string.
> You
> >> > have to combine strings and variables or two strings with a dot.
> >> >
> >> > echo 'foo'.$bar;
> >> >
> >> > http://www.php.net/manual/de/language.types.string.php
> >> >
> >> > I'm not shure but I think to validate xhtml you need the double
> quotes.
> >>
> >> Problem I'm having is I've inherited a PHP page that contains sections
> of PHP/Javascript/HTML/CSS all inside of a PHP echo tag. My PHP and JS
> skill are rudimentary at best so when it comes to a block of code 40 to 50
> lines in length, it becomes daunting to reverse the ' with ". Each echo
> block starts with a " and the html inside uses a '. JS also uses '.
> >>
> >> Below is an actual block in more detail with JS in it. Is it still
> recommended to switch with " with ' and ' with "?
> >>
> >> --Rick
> >>
> >>
> >> echo "
> >> <p>Click on a picture to view that color:</p>";
> >> If (trim($pic_1) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_1' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_1',0) border='0'
> /></a></div>";}
> >> If (trim($pic_2) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_2' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_2',0) border='0'
> /></a></div>";}
> >> If (trim($pic_3) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_3' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_3',0) border='0'
> /></a></div>";}
> >> If (trim($pic_4) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_4' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_4',0) border='0'
> /></a></div>";}
> >> If (trim($pic_5) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_5' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_5',0) border='0'
> /></a></div>";}
> >> If (trim($pic_6) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_6' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_6',0) border='0'
> /></a></div>";}
> >> If (trim($pic_7) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_7' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_7',0) border='0'
> /></a></div>";}
> >> If (trim($pic_8) <> "") {echo "<div class='proddetailpics'><a
> href='#' class='color_thumb'> <img src='/imagedir/$pic_8' alt='$itemgroup
> $itemsubgroup' width='60' height='60'
> onclick=MM_swapImage('prodimage','','/imagedir/$pic_8',0) border='0'
> /></a></div>";}
> >> }
> >>
> >>
> >
> > That's some damn ugly code that I'd consider putting in a loop right
> away!
>
> The whole page is like this.... that's why I said it was daunting : (
From: Richard Quadling on
On 4 August 2010 13:33, Carlton Whitehead <carlton.whitehead(a)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.