Archive for the ‘ Useful code ’ Category

CakePHP Printable Cookbook Page Generator

Wednesday, August 17th, 2011

CakePhp is one of the most popular php-based web application frameworks, but it’s not without its frustrations. I’m not speaking of its steep learning curve, but its misnamed cookbook, which is really its manual.   Try as you may, you won’t get their cookbook to print properly, and there’s no version in book form that you can buy. Did they make it this way intentionally?

If you’re a little bit patient, I have a free solution for you, that will save you a bundle on ink cartridges. It’s a little script which you can use that will churn out printable versions of the cookbook pages you need, minus the stuff that causes the problems and the header and sidebar. I can’t host the script on my site since it might get me the wrath of the CakePhp people, and I can’t offer you a ready made printable manual for the same reason. Thus, the fair thing to do is to give you the code below and you use it on your own web site on a non-public page for your own use.

Usage: Just enter the full cookbook URLs into the form and it will fetch them, parse them, and then combine them into one html doc, which you can then save or print directly from your browser. It’s probably not a good idea to try to paste hundreds of URLs at a time. What I did was go section by section. Here’s the code…


<?php
/**
* By Tom Germain, cgiware.com
* Free for personal use only.
* CakePHP cookbook printable page generator
* Just install this on your web site and use it!
**/
if(isset($_REQUEST['url'])&&!empty($_REQUEST['url'])){
$URLS=explode("\n",$_REQUEST['url']);
if(count($URLS)==1) $URLS=explode("\r",$_REQUEST['url']);

$DOC="";
foreach($URLS as $url){
$url=trim($url);
if(!$url) continue;

$HTML=file_get_contents($url);

if(!$HTML){
echo "Could not fetch url: $url";
exit;
}

$x=explode('<div class="node-nav">',$HTML);
$HTML=array_shift($x);
$x=explode('<div id="content">',$HTML);
$HTML=array_pop($x);

$HTML=preg_replace('/<a href="http\:\/\/cakefest\.org".+?<\/a>\n/','',$HTML);

$x=explode('<ol class="code">',$HTML);
$HTML=array_shift($x);
foreach($x as $xx){
$y=explode("</ol>",$xx);
$HTML.=array_pop($y);
}

$HTML=preg_replace('/<div class="options">(.|\n)+?<\/div>/m','',$HTML);
$HTML=preg_replace('/<div class="comments".+?<\/div>/','',$HTML);

$HTML=str_replace('class="code"','style="color: blue; font-style:italic;"',$HTML);
if($DOC) $DOC.="<p><hr><p>";
$DOC.=$HTML;
sleep(1);
}

 

header("Content-Type: text/html; charset=utf-8");
print $DOC;
exit;
}
?>
<form method="POST">
Enter URLs and hit button: <p><textarea cols=80 rows=40 name="url"></textarea>
<p>
<input type=submit>
</form>

Related Articles:

  • No related article

IP Address to Country Lookup

Sunday, May 22nd, 2011

I strive to provide free data that programmers in general can use. Here, I give you a table and php code that will allow you to identify the country your visitors are coming from, using only their ip address. The database table contains three field: starting ip (in long form), ending ip, and corresponding two  letter ISO country code. The reason for using the long version, which is an integer number, instead of the dotted string notation  is that it’s faster to match in SQL queries. In php, you have two built-in functions that allow you to convert from the long form to the dotted notation and back.

To retrieve the country code for a given ip, use this php code:

Your country is: <?php print get_country($_REQUEST['REMOTE_ADDR']);?>
<?php 

function get_country($IP){
$IPL=ip2long($IP);
$sth=mysql_query(“SELECT country FROM `ip_countries` WHERE $IPL between start and end”);
if(!$result=mysql_fetch_row($sth)) return ”Unknown”;
return $result[0];
}

?>

You can derive the dotted  ip address from the long address by using php’s long2ip function.

If you want the full country name, you can look-up the 2 letter code in a table of countries. I provided that data in a previous post: “List of countries in English and French“.

For a query file that will build you and populate a table called “ip_countries”, download this zip file (1.1 Mb) , unzip and pump it into Mysql (Ex: mysql -uuser -ppassword -Ddatabase <ip_countries.sql).

Alternatively, you can download this zipped  flat file that  uses 3 columns separated by tabs.

Related Articles:

Table of US Counties and Cities

Sunday, November 7th, 2010

I never could find a free table or list of US counties and cities per county, thus I had to do a bit of data mining to come up with my own. This is just too much data to paste into a page, so I offer it to you as downloads.

For a query file that will build you and populate a table called “counties”, download this file (68.6 Kb) and pump into Mysql (Ex: mysql -uuser -ppassword -Ddatabase < counties.sql.txt).

For a query file that will build you and populate a table called “cities”, download this file (2.17 Mb) and pump into Mysql (Ex: mysql -uuser -ppassword -Ddatabase < cities.sql.txt).

Alternatively, you can download this flat file (2.47 Mb) that lists city names, counties and states over 3 columns separated by tabs.

Once you have both these tables, you can use queries to get at the data you want. For example:

Find all counties in Washington state:

select county_id,county  from counties where state=’WA’;

Find all the cities in Franklin county in Washington state:

Select city from cities where county_id in(select county_id from counties where state=’WA’ and county=’Franklin’);

Related Articles:

List of States, Provinces

Sunday, November 7th, 2010

Previously, I posted lists of countries and languages that you can use in your code. Now here’s some for US states and Canadian provinces, the keys being the 2 letter standard codes.

First, the US states:

Just copy the code below, or download it as a file here, or use this ready to go html select, or this php array, or use this file with Mysql to create and populate a table called “states” (Ex: mysql -uuser -ppassword -Ddatabase < states.sql.txt)


AL Alabama
AK Alaska
AZ Arizona
AR Arkansas
CA California
CO Colorado
CT Connecticut
DE Delaware
DC District Of Columbia
FL Florida
GA Georgia
HI Hawaii
ID Idaho
IL Illinois
IN Indiana
IA Iowa
KS Kansas
KY Kentucky
LA Louisiana
ME Maine
MD Maryland
MA Massachusetts
MI Michigan
MN Minnesota
MS Mississippi
MO Missouri
MT Montana
NE Nebraska
NV Nevada
NH New Hampshire
NJ New Jersey
NM New Mexico
NY New York
NC North Carolina
ND North Dakota
OH Ohio
OK Oklahoma
OR Oregon
PA Pennsylvania
RI Rhode Island
SC South Carolina
SD South Dakota
TN Tennessee
TX Texas
UT Utah
VT Vermont
VA Virginia
WA Washington
WV West Virginia
WI Wisconsin
WY Wyoming

…and if you need them, you can add the US territories:


AS American Samoa
FM Federated States of Micronesia
GU Guam
MH Marshall Islands
MP Northern Mariana Islands
PW Palau
PR Puerto Rico
VI Virgin Islands

…and now the Canadian provinces, of which there are few! You can also use this ready to go html select


AB Alberta
BC British Columbia
MB Manitoba
NB New Brunswick
NF Newfoundland
NS Nova Scotia
NU Nunavut
NW North West Territories
ON Ontario
PE Prince Edward Island
QC Québec
SK Saskatchewen
YU Yukon

Related Articles:

List of Languages

Saturday, November 6th, 2010

Previously, I posted lists of countries that you can use in your code. Now here’s one for languages, the keys being the 2 letter ISO codes.

Just copy the code below, or download it as a file here or, use this file with Mysql to create and populate a table called “languages”  (Ex: mysql -uuser -ppassword -Ddatabase < languages.sql.txt)


AA Afar
AB Abkhazian
AF Afrikaans
AM Amharic
AR Arabic
AS Assamese
AY Aymara
AZ Azerbaijani
BA Bashkir
BE Byelorussian
BG Bulgarian
BH Bihari
BI Bislama
BN Bengali
BO Tibetan
BR Breton
CA Catalan
CO Corsican
CS Czech
CY Welsh
DA Danish
DE German
DZ Bhutani
EL Greek
EN English
EO Esperanto
ES Spanish
ET Estonian
EU Basque
FA Persian
FI Finnish
FJ Fiji
FO Faeroese
FR French
FY Frisian
GA Irish
GD Gaelic
GL Galician
GN Guarani
GU Gujarati
HA Hausa
HI Hindi
HR Croatian
HU Hungarian
HY Armenian
IA Interlingua
IE Interlingue
IK Inupiak
IN Indonesian
IS Icelandic
IT Italian
IW Hebrew
JA Japanese
JI Yiddish
JW Javanese
KA Georgian
KK Kazakh
KL Greenlandic
KM Cambodian
KN Kannada
KO Korean
KS Kashmiri
KU Kurdish
KY Kirghiz
LA Latin
LN Lingala
LO Laothian
LT Lithuanian
LV Latvian
MG Malagasy
MI Maori
MK Macedonian
ML Malayalam
MN Mongolian
MO Moldavian
MR Marathi
MS Malay
MT Maltese
MY Burmese
NA Nauru
NE Nepali
NL Dutch
NO Norwegian
OC Occitan
OM Oromo
OR Oriya
PA Punjabi
PL Polish
PS Pashto
PT Portuguese
QU Quechua
RM Rhaeto-Romance
RN Kirundi
RO Romanian
RU Russian
RW Kinyarwanda
SA Sanskrit
SD Sindhi
SG Sangro
SH Serbo-Croatian
SI Singhalese
SK Slovak
SL Slovenian
SM Samoan
SN Shona
SO Somali
SQ Albanian
SR Serbian
SS Siswati
ST Sesotho
SU Sudanese
SV Swedish
SW Swahili
TA Tamil
TE Tegulu
TG Tajik
TH Thai
TI Tigrinya
TK Turkmen
TL Tagalog
TN Setswana
TO Tonga
TR Turkish
TS Tsonga
TT Tatar
TW Twi
UK Ukrainian
UR Urdu
UZ Uzbek
VI Vietnamese
VO Volapuk
WO Wolof
XH Xhosa
YO Yoruba
ZH Chinese
ZU Zulu

Related Articles: