Login   Register  
PHP Classes
elePHPant
Icontem

File: sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of jerry mattsson  >  dbff  >  sample.php  >  Download  
File: sample.php
Role: Example script
Content type: text/plain
Description: Some samples
Class: dbff
Read and write flat file databases in CSV format
Author: By
Last change: filled it with text
Date: 8 years ago
Size: 10,218 bytes
 

Contents

Class file image Download
<?php

// This would be the format and sample conten of the data files to load
/* countries.txt
AF,AFG,AFGHANISTAN
AL,ALB,ALBANIA
BE,BEL,BELGIUM
BZ,BLZ,BELIZE
CM,CMR,CAMEROON
CA,CAN,CANADA
DK,DNK,DENMARK
DJ,DJI,DJIBOUTI
EC,ECU,ECUADOR
EG,EGY,EGYPT

cities.txt
Afghanistan;Herat
Afghanistan;KABUL
Afghanistan;Kandahar
Afghanistan;Mazar-i-Sharif
Albania;TIRANA
Belgium;Antwerp
Belgium;Brugge
Belgium;BRUSSELS
Belgium;Charleroi
Belgium;Gent
Belgium;Kortrijk
Belgium;La Louvi´le
Belgium;Li´le
Belgium;Mons
Belgium;Namur
Belize;BELMOPAN
Cameroon;Douala
Cameroon;YAOUNDE
Canada;Brampton
Canada;Burlington 
Canada;Burnaby 
Canada;Calgary 
Canada;East York 
Canada;Edmonton 
Canada;Etobicoke 
Canada;Gloucester 
Canada;Halifax 
Canada;Hamilton 
Canada;Kitchener 
Canada;Laval 
Canada;London 
Canada;Longueuil 
Canada;Markham 
Canada;Mississauga 
Canada;Montreal 
Canada;Nepean 
Canada;North York 
Canada;Oakville 
Canada;Oshawa 
Canada;OTTAWA 
Canada;Qu´┐Żec 
Canada;Regina 
Canada;Richmond 
Canada;Saskatoon 
Canada;Scarborough 
Canada;St Catharines 
Canada;Surrey 
Canada;Thunder Bay 
Canada;Toronto 
Canada;Vancouver 
Canada;Vaughan
Canada;Windsor
Canada;Winnipeg
Canada;York
Denmark;COPENHAGEN 
Ecuador;Ambato 
Ecuador;Cuenca 
Ecuador;Esmeraldas 
Ecuador;Guayaquil 
Ecuador;Machala 
Ecuador;Manta
Ecuador;Milagro
Ecuador;Portoviejo
Ecuador;QUITO
Ecuador;Santo Domingo de los Colorados
Egypt;Alexandria
Egypt;Assyt
Egypt;Aswan
Egypt;Banha
Egypt;Beni-Suef
Egypt;CAIRO
Egypt;Damanhr
Egypt;El-Mahalla El-Kubra
Egypt;Faiym
Egypt;Giza
Egypt;Ismailia
Egypt;Kafr-El-Dwar
Egypt;Kena
Egypt;Luxer
Egypt;Mansra
Egypt;Menia
Egypt;Port Said
Egypt;Shebin-El-Kom
Egypt;Shubra-El-Khema
Egypt;Sohag
Egypt;Suez
Egypt;Tanta
Egypt;Zagazig
*/

/*  Name: load_countries.php dbff sample
    Author: Jerry Mattsson
    Created/Updated july-05/oct-06
*/
require_once  'dbff.php';
/* load file
AF,AFG,AFGHANISTAN
AL,ALB,ALBANIA
DZ,DZA,ALGERIA
....
into file db
*/
print '<HTML><HEAD><BODY BGCOLOR="aqua">';
$file               'countries.txt';
$delimiter          ',';
$countries_file     'countries';

$countries_def = array (
   
'name'    => array ('size'=>50'chkNN'=>true),
   
'iso2'    => array ('size'=>2,  'pk'=>1'UPPER'=>true),
   
'iso3'    => array ('size'=>3,  'UPPER'=>true));

$countries            = new dbff;
$countries->file      $countries_file;
$countries->recdef    $countries_def;
$countries->changelog FALSE;
$countries->rectype   1;
$countries->trace     TRUE;

$rec = array ();
if ( !
file_exists($file) ) {
   print 
"File $file not found";
   exit;
}
// read and insert
  
$fp     fopen($file'r') or die("Cannot open file $file");
  print 
'Reading Country File<br>';
   while (!
feof ($fp)) {
         
$str trim(fgets($fp));
         if (
trim($str)<>'') {
            
$arr explode($delimiter,$str);
        
$rec = array ( ucwords(strtolower($arr[2])), $arr[0], $arr[1] );
        print 
'Inserting '.$rec[0].' ';
        if(!
$countries->insert ($rec)) print $dbfferr.'<br>';
     };
   };
  
fclose($fp) or die("Cannot close file $file");
// done

print '<br>';
print 
count($countries->recs). ' Inserted<br>';
$countries->commit();
print_r($countries->errstk);
print 
'</BODY></HTML>';
exit;
?>
<?php
/*  Name: load_cities.php, dbff sample
    Author: Jerry Mattsson
    Created/Updated july 05
*/
require_once  'dbff.php';
/* load file
Afghanistan;Herat
Afghanistan;KABUL
Afghanistan;Kandahar
Afghanistan;Mazar-i-Sharif
....
into file db
*/
print '<HTML><HEAD><BODY BGCOLOR="aqua">';
$file               'cities.txt';
$delimiter          ';';
$countries_file     'countries';
$cities_file        'cities';

$countries_def = array (
   
'name'    => array ('size'=>50'chkNN'=>true),
   
'iso2'    => array ('size'=>2,  'pk'=>1'UPPER'=>true),
   
'iso3'    => array ('size'=>3,  'UPPER'=>true));

$cities_def = array (
   
'id'        => array ('size'=>10'type'=>'INT''pk'=>'sequence'),
   
'name'    => array ('size'=>35'chkNN'=>true'uk'=>true),
   
'country'    => array ('size'=>2,  'chkNN'=>true'UPPER'=>true'fk'=> array('COUNTRY','ISO2')));

$countries          = new dbff;
$countries->file    $countries_file;
$countries->recdef  $countries_def;

$cities             = new dbff;
$cities->file       $cities_file;
$cities->recdef     $cities_def;
$cities->rectype    1;
$cities->changelog  FALSE;
//$cities->trace      = TRUE;

$i 0;
if ( !
file_exists($file) ) {
   print 
"File $file not found";
   exit;
}
// read file
  
$fp     fopen($file'r') or die("Cannot open file $file");
  print 
'Reading City File<br>';
   while (!
feof ($fp)) {
         
$str trim(fgets($fp));
         if (!
trim($str)=='') {
            
$arr explode($delimiter,$str);
        
$cty ucwords(strtolower(trim($arr[0])));
        
$cntyrec $countries->getrec($cty,0);
        if (
$cntyrec==NULL) print "Country $cty not found<br>";
        else {
               
$rec = array ( 0ucwords(strtolower($arr[1])), $cntyrec[1] ); // dummy id value
           
if(!$cities->insert ($rec)) print '<br>'.$dbfferr;
        }
     };
   };
  
fclose($fp) or die("Cannot close file $file");
// Done
print '<br>'.count($cities->recs). ' Inserted<br>';
$cities->commit();
//if ($cities->errstk) print_r ($cities->errstk);
print '</BODY></HTML>';
?>

<?php
/*  Name: list_countries.php dbff sample
    Author: Jerry Mattsson
    Created/Updated july-05/oct-06
*/
require_once  'dbff.php';

print 
'<HTML><HEAD><BODY BGCOLOR="aqua">';
$countries_file     'countries';
$countries_def = array ( 'TABLE_NAME'=>'COUNTRY',
   
'name'    => array ('size'=>50'chkNN'=>true),
   
'iso2'    => array ('size'=>2,  'pk'=>1'UPPER'=>true),
   
'iso3'    => array ('size'=>3,  'UPPER'=>true));

$countries            = new dbff;
$countries->file      $countries_file;
$countries->recdef    $countries_def;
//$countries->trace     = TRUE;
// read and list
$countries->select();
while (
$c=$countries->selectget()) {
   print 
'Iso2='.$c['COUNTRY.ISO2'].' Iso3='.$c['COUNTRY.ISO3'].' '.$c['COUNTRY.NAME'].'<br>';
}
print 
'<br>'.count($countries->recs). ' Listed<br>';
print_r($countries->errstk);
print 
'</BODY></HTML>';
exit;
?>
<?php
/*  Name: search_countries.php, dbff sample
    Author: Jerry Mattsson
    Created oct-06
*/
require_once  'dbff.php';

$country_name $city_name NULL;
if (isset(
$_POST['scountry']) && isset($_POST['country']) && !empty($_POST['country'])) 
   
$country_name trim($_POST['country']);

$countries_def = array ( 'TABLE_NAME'=>'COUNTRY''FILE_NAME'=>'countries',
   
'name'    => array ('size'=>50'chkNN'=>true),
   
'iso2'    => array ('size'=>2,  'pk'=>1'UPPER'=>true),
   
'iso3'    => array ('size'=>3,  'UPPER'=>true));

