php-general Digest 20 Mar 2012 13:23:42 -0000 Issue 7735

Topics (messages 317131 through 317140):

Re: Getting knotted with quotes encoding - (one possible solution)
        317131 by: Arno Kuhl

mysql list to two-column list
        317132 by: Tom Sparks
        317133 by: Ashley Sheridan
        317134 by: Tom Sparks
        317135 by: Ashley Sheridan
        317136 by: Govinda
        317137 by: Tom Sparks
        317138 by: Tom Sparks
        317139 by: Stuart Dallas

Sunset/Sunrise
        317140 by: Tedd Sperling

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
-----Original Message-----
From: tamouse mailing lists [mailto:tamouse.li...@gmail.com] 
Sent: 19 March 2012 10:28 AM
To: php-gene...@lists.php.net
Subject: Re: [PHP] Getting knotted with quotes encoding - (one possible 
solution)

On Sun, Mar 18, 2012 at 10:19 PM, Tamara Temple 
<tamouse.li...@tamaratemple.com> wrote:
> On Tue, 13 Mar 2012 16:35:44 +0200, Arno Kuhl <a...@dotcontent.net> sent:
>
>> From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk]
>> Sent: 13 March 2012 03:25 PM
>> To: a...@dotcontent.net; php-gene...@lists.php.net
>> Subject: Re: [PHP] Getting knotted with quotes encoding
>>
>>
>> Arno Kuhl <a...@dotcontent.net> wrote:
>>
>>> I've been battling with quotes encoding when outputting javascript 
>>> with php.
>>> It can't be unique, so I'm hoping someone has a working solution 
>>> they're willing to share.
>>>
>>> The following works perfectly as long as there aren't any single 
>>> quotes in the link text:
>>>        echo "<span onclick=\"insertLink('$sUrl','$sTitle')\"
>>> class='linkSel'>$sTitle</span>";
>>>
>>> if $sTitle has the value    What's new    it outputs:
>>>        <span 
>>> onclick="insertLink('article/whats-new.html','What&#039;s
>>> new')" class='linkSel'>What&#039;s new</span>
>>>
>>> It displays fine, but javascript complains with:
>>>        Expected ')'  linkmanager.php Line:525 Char:63
>>>
>>>
>>> So I fix this by swapping the double and single quotes around:
>>>        echo "<span onclick='insertLink(\"$sUrl\",\"$sTitle\")'
>>> class='linkSel'>$sTitle</span>";
>>>
>>> Now for that specific link it outputs:
>>>        <span 
>>> onclick='insertLink("article/whats-new.html","What&#039;s
>>> new")' class='linkSel'>What&#039;s new</span> And javascript is happy.
>>>
>>> But elsewhere there's a link     Fred "Buster" Cox     and it outputs:
>>>        <span 
>>> onclick='insertLink("article/fred-buster-cox.html","Fred
>>> &quot;Buster&quot; Cox")' class='linkSel'>Fred &quot;Buster&quot; 
>>> Cox</span>
>>>
>>> Again it displays fine, but javascript complains with:
>>>        Expected ')'  linkmanager.php Line:743 Char:77
>>>
>>>
>>> So it looks like I can't have links that include single quotes and 
>>> double quotes, only one or the other.
>>>
>>> One work-around I thought of was to convert any link texts that 
>>> included double quotes into single quotes when the content is 
>>> posted, and it would then be displayed with single quotes even 
>>> though the user entered double quotes. It's far from ideal but it 
>>> would work, though I can think of a few situations where it would be 
>>> quite confusing to the reader. Are there any other solutions that 
>>> would allow both types of quotes without any conversions?
>>>
>>> Cheers
>>> Arno
>>>
>>>
>>> --
>>
>>
>> You aren't escaping the quotes correctly when they go into your  output.
>> You're escaping them for html not javascript. Javascript  (like php) 
>> escapes single quotes inside a single quote string with a  back slash.
>>
>>
>>  Thanks,
>> Ash
>> http://ashleysheridan.co.uk
>> ---------
>>
>> Thanks for that Ashley.
>> You're right about the encoding.
>> I had a line prior to that:
>>        $sTitle = htmlentities($title, ENT_QUOTES, 'ISO-8859-1', 
>> FALSE); Which encoded the quotes.
>>
>>
>> I couldn't find anything so made a function, which might be useful  
>> for others.
>> It’s a first shot, I'm sure there are ways to improve performance.
>> I also changed the encoding to exclude single quotes.
>> (I'm sure the indenting will get screwed up in the mail)
>>
>>
>> $sTitle = fixSingleQuotes(htmlentities($title, ENT_COMPAT,  
>> 'ISO-8859-1', FALSE));
>>
>> .....
>>
>>
>> /////////////////////////////////////////////////////////////////////
>> /////////// // convert single quotes to curly quotes, xml compliant 
>> // assumes apostrophes must be between 2 alpha chars // and any other 
>> ' is a single quote // &#8216; = left single quote // &#8217; = right 
>> single quote and apostrophe function fixSingleQuotes($sText) {
>>        if (strpos($sText, "'") !== FALSE) {
>>                // there are quotes to convert
>>                $bOpenQuote = FALSE;
>>                $arrAlpha = explode(' ', "a b c d e f g h i j k l m n 
>> o p q r s t  u v w x y z A B C D E F G H I J K L M N O P Q R S T U V 
>> W X Y Z");
>>                $arrText = str_split($sText);
>>                while (($pos = strpos($sText, "'")) !== FALSE) {
>>                        if ($pos == 0) {
>>                                // must be an open quote in first pos
>>                                $sText = "&#8216;".substr($sText, 1);
>>                                $bOpenQuote = TRUE;
>>                        } else {
>>                                if (in_array($arrText[$pos-1], 
>> $arrAlpha)
>>  AND   in_array($arrText[$pos+1], $arrAlpha)) {
>>                                        // apostrophe
>>                                        $quote = "&#8217;";
>>                                } else {
>>                                        // quote
>>                                        if (!$bOpenQuote) {
>>                                                $quote = "&#8216;";
>>                                                $bOpenQuote = TRUE;
>>                                        } else {
>>                                                $quote = "&#8217;";
>>                                                $bOpenQuote = FALSE;
>>                                        }
>>                                }
>>                                $sText = substr($sText, 0, 
>> $pos).$quote.substr($sText, $pos+1);
>>                        }
>>                }
>>        }
>>        return ($sText);
>>
>> } //fixSingleQuotes()
>>
>>
>>
>> Cheers
>> Arno
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: 
>> http://www.php.net/unsub.php
>>
>>
>
>
> This is interesting. I wasn't aware that javascript reencoded html 
> entities in this way.
>
> I'm wondering, though, wouldn't a call to addslashes work in this case?

Wow, no it wouldn't. At least not the way it needs to to be more universal. I 
spent quite a bit of time pondering this and testing things, and finally came 
up with this writeup:
http://wiki.tamaratemple.com/Technology/HandlingQuotesInJavascript

Given the final outcome of needing to write a base64 codec for javascript, 
perhaps Arno's solution above it better?
---- 

Hi tamouse

Interesting article. I referenced 
http://www.dwheeler.com/essays/quotes-in-html.html which you also might find 
useful for this topic. David Wheeler deals generally with converting double and 
single quotes to improve style while remaining compliant across as many areas 
as possible, whereas I was specifically focussed on replacing single quotes 
with something else to overcome the javascript encoding problem (as was your 
article). It's worth reading why he suggests avoiding things like &quot;


Hi Tamara

Be careful of using that code which was a first attempt, (1) it has a bug, (2) 
the rules are too simplistic - it covers most cases, but to cover that last bit 
requires a LOT more code, plus there are 2 conditions that just can't be 
handled as far as I can see, and (3) it only works for English.

Firstly the pos pointer goes out of sync between the string and the array as 
soon as the string is expanded with the first conversion, so the array needs to 
be recreated each time, by moving the line   $arrText = str_split($sText);   
inside the while loop.

Secondly the rules of that function don't handle texts like:  "rock 'n roll", 
"Cass' ball", " 'twas the...", "1's and 2's",  3"6' .

I rewrote the function to fix the bug and handle some of the extra cases, so it 
now works for "Cass' ball" (as long as it's not inside a single quote set, as 
in " he said 'this is Cass' ball' ") and it works for "rock 'n roll" (with a 
special test for " 'n ") and for "1's and 2's" and 3"6'. In fact it does work 
for " he said 'this is Cass' ball' " and " 'twas the..." as long as there 
aren't any following single quotes sets, but to do that required an extra pass 
so the performance drops (I define single quote sets as open/close single 
quotes excluding apostrophes). I think you can't come up with a function to 
handle an unreal string such as " he said 'these are Cass' balls' and she said 
'these are Jess' cats' and 'twas the night before he said 'boo' to her 'cause 
Jess' cat is 'mad' red ".

This was quite an interesting exercise, and could make quite an interesting 
competition to see who could come up with the most efficient function to handle 
the most cases correctly. I think the main rule to follow is that in almost 
every case where the text is impossible to correctly convert it's also probably 
very difficult for users to read, so maybe you can assume that the most 
difficult cases won't occur in the real world.

Cheers
Arno


--- End Message ---
--- Begin Message ---
I have a members list witch I print out once a week,
I would like to make the list into two-column list, but I dont know where to 
start looking to change the code?

here is the code
"
$result = mysql_query("SELECT * FROM customers ORDER BY LastName");

while($row = mysql_fetch_array($result))
  {
  echo $row['LastName'];
  echo " " . $row['FirstName'];
  echo " " . $row['CustomNo'];
  echo "<br />";
  }
"

example output:

Bond James Bond 007
Quagmire Glenn 101
Griffin Peter 102
etc

---
tom_a_sparks "It's a nerdy thing I like to do"
Please use ISO approved file formats excluding Office Open XML - 
http://www.gnu.org/philosophy/no-word-attachments.html
Ubuntu wiki page https://wiki.ubuntu.com/tomsparks
3 x (x)Ubuntu 10.04, Amiga A1200 WB 3.1, UAE AF 2006 Premium Edition, AF 2012 
Plus Edition, Sam440 AOS 4.1.2, Roland DXY-1300 pen plotter, Cutok DC330 
cutter/pen plotter
Wanted: RiscOS system, GEOS system (C64/C128), Atari ST, Apple Macintosh 
(6502/68k/PPC only)

--- End Message ---
--- Begin Message ---
On Mon, 2012-03-19 at 15:43 -0700, Tom Sparks wrote:

> I have a members list witch I print out once a week,
> I would like to make the list into two-column list, but I dont know where to 
> start looking to change the code?
> 
> here is the code
> "
> $result = mysql_query("SELECT * FROM customers ORDER BY LastName");
> 
> while($row = mysql_fetch_array($result))
>   {
>   echo $row['LastName'];
>   echo " " . $row['FirstName'];
>   echo " " . $row['CustomNo'];
>   echo "<br />";
>   }
> "
> 
> example output:
> 
> Bond James Bond 007
> Quagmire Glenn 101
> Griffin Peter 102
> etc
> 
> ---
> tom_a_sparks "It's a nerdy thing I like to do"
> Please use ISO approved file formats excluding Office Open XML - 
> http://www.gnu.org/philosophy/no-word-attachments.html
> Ubuntu wiki page https://wiki.ubuntu.com/tomsparks
> 3 x (x)Ubuntu 10.04, Amiga A1200 WB 3.1, UAE AF 2006 Premium Edition, AF 2012 
> Plus Edition, Sam440 AOS 4.1.2, Roland DXY-1300 pen plotter, Cutok DC330 
> cutter/pen plotter
> Wanted: RiscOS system, GEOS system (C64/C128), Atari ST, Apple Macintosh 
> (6502/68k/PPC only)
> 


How do you mean? In your example, it's a 3-column list (surname,
forename, customer number) although you've added a 'cute' field to the
first entry for giggles. It's a simple thing to join the first two
fields, but what do you determine to be a single column?

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
--- On Tue, 20/3/12, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:

On Mon, 2012-03-19 at 15:43 -0700, Tom Sparks wrote:

>>I have a members list witch I print out once a week,
>>I would like to make the list into two-column list, but I dont know >>where 
>>to start looking to change the code?

>>here is the code
>>"
>>$result = mysql_query("SELECT * FROM customers ORDER BY LastName");

>>while($row = mysql_fetch_array($result))
>>  {
>>  echo $row['LastName'];
>>  echo " " . $row['FirstName'];
>>  echo " " . $row['CustomNo'];
>>  echo "<br />";
>>  }
>>"

>>example output:

>>Bond James 007
>>Quagmire Glenn 101
>>Griffin Peter 102
>>etc

>>---
>>tom_a_sparks "It's a nerdy thing I like to do"
>>Please use ISO approved file formats excluding Office Open XML - 
>>>>http://www.gnu.org/philosophy/no-word-attachments.html
>>Ubuntu wiki page https://wiki.ubuntu.com/tomsparks


>How do you mean?
my goal is to do something phonebook like
> In your example, it's a 3-column list (surname, 
>forename, customer number) although you've added a 'cute' field to the >first 
>entry for giggles. It's a simple thing to join the first two >fields, but what 
>do you determine to be a single column?







-- 












--- End Message ---
--- Begin Message ---
On Mon, 2012-03-19 at 16:09 -0700, Tom Sparks wrote:

> --- On Tue, 20/3/12, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:
> 
> On Mon, 2012-03-19 at 15:43 -0700, Tom Sparks wrote:
> 
> >>I have a members list witch I print out once a week,
> >>I would like to make the list into two-column list, but I dont know >>where 
> >>to start looking to change the code?
> 
> >>here is the code
> >>"
> >>$result = mysql_query("SELECT * FROM customers ORDER BY LastName");
> 
> >>while($row = mysql_fetch_array($result))
> >>  {
> >>  echo $row['LastName'];
> >>  echo " " . $row['FirstName'];
> >>  echo " " . $row['CustomNo'];
> >>  echo "<br />";
> >>  }
> >>"
> 
> >>example output:
> 
> >>Bond James 007
> >>Quagmire Glenn 101
> >>Griffin Peter 102
> >>etc
> 
> >>---
> >>tom_a_sparks "It's a nerdy thing I like to do"
> >>Please use ISO approved file formats excluding Office Open XML - 
> >>>>http://www.gnu.org/philosophy/no-word-attachments.html
> >>Ubuntu wiki page https://wiki.ubuntu.com/tomsparks
> 
> 
> >How do you mean?
> my goal is to do something phonebook like
> > In your example, it's a 3-column list (surname, 
> >forename, customer number) although you've added a 'cute' field to the 
> >>first entry for giggles. It's a simple thing to join the first two >fields, 
> >but what do you determine to be a single column?
> 

Your reply of 9 words adds no extra information. Rather than have us all
guess what you want, try and tell us specifically what it is that you
want.
-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
>> my goal is to do something phonebook like
>> 
> 
> Your reply of 9 words adds no extra information. Rather than have us all
> guess what you want, try and tell us specifically what it is that you
> want.

Hi Tom
I *think* what want to be asking is, "what HTML do I change/add such that my 
data coming from the database will format on the page in the browser like a 
phone book (in columns)?"

If so, then:

This is a PHP list where people mostly discuss PHP-specific things.. whereas it 
seem what you need is to learn some basic HTML which will allow you to format 
content on a page so it looks like a "phone book".  

I would say:

Forget your database data for a day.  Start with seeing if you can just write 
some static HTML that looks like what you want (formatted like a phone book), 
and THEN alter your real PHP code so that it spits out the HTML that mimics 
what you mocked up.  If you get stuck with the HTML part of this task, then 
consult a good HTML list.  If you get stuck with something specific to PHP, 
then ask again about that, here.

Good luck,
-Govinda


--- End Message ---
--- Begin Message ---
--- On Tue, 20/3/12, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:

From: Ashley Sheridan <a...@ashleysheridan.co.uk>
Subject: Re: [PHP] mysql list to two-column list
To: "Tom Sparks" <tom_a_spa...@yahoo.com.au>
Cc: "php-general" <php-gene...@lists.php.net>
Received: Tuesday, 20 March, 2012, 10:15 AM




  
  


On Mon, 2012-03-19 at 16:09 -0700, Tom Sparks wrote:

--- On Tue, 20/3/12, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:

On Mon, 2012-03-19 at 15:43 -0700, Tom Sparks wrote:

>>I have a members list witch I print out once a week,
>>I would like to make the list into two-column list, but I dont know >>where 
>>to start looking to change the code?

>>here is the code
>>"
>>$result = mysql_query("SELECT * FROM customers ORDER BY LastName");

>>while($row = mysql_fetch_array($result))
>>  {
>>  echo $row['LastName'];
>>  echo " " . $row['FirstName'];
>>  echo " " . $row['CustomNo'];
>>  echo "<br />";
>>  }
>>"

>>example output:

>>Bond James 007
>>Quagmire Glenn 101
>>Griffin Peter 102
>>etc

>>---
>>tom_a_sparks "It's a nerdy thing I like to do"
>>Please use ISO approved file formats excluding Office Open XML - 
>>>>http://www.gnu.org/philosophy/no-word-attachments.html
>>Ubuntu wiki page https://wiki.ubuntu.com/tomsparks


>How do you mean?
my goal is to do something phonebook like
> In your example, it's a 3-column list (surname, 
>forename, customer number) although you've added a 'cute' field to the >first 
>entry for giggles. It's a simple thing to join the first two >fields, but what 
>do you determine to be a single column?



Your reply of 9 words adds no extra information. Rather than have us all guess 
what you want, try and tell us specifically what it is that you want.




    Cleveland Brown      | Griffin family
    Cleveland Brown, Jr.  |  Brian Griffin
    


--- End Message ---
--- Begin Message ---
please delete me
---
tom_a_sparks "It's a nerdy thing I like to do"
Please use ISO approved file formats excluding Office Open XML - 
http://www.gnu.org/philosophy/no-word-attachments.html
Ubuntu wiki page https://wiki.ubuntu.com/tomsparks
3 x (x)Ubuntu 10.04, Amiga A1200 WB 3.1, UAE AF 2006 Premium Edition, AF 2012 
Plus Edition, Sam440 AOS 4.1.2, Roland DXY-1300 pen plotter, Cutok DC330 
cutter/pen plotter
Wanted: RiscOS system, GEOS system (C64/C128), Atari ST, Apple Macintosh 
(6502/68k/PPC only)


--- On Tue, 20/3/12, Tom Sparks <tom_a_spa...@yahoo.com.au> wrote:

> From: Tom Sparks <tom_a_spa...@yahoo.com.au>
> Subject: Re: [PHP] mysql list to two-column list
> To: a...@ashleysheridan.co.uk
> Cc: "php-general" <php-gene...@lists.php.net>
> Received: Tuesday, 20 March, 2012, 10:41 AM
> --- On Tue, 20/3/12, Ashley Sheridan
> <a...@ashleysheridan.co.uk>
> wrote:
> 
> From: Ashley Sheridan <a...@ashleysheridan.co.uk>
> Subject: Re: [PHP] mysql list to two-column list
> To: "Tom Sparks" <tom_a_spa...@yahoo.com.au>
> Cc: "php-general" <php-gene...@lists.php.net>
> Received: Tuesday, 20 March, 2012, 10:15 AM
> 
> 
> 
> 
>   
>   
> 
> 
> On Mon, 2012-03-19 at 16:09 -0700, Tom Sparks wrote:
> 
> --- On Tue, 20/3/12, Ashley Sheridan <a...@ashleysheridan.co.uk>
> wrote:
> 
> On Mon, 2012-03-19 at 15:43 -0700, Tom Sparks wrote:
> 
> >>I have a members list witch I print out once a
> week,
> >>I would like to make the list into two-column list,
> but I dont know >>where to start looking to change the
> code?
> 
> >>here is the code
> >>"
> >>$result = mysql_query("SELECT * FROM customers ORDER
> BY LastName");
> 
> >>while($row = mysql_fetch_array($result))
> >>  {
> >>  echo $row['LastName'];
> >>  echo " " . $row['FirstName'];
> >>  echo " " . $row['CustomNo'];
> >>  echo "<br />";
> >>  }
> >>"
> 
> >>example output:
> 
> >>Bond James 007
> >>Quagmire Glenn 101
> >>Griffin Peter 102
> >>etc
> 
> >>---
> >>tom_a_sparks "It's a nerdy thing I like to do"
> >>Please use ISO approved file formats excluding
> Office Open XML - >>http://www.gnu.org/philosophy/no-word-attachments.html
> >>Ubuntu wiki page https://wiki.ubuntu.com/tomsparks
> 
> 
> >How do you mean?
> my goal is to do something phonebook like
> > In your example, it's a 3-column list (surname, 
> >forename, customer number) although you've added a
> 'cute' field to the >first entry for giggles. It's a
> simple thing to join the first two >fields, but what do
> you determine to be a single column?
> 
> 
> 
> Your reply of 9 words adds no extra information. Rather than
> have us all guess what you want, try and tell us
> specifically what it is that you want.
> 
> 
> 
> 
>     Cleveland Brown      | Griffin
> family
>     Cleveland Brown, Jr.  |  Brian
> Griffin
>     
> 
>

--- End Message ---
--- Begin Message ---
On 19 Mar 2012, at 22:43, Tom Sparks wrote:

> I have a members list witch I print out once a week,
> I would like to make the list into two-column list, but I dont know where to 
> start looking to change the code?
> 
> here is the code
> "
> $result = mysql_query("SELECT * FROM customers ORDER BY LastName");
> 
> while($row = mysql_fetch_array($result))
>  {
>  echo $row['LastName'];
>  echo " " . $row['FirstName'];
>  echo " " . $row['CustomNo'];
>  echo "<br />";
>  }
> "


The following is untested so it may contain syntax errors, but I'm pretty sure 
the logic is sound and I think it will give you what you want. You may want to 
play with the cellpadding value to adjust the amount of space between the table 
cells.

echo '<table cellspacing="0" cellpadding="5" border="0">';
$column = 1;
while ($row = mysql_fetch_assoc($result))
{
  if ($column == 1) {
    echo '<tr>'.PHP_EOL;
  }
  echo '  <td>'.$row['LastName'].' '.$row['FirstName'].' 
'.$row['CustomNo'].'</td>';
  $column++;
  if ($column > 2) {
    echo '</tr>'.PHP_EOL;
    $column = 1;
  }
}
if ($column == 2) {
  echo '<td>&nbsp;</td>'.PHP_EOL;
}
echo '</tr></table>';

Assuming I'm right in my interpretation of what you want, the following needs 
to be said... this is *very* basic HTML being rendered by *very* basic PHP. I 
suggest you learn about basic HTML first, then learn basic PHP, then work out 
how to use logic in PHP to build the HTML you want.

If you're unsure how the above works, start by viewing the source of the page 
in your browser. If it's still not clear, run it in your head. Use two pieces 
of paper, one to store the variables and the other to keep track of the output. 
Go through each loop and for each line update the variables and update the 
output for each echo statement.

If I got what you want wrong then I have absolutely no clue what you're after.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
Hi gang:

What's wrong with this?

echo date("D M d Y"). ', sunrise time : ' .date_sunrise(time(), 
SUNFUNCS_RET_STRING, 42.57, 84.3320, 90, -5);
echo('<br>');
echo date("D M d Y"). ', sunset time : ' .date_sunset(time(), 
SUNFUNCS_RET_STRING, 42.57, 84.3320, 90, -5);

It gives exactly the wrong time -- Sunset is reported as Sunrise and vice 
versa. What's up?

Thanks,

tedd


_____________________
tedd.sperl...@gmail.com
http://sperling.com






--- End Message ---

Reply via email to