[PHP] PHP Examples

2006-02-10 Thread Dave Jarvis
Hi,

I didn't quite know where this should go inside the PHP user manual.
It is a collection of six simple tricks I use to give websites that
extra flair of functionality.

http://joot.com/dave/writings/articles/php-examples.shtml

I hope you find the auto-complete city and country is especially useful.

Sincerely,
Dave Jarvis

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



Re: [PHP] PHP Examples

2006-02-10 Thread Paul Novitski

At 04:08 AM 2/10/2006, Dave Jarvis wrote:

It is a collection of six simple tricks I use to give websites that
extra flair of functionality.

http://joot.com/dave/writings/articles/php-examples.shtml

I hope you find the auto-complete city and country is especially useful.



Nice collection, Dave.  I've got a few comments:

Why don't you make the table of contents at the top of the page a 
list of links for easier navigation?



XML Tag Parsing:

Your code assumes that the tag you're searching for is unique and 
doesn't have any attributes.  I'd think you'd want to use RegExp to 
search for something like this:


$tag( [^]*)*

= tagName followed by (zero or more groups of [a space followed by 
any number of characters that aren't a close-bracket]) followed by a 
close-bracket [1]


cf: PCRE regex pattern syntax
http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

If you use preg_match() you can capture multiple instances of the tag 
and not just the first one.


cf: preg_match()
http://au.php.net/preg_match


Auto-complete City  Country:

These function references are undefined -- deliberately?

get_location_xml()
parse_city()
parse_country()

I have some suggestions for your country select list:

a) For greater compatibility with future markup (XHTML etc.) I 
suggest you change:

SELECTED='true'
to:
selected=selected

b) Your exhaustive list of if-tests is quite verbose and could be 
shrunk.  For example, you could throw the country abbreviations  
names into an array and then walk the array outputting the select 
options, including the selected attribute when there's a match with 
the target country.


c) You're mixing PHP logic with HTML markup more than is 
necessary.  Even including both in the same script you could separate 
them something like this:

__

$aNations = array(AU=Australia, CA=Canada, ...);
$sSelectedAttr =  selected=\selected\;

foreach ($aNations as $sNA = $sNation)
{
$sSelected = (($country == $sNA) ? $sSelectedAttr : );

echo  heredocNationOption
option value=$sNA$sSelected$sNation/option

heredocNationOption;
}
__

Performing the logic and outputting the markup as separate steps is 
my personal preference simply because it tends to make the code 
easier to modify in future.  More ideally, I'd remove the literal 
markup from the function entirely, passing it as template argument, 
and even further removing the markup completely from the PHP script 
to a separate template file, so that the user (developer) can tweak 
the markup independently of messing with the logic.  For example, if 
you pass the markup to the function as arguments (listBegin, 
listOption, listSelected, and listEnd) then you can output the list 
as a SELECT, OL, UL, TABLE, etc. without having to modify the script.


Regards,
Paul 


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



Re: [PHP] PHP Examples

2006-02-10 Thread tedd

Hi,

I didn't quite know where this should go inside the PHP user manual.
It is a collection of six simple tricks I use to give websites that
extra flair of functionality.

http://joot.com/dave/writings/articles/php-examples.shtml

I hope you find the auto-complete city and country is especially useful.

Sincerely,
Dave Jarvis


Dave:

Thanks for posting your code -- it works great!

However, I detected a slight problem, which can be corrected like so.

//---

function get_tag_contents( $xml, $tag )
{
$result = ;
$s_tag = $tag;
$s_offs = strpos( $xml, $s_tag );

// Note, use ===  because == will not work as expected.
// The position of the first character is zero not one.
if ($s_offs === false)
{
echo ((1) The needle was not found in the haystack \ $xml\.);
}
else
{
$e_tag = /$tag;
$e_offs = strpos( $xml, $e_tag, $s_offs );

// If we have both tags, then dig out the contents.
//
if( $e_offs === false )
{
echo ((2) The needle was not found in the haystack \ $xml\.);
}
else
{
$result = substr(
$xml,
$s_offs + strlen( $s_tag ),
$e_offs - $s_offs - strlen( $e_tag ) + 1 );
}

}

return $result;
}
//---

tedd
--

http://sperling.com/

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



Re: [PHP] PHP Examples

2006-02-10 Thread Richard Lynch
On Fri, February 10, 2006 1:36 pm, Paul Novitski wrote:
 At 04:08 AM 2/10/2006, Dave Jarvis wrote:
It is a collection of six simple tricks I use to give websites that
extra flair of functionality.

http://joot.com/dave/writings/articles/php-examples.shtml

I hope you find the auto-complete city and country is especially
 useful.

While we're at it, I'd recommend a cron job to check the ISO (?)
reference standard of countries and country codes to re-populate your
data every month or so.

After all, the list of countries *DOES* change, and your site should
be current, no?

I forget exactly where I found the standard list, as I coded it and
forgot it and it's worked fine for many years now.
http://info.com/country+codes+reference+standard

-- 
Like Music?
http://l-i-e.com/artists.htm

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