$cities_def = array ( 'TABLE_NAME'=>'CITY''FILE_NAME'=>'cities',
   
'id'        => array ('size'=>10'type'=>'INT''pk'=>'sequence'),
   
'name'    => array ('size'=>35'chkNN'=>true'uk'=>true),
   
'country'    => array ('size'=>2,  'chkNN'=>true'UPPER'=>true'fk'=> array('COUNTRY','ISO2')));

$countries          = new dbff;
$countries->recdef  $countries_def;
$cities             = new dbff;
$cities->recdef     $cities_def;

$c1 $countries->select();
$c2 $cities->select();
$c4 $c1*$c2;

print 
'<HTML><HEAD><BODY BGCOLOR="aqua">';
print    
'<form name="ccsearch" action="'.$_self.'" method="post" >
    Enter Letters or name for Country search:
    <input  type="TEXT" name="country" id="country" size="30" value="">
    <input  type="SUBMIT" name="scountry" id="scountry">
    <br>
    </FORM>'
;

Print 
"Reading $c1 countries and $c2 cities $c1 ($c4 possible combinations)<br>";

if (!isset(
$country_name) || empty($country_name)) {
   print 
"No Search value entered";
   exit;
}

$str NULL;
if (!
is_null($country_name)) {    /// Search country
   
$c1 $countries->select('name',$country_name,'like');
   if (
$c1==0) {
      
$str .= "No Country starting with $country_name found";
      exit;
   }
   while (
$c=$countries->selectget()) {
      
$str .= '<br>'.$c['COUNTRY.NAME'].' ';
      
$c3 $cities->select('country',$c['COUNTRY.ISO2']);
      if (
$c3>0) {
         
$str .= "Has $c3 City/Cities=";
         while (
$cc=$cities->selectget()) $str .= $cc['CITY.NAME'].' ';
      } else 
$str .= ' Has no cities stored';
   }
}
print 
$str.'<br>';

print_r($countries->errstk);
print_r($cities->errstk);
print 
'</BODY></HTML>';
exit;
?>
<?php
/*  Name: search_cities.php dbff sample
    Author: Jerry Mattsson
    Created oct-06
*/
require_once  'dbff.php';

if (isset(
$_POST['scity']) && isset($_POST['city']) && !empty($_POST['city'])) 
  
$city_name trim($_POST['city']);

$countries_def = array ( 'TABLE_NAME'=>'COUNTRY''FILE_NAME'=>'countries',
   
'name'    => array ('size'=>50'chkNN'=>true),
   
'iso2'    => array ('size'=>2,  'pk'=>1'UPPER'=>true),
   
'iso3'    => array ('size'=>3,  'UPPER'=>true));

$cities_def = array ( 'TABLE_NAME'=>'CITY''FILE_NAME'=>'cities',
   
'id'        => array ('size'=>10'type'=>'INT''pk'=>'sequence'),
   
'name'    => array ('size'=>35'chkNN'=>true'uk'=>true),
   
'country'    => array ('size'=>2,  'chkNN'=>true'UPPER'=>true'fk'=> array('COUNTRY','ISO2')));

$countries          = new dbff;
$countries->recdef  $countries_def;
$cities             = new dbff;
$cities->recdef     $cities_def;

$c1 $countries->select();
$c2 $cities->select();
$c4 $c1*$c2;

print 
'<HTML><HEAD><BODY BGCOLOR="aqua">';
print    
'<form name="ccsearch" action="'.$_self.'" method="post" >
    Enter Letters or name for City search:
    <input  type="TEXT" name="city" id="city" size="30" value="">
    <input  type="SUBMIT" name="scity" id="scity">
    <br>
    </FORM>'
;

Print 
"Reading $c1 countries and $c2 cities $c1 ($c4 possible combinations)<br>";

if (!isset(
$city_name) || empty($city_name)) {
   print 
"No Search value entered";
   exit;
}

$str NULL;
/// Search city
$c3 $cities->select('name',$city_name,'like');
if (
$c3==0) {
   
$str .= "No City starting with $city_name found";
} else {
   while (
$cc=$cities->selectget()) {
      
$str .=  '<br>City named: '.$cc['CITY.NAME'].' ';
      
$c1 $countries->select('iso2',$cc['CITY.COUNTRY']);
      if (
$c1>0) {
         
$str .= "Exists in ";
         while (
$c=$countries->selectget()) $str .= $c['COUNTRY.NAME'].' ';
      } else 
$str .= ' Has no corresponding country!';
   }
}
print 
$str.'<br>';

print_r($countries->errstk);
print_r($cities->errstk);
print 
'</BODY></HTML>';
exit;
?>