5. CRUD simple table (ID,…some data) PDO SQLite

HOME  2.Sept.2015 Download – see site_ver2.rar from my article 9.

Excellent code to learn object oriented programming, model-view-controller code separation (structured programming), single page application (require scripts, not URL them), most code outside Apache doc root.

1. Download sqlitestudio and install DDL below.

--*****************************************
-- sqlite 3
--*****************************************
/*
SELECT * FROM message ;
select * from zodiac ;
*/

/*
http://dev:8083/test/books/a01cookbook/tema.php
J:\dev_web\htdocs\test\books\a01cookbook\tema.php

sqlite db is: J:\dev_web\htdocs\test\books\a01cookbook\tema.sqlite
                                         
SELFJOIN TABLE :
   1. C INSERT frm data, 
   2. R DISPLAY tbl, row, 
   3. V VALIDATE, E set_exception_handler 
Not neccessarily here:
U UPDATE and D DELETE WITH J:\aplp\aplp\sqlitestudio\SQLiteStudio.exe

TEME (msgs-PORUKE I replays-ODGOVORI) SELFJOIN
TEME (msgs-PORUKE I replays-ODGOVORI) SELFJOIN
----------------------------------------------------------------
5 KEYS: id,thread_id,parent_id,level,thread_pos
----------------------------------------------------------------
1 1 0 0 0   TEMA1 (thread1) by ss @ 2015-03-25 00:41 (99 bytes)
2 1 1 1 1     funkcije by ss @ 2015-03-25 00:42 (242 bytes)
3 1 2 2 2       funkcija save() by ss @ 2015-03-25 00:52 (1335 bytes)
6 1 2 2 3       funkcija frm_post() by ss @ 2015-03-25 19:29 (303 bytes) 

4 2 0 0 0   TEMA2 CRUD šifrarnika sqlite3 by ss ...

5 3 0 0 0   TEMA3 MAPE web servera by ss ...
______________________________________________________________
Otvoriti novu temu (nit, thread)


*/

-- sqlite db: J:\dev_web\htdocs\test\books\a01cookbook\tema.sqlite
CREATE TABLE message (
  id          INTEGER PRIMARY KEY AUTOINCREMENT,
  posted_on   DATETIME NOT NULL,
  author      CHAR(255),
  subject     CHAR(255),
  body        MEDIUMTEXT,
  thread_id   INT UNSIGNED NOT NULL,
  parent_id   INT UNSIGNED NOT NULL,
  level INT   UNSIGNED     NOT NULL,
  thread_pos  INT UNSIGNED NOT NULL
);



--sqlite db: J:\dev_web\htdocs\test\books\a01cookbook\zodiac.sqlite
CREATE TABLE zodiac (
  id          INT UNSIGNED NOT NULL,
  sign        CHAR(11),
  symbol      CHAR(13),
  planet      CHAR(7),
  element     CHAR(5),
  start_month TINYINT,
  start_day   TINYINT,
  end_month   TINYINT,
  end_day     TINYINT,
  PRIMARY KEY(id)
)
;

INSERT INTO zodiac VALUES (1,'Aries','Ram','Mars','fire',3,21,4,19);
INSERT INTO zodiac VALUES (2,'Taurus','Bull','Venus','earth',4,20,5,20);
INSERT INTO zodiac VALUES (3,'Gemini','Twins','Mercury','air',5,21,6,21);
INSERT INTO zodiac VALUES (4,'Cancer','Crab','Moon','water',6,22,7,22);
INSERT INTO zodiac VALUES (5,'Leo','Lion','Sun','fire',7,23,8,22);
INSERT INTO zodiac VALUES (6,'Virgo','Virgin','Mercury','earth',8,23,9,22);
INSERT INTO zodiac VALUES (7,'Libra','Scales','Venus','air',9,23,10,23);
INSERT INTO zodiac VALUES (8,'Scorpio','Scorpion','Mars','water',10,24,11,21);
INSERT INTO zodiac VALUES (9,'Sagittarius','Archer','Jupiter','fire',11,22,12,21);
INSERT INTO zodiac VALUES (10,'Capricorn','Goat','Saturn','earth',12,22,1,19);
INSERT INTO zodiac VALUES (11,'Aquarius','Water Carrier','Uranus','air',1,20,2,18);
INSERT INTO zodiac VALUES (12,'Pisces','Fishes','Neptune','water',2,19,3,20);

2. Controller and view ~ 120 lines :

<?php
// http://dev:8083/test/books/a01cookbook/
// J:\dev_web\htdocs\test\books\a01cookbook\zodiac.php

    // ***********************************
    // 1. M O D E L - file where b u i l d_ q u e r y ( )  is defined
    // ***********************************
$fields = array(
   'sign'
 , 'symbol'
 , 'planet'
 , 'element'
 , 'start_month'
 , 'start_day'
 , 'end_month'
 , 'end_day'
);
$lbls = array(
    'Znak'
  , 'Simbol'
  , 'Planeta'
  , 'Element'
  , 'Od mjeseca'
  , 'Od dana'
  , 'Do mjeseca'
  , 'Do dana'
);

include __DIR__ . '/mdl.php';
$db = new PDO('sqlite:zodiac.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    // ***********************************
    // 2. C O N T R O L L E R
    // ***********************************
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : 'show';
switch ($cmd) 
{
   case 'edit':
       try {
          $st = $db->prepare('SELECT ' . implode(',',$fields) .
                             ' FROM zodiac WHERE id = ?');
          $st->execute(array($_GET['id']));
          $row = $st->fetch(PDO::FETCH_ASSOC);
       } catch (Exception $e) {
           $row = array();
       }
   case 'add':
           print '<form method="post" action="' .
                 htmlentities($_SERVER['PHP_SELF']) . '">';
           print '<input type="hidden" name="cmd" value="save">';
           
           print '<table>';
           
           if ('edit' == $cmd) {
               printf('<input type="hidden" name="id" value="%d">',
                      $_GET['id']);
           }
           foreach ($fields as $field) {
               if ('edit' == $cmd) {
                  $value = htmlentities($row[$field]);
               } else {
                   $value = '';
               }
               printf('<tr><td>%s: </td><td><input type="text" name="%s" value="%s">',
                      $field, $field, $value);
               printf('</td></tr>');
           }
           print '<tr>
                <td></td>
                <td><input type="submit" value="Save"></td>
              </tr>';
           
           print '</table></form>';
           break;
   case 'save':
           try {
             $st = build_query($db,'id',$fields,'zodiac');
             print 'Added info.';
           } catch (Exception $e) {
             print "Couldn't add info: " . htmlentities($e->getMessage());
           }
           print '<hr>';
   case 'show':
          // ***********************************
          // 3. V I E W - P A G I N A T O R
          // ***********************************
       default:
           $self = htmlentities($_SERVER['PHP_SELF']);
           
           /* without P A G I N A T O R :
           foreach ($db->query('SELECT id,sign FROM zodiac') as $row) {
               printf('<li> <a href="%s?cmd=edit&id=%s">%s</a>',
                      $self,$row['id'],htmlentities($row['sign']));
           }
           */
      $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 1;
      if (! $offset) { $offset = 1; }
      $per_page = 10;
      $total = $db->query('SELECT COUNT(*) FROM zodiac')->fetchColumn(0);
      $limitedSQL = 'SELECT * FROM zodiac ORDER BY id ' .
                    "LIMIT $per_page OFFSET " . ($offset-1);
      $lastRowNumber = $offset - 1;

           print '<a href="'.$self.'?cmd=add">Dodati redak</a><hr />';
           print '<ol>';
      foreach ($db->query($limitedSQL) as $row) {
          $lastRowNumber++;
          //print "{$row['sign']}"
          
          printf('<li> <a href="%s?cmd=edit&id=%s">%s</a>',
                      $self,$row['id'],htmlentities($row['sign']));
          echo ", {$row['symbol']} ({$row['id']}) <br/>\n";
      }
          print '</ol>';
      //
      indexed_links($total,$offset,$per_page);
      print "<br/>";
      print "(Prikazani retci $offset - $lastRowNumber od $total)";
           //
           break;
} // e n d 
 s w i t c h

3. Model- universal code ~ 70 lines :

 <?php
function build_query($db, $key_field, $fields, $table) {
    $values = array();
    if (! empty($_POST[$key_field])) {
        $update_fields = array();
        foreach ($fields as $field) {
            $update_fields[] = "$field = ?";
            // Assume data is coming from a form
            $values[] = $_POST[$field];
        }
        // Add the key field's value to the $values array
        $values[] = $_POST[$key_field];
        $st = $db->prepare("UPDATE $table SET " .
                   implode(',', $update_fields) .
                   "WHERE $key_field = ?");
    } else {
        // Start values off with a unique ID
        // If your DB is set to generate this value, use NULL instead
        $values[] = md5(uniqid());
        $placeholders = array('?');
        foreach ($fields as $field) {
            // One placeholder per field
            $placeholders[] = '?';
            // Assume the data is coming from a form
            $values[] = $_POST[$field];
        }
        $st = $db->prepare(
           "INSERT INTO $table ($key_field," .
              implode(',',$fields) . ') VALUES ('.
              implode(',',$placeholders) .')');
    }
    $st->execute($values);
    return $st;
}

function print_link($inactive,$text,$offset='') {
    if ($inactive) {
        print "<span class='inactive'>$text</span>";
    } else {
        print "<span class='active'>".
              "<a href='" . htmlentities($_SERVER['PHP_SELF']) .
              "?offset=$offset'>$text</a></span>";
    }
}

function indexed_links($total,$offset,$per_page) {
    $separator = ' | ';
    // 
    print_link($offset == 1, '<< Preth', max(1, $offset - $per_page));
    // print all groupings except last one
    for ($start = 1, $end = $per_page;
         $end < $total;
         $start += $per_page, $end += $per_page) {
             print $separator;
             print_link($offset == $start, "$start-$end", $start);
    }
    /* print the last grouping -
     * at this point, $start points to the element at the beginning
     * of the last grouping
     */
    /* the text should only contain a range if there's more than
     * one element on the last page. For example, the last grouping
     * of 11 elements with 5 per page should just say "11", not "11-11"
     */
    $end = ($total > $start) ? "-$total" : '';
    print $separator;
    print_link($offset == $start, "$start$end", $start);
    // 
    print $separator;
    print_link($offset == $start, 'Sljed >>',$offset + $per_page);
}

 

4. Multiple files upload OOP, namespaces & How to recognize mobile device – OOP, SPA, MVC domain style, PHP outside web doc root

HOME

Two scripts for multiple files upload – OOP, namespaces (see also article 10)

<?php
// 1. J:\zwamp64\vdrive\web\papl1\upload\index.php
use Classes\File\Upload; // = dir/subdir/classname 
                         // = namespace_name/clsname

// set the maximum upload size in bytes
$max = 10000 *      // 6 GB
        600 * 1024; // 600 KB
if (isset($_POST['upload'])) {
    // path to the upload folder : 
    //work: $destination = 'C:/upload_test/'; //or  __DIR__;
    $destination = DROOTPATH.DS.'FILE_TRANSFER'.DS; 
              // CONVENTION: below web doc.root
    require_once __DIR__.'/Classes/File/Upload.php'; // clsscript
    // -----------------------
    
    
    try {
        $loader = new Upload($destination);
        $loader->setMaxSize($max);
        $loader->allowAllTypes();
        $loader->upload();
        $result = $loader->getMessages();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Multiple Upload</title>
</head>

<body>
<?php
if (isset($result)) {
    echo '<ul>';
    foreach ($result as $message) {
        echo "<li>$message</li>";
    }
    echo '</ul>';
}
?>
<form action="" method="post" enctype="multipart/form-data">
  <p>
    <label for="image">Upload files (multiple selections permitted):
    </label>
    <input type="file" name="image[]" id="imageid" multiple>
  </p>
  <p>
    <input type="submit" name="upload" id="uploadid" value="Upload">
  </p>
</form>


<h3>Upload info ($_FILES)</h3>
<pre>
<?php if (isset($_POST['upload'])) 
{
  print_r($_FILES);
?>
Error levels in $_FILES array $err = $_FILES['file']['error'];
Error level Meaning
0 Upload successful $err = UPLOAD_ERR_OK
1 File exceeds maximum upload size specified in php.ini (default 2 MB)
  UPLOAD_ERR_INI_SIZE
  upload_max_filesize in php.ini eg 6400M (=6,4 GB)
  post_max_size = 64000M
  file_uploads = On  (see Local value in phpinfo -> Core section)
  upload_tmp_dir = "j:/wamp/tmp"  or whatever, default C:\Windows\Temp
  max_file_uploads = 20  for single request
  max_execution_time  60 sekundi<br />
  max_input_time  60 sek. (parse $_POST, $_GET, $_FILE arrays)
2 File exceeds size specified by MAX_FILE_SIZE in HTML form eg 6400M
  UPLOAD_ERR_FORM_SIZE
3 File only partially uploaded UPLOAD_ERR_PARTIAL
4 Form submitted with no file specified UPLOAD_ERR_NO_FILE
Error level 5 is currently not defined.
6 No temporary folder UPLOAD_ERR_NO_TMP_DIR (no destination_path)
7 Cannot write file to disk UPLOAD_ERR_CANT_WRITE (chmod 777)
8 Upload stopped by an unspecified PHP extension UPLOAD_ERR_EXTENSION
<?php } ?>
</pre>


<h3>$_POST parameters of this page</h3>

<table>
    <tr>
        <th>Parameter name</th>
        <th>Value</th>
    </tr>
    <?php 
     $count = 0; foreach ($_POST as $name => $value) { $count++ ?>
      <tr class="<?php echo $count % 2 == 0 ? 'alt' : ''; ?>">
        <td><?php echo htmlentities(stripslashes($name)) ?></td>
        <td><?php echo nl2br(htmlentities(stripslashes($value))) ?></td>
      </tr>
    <?php } 
echo '</table>';
?>

</body>
</html>
<?php
// 2. J:\zwamp64\vdrive\web\papl1\upload\Classes\File\Upload.php
namespace Classes\File; // dir/subdir
class Upload {

    protected $uploaded = [];
    protected $destination;
    protected $max = 51200;
    protected $messages = [];
    protected $permitted = [
        'image/gif',
        'image/jpeg',
        'image/pjpeg',
        'image/png'
    ];
    protected $typeCheckingOn = true;
    protected $notTrusted = ['bin', 'cgi', 'exe', 'js', 'pl', 'php'
           , 'py', 'sh'];
    protected $suffix = '.upload';
    protected $newName;
    protected $renameDuplicates;


    public function __construct($path) {
        if (!is_dir($path) || !is_writable($path)) {
            throw new \Exception("$path must be a valid
                 , writable directory.");
        }
        $this->destination = $path;
    }

    public function upload($renameDuplicates = true) {
        $this->renameDuplicates = $renameDuplicates;
        $uploaded = current($_FILES);
        if (is_array($uploaded['name'])) {
            // deal with multiple uploads
            foreach ($uploaded['name'] as $key => $value) {
                $currentFile['name'] = $uploaded['name'][$key];
                $currentFile['type'] = $uploaded['type'][$key];
                $currentFile['tmp_name'] = $uploaded['tmp_name'][$key];
                $currentFile['error'] = $uploaded['error'][$key];
                $currentFile['size'] = $uploaded['size'][$key];
                if ($this->checkFile($currentFile)) {
                    $this->moveFile($currentFile);
                }
            }
        } else {
            if ($this->checkFile($uploaded)) {
                $this->moveFile($uploaded);
            }
        }
    }

    public function getMessages() {
        return $this->messages;
    }

    public function getMaxSize() {
        return number_format($this->max/1024, 1) . ' KB';
    }

    public function setMaxSize($num) {
        if (is_numeric($num) && $num > 0) {
            $this->max = (int) $num;
        }
    }

    public function allowAllTypes($suffix = true) {
        $this->typeCheckingOn = false;
        if (!$suffix) {
            $this->suffix = '';  // empty string
        }
    }

    protected function checkFile($file) {
        $accept = true;
        if ($file['error'] != 0) {
            $this->getErrorMessage($file);
            // stop checking if no file submitted
            if ($file['error'] == 4) {
                return false;
            } else {
                $accept = false;
            }
        }
        if (!$this->checkSize($file)) {
            $accept = false;
        }
        if ($this->typeCheckingOn) {
            if (!$this->checkType($file)) {
                $accept = false;
            }
        }
        if ($accept) {
            $this->checkName($file);
        }
        return $accept;
    }

    protected function getErrorMessage($file) {
        switch($file['error']) {
            case 1:
            case 2:
                $this->messages[] = $file['name'] . ' is too big: (max: ' .
                    $this->getMaxSize() . ').';
                break;
            case 3:
                $this->messages[] = $file['name'] 
                     . ' was only partially uploaded.';
                break;
            case 4:
                $this->messages[] = 'No file submitted.';
                break;
            default:
                $this->messages[] = 
                   'Sorry, there was a problem uploading ' 
                   . $file['name'];
                break;
        }
    }

    protected function checkSize($file) {
        if ($file['error'] == 1 || $file['error'] == 2) {
            return false;
        } elseif ($file['size'] == 0) {
            $this->messages[] = $file['name'] . ' is an empty file.';
            return false;
        } elseif ($file['size'] > $this->max) {
            $this->messages[] = $file['name'] 
                     . ' exceeds the maximum size
                for a file (' . $this->getMaxSize() . ').';
            return false;
        } else {
            return true;
        }
    }

    protected function checkType($file) {
        if (in_array($file['type'], $this->permitted)) {
            return true;
        } else {
            if (!empty($file['type'])) {
                $this->messages[] = $file['name'] 
                    . ' is not permitted type of file.';
            }
            return false;
        }
    }

    protected function checkName($file) {
        $this->newName = null;
        $nospaces = str_replace(' ', '_', $file['name']);
        if ($nospaces != $file['name']) {
            $this->newName = $nospaces;
        }
        $extension = pathinfo($nospaces, PATHINFO_EXTENSION);
        if (!$this->typeCheckingOn && !empty($this->suffix)) {
            if (in_array($extension
                  , $this->notTrusted) || empty($extension)) {
                $this->newName = $nospaces . $this->suffix;
            }
        }
        if ($this->renameDuplicates) {
            $name = isset($this->newName) 
                       ? $this->newName : $file['name'];
            $existing = scandir($this->destination);
            if (in_array($name, $existing)) {
                // rename file
                $basename = pathinfo($name, PATHINFO_FILENAME);
                $extension = pathinfo($name, PATHINFO_EXTENSION);
                $i = 1;
                do {
                    $this->newName = $basename . '_' . $i++;
                    if (!empty($extension)) {
                        $this->newName .= ".$extension";
                    }
                } while (in_array($this->newName, $existing));
            }
        }
    }

    protected function moveFile($file) {
        $filename = isset($this->newName) 
                ? $this->newName : $file['name'];
        $success = move_uploaded_file($file['tmp_name']
              , $this->destination . $filename);
        if ($success) {
            $result = $file['name'] . ' was uploaded successfully to '
              . 'server directory : <br />'. $this->destination ;
            if (!is_null($this->newName)) {
                $result .= ', and was renamed ' . $this->newName;
            }
            $this->messages[] = $result;
        } else {
            $this->messages[] = 'Could not upload ' . $file['name'];
        }
    }
}

How to recognize mobile device – non OOP code

Excellent code to learn object oriented programming, model-view-controller code separation (structured programming), single page application (require scripts, not URL them), most code outside Apache doc root.

function findDevice() {
    $userAgent=strtolower($_SERVER['HTTP_USER_AGENT']);
$device=array('iphone','ipad','android','silk','blackberry', 'touch');
$deviceLength=count($device);

for($ii=0;$ii < $deviceLength;$ii ++) {
if(strstr($userAgent, $device[$ii])) {
return $device[$ii];
} else return 'desktop/laptop';
}
}

OOP, SPA, MVC domain style, PHP outside web doc root

Display ibrowser properties (view class methode):

  1. Device = desktop/laptop
  2. Browser = firefox
  3. userAgent = mozilla/5.0 (windows nt 6.3; win64; x64; rv:25.3) gecko/20150323 firefox/31.9 palemoon/25.3.1

Or display one property – returned from getter: Device = desktop/laptop

About OOP programs

  1. CODE INSIDE APACHE DOC ROOT (this page script) :  kod  edit  phpinfo
    INCLUDED CODE FROM OUTSIDE APACHE DOC ROOT :
  2. code behind this page script (model .pcls) :  kod  edit  phpinfo
  3. code config – set up :  kod  edit  phpinfo
  4. code helper (util) :  kod  edit  phpinfo
  5. /**
    * This page URL $idxurl = 
    *    http://dev:8083/inc/utl/get_ibrowser_device.php
    *   displays o u t p u t  o f  server script 
    *          $idx.DS.$idxscript =
    *   J:\dev_web\htdocs\inc\utl\get_ibrowser_device.php, 
    *           which contains :
    *      MODEL_fn_call from public fn __ c o n s t r u c t ( )
    *      protected_VIEW_fn_call from public fn
    *      CONTROLLER_code before c l a s s  C l i e n t
    *
    * server script i n c l u d e s scripts which are outside Apache doc root :
    *   1. config $confglob                : require_once('J:\dev_web\inc\confglob.php');
    *   2. helper (util) $edrun            : require_once('J:\dev_web\inc\utl\kod_edit_run.php');
    *   3. code behind (class) $codebehind : require_once('J:\dev_web\inc\utl\get_ibrowser_device.pcls');
    *
    * $confglob contains  P H P  s e t  u p :
    *    ini_set('display_errors','2');
    *    ERROR_REPORTING(E_ALL);
    **/

SCRIPTS:

  1. J:\dev_web\htdocs\inc\utl\get_ibrowser_device.php
    // 1. a d r e s s e s :
    if (!defined('DS')) define('DS',DIRECTORY_SEPARATOR);
    if (!defined('CONFGLOB_DIR')) define('CONFGLOB_DIR',
    realpath($_SERVER['DOCUMENT_ROOT'].'/../inc'));
    $apl = dirname(dirname(__DIR__)).DS.’htdocs’; // *** !!! *** ONLY YOU TO SET UP, used for link
    $idx = __DIR__ ; $idxscript = basename(__FILE__) ;// 2. i n c l u d e s :
    $confglob   = CONFGLOB_DIR.DS.’confglob.php’;
    $edrun      = CONFGLOB_DIR.DS.’utl’.DS.’kod_edit_run.php’;
    $codebehind = CONFGLOB_DIR.DS.’utl’.DS.’get_ibrowser_device.pcls’;
    require_once($confglob);   // c o n f i g
    require_once($edrun);      // h e l p e r
    require_once($codebehind); // m o d e l// 3. c o n t r o l l e r :
    $trigger = new Client(); // trigger becouse can direct output view// ************** e n d  c o n t r o l l e r  p r o g r a m// v i e w :
    ?>
    <!DOCTYPE html>
    <html lang=”hr”>
    <head>
    <title>Is mobile</title>
    <meta content=”text/html; charset=utf-8″; http-equiv=”content-type”>
    <!–base href=’/’–>
    <link rel=’stylesheet’ href=’lib/bootstrap/dist/css/bootstrap.min.css’ />
    <link rel=’stylesheet’ href=’src/bootstrap.min.css’ />
    </head>
    <body><h2>How to recognize mobile device – non OOP code</h2>
    <p><code><code><span style=”color:#000000; “><span style=”color:#007700; “>function </span><span style=”color:#0000BB; “>findDevice</span><span style=”color:#007700; “>() {<br>
    </span></span><code><span style=”color:#000000; “><span style=”color:#007700; “>    $</span><span style=”color:#0000BB; “>userAgent</span><span style=”color:#007700; “>=</span><span style=”color:#0000BB; “>strtolower</span><span style=”color:#007700; “>(</span><span style=”color:#0000BB; “>$_SERVER</span><span style=”color:#007700; “>[</span><span style=”color:#DD0000; “>’HTTP_USER_AGENT'</span><span style=”color:#007700; “>]);<br>
    </span><span style=”color:#FF8000; “>
    </span><span style=”color:#007700; “>$</span><span style=”color:#0000BB; “>device</span><span style=”color:#007700; “>=array(</span><span style=”color:#DD0000; “>’iphone'</span><span style=”color:#007700; “>,</span><span style=”color:#DD0000; “>’ipad'</span><span style=”color:#007700; “>,</span><span style=”color:#DD0000; “>’android'</span><span style=”color:#007700; “>,</span><span style=”color:#DD0000; “>’silk'</span><span style=”color:#007700; “>,</span><span style=”color:#DD0000; “>’blackberry'</span><span style=”color:#007700; “>, </span><span style=”color:#DD0000; “>’touch'</span><span style=”color:#007700; “>);<br>$</span><span style=”color:#0000BB; “>deviceLength</span><span style=”color:#007700; “>=</span><span style=”color:#0000BB; “>count</span><span style=”color:#007700; “>($</span><span style=”color:#0000BB; “>device</span><span style=”color:#007700; “>);<br>
    </span></span></code><span style=”color:#000000; “><span style=”color:#007700; “><br>
    for(</span><span style=”color:#0000BB; “>$ii</span><span style=”color:#007700; “>=</span><span style=”color:#0000BB; “>0</span><span style=”color:#007700; “>;</span><span style=”color:#0000BB; “>$ii </span><span style=”color:#007700; “>&lt; $</span><span style=”color:#0000BB; “>deviceLength</span><span style=”color:#007700; “>;</span><span style=”color:#0000BB; “>$ii </span><span style=”color:#007700; “>++) {<br>
    if(</span><span style=”color:#0000BB; “>strstr</span><span style=”color:#007700; “>($</span><span style=”color:#0000BB; “>userAgent</span><span style=”color:#007700; “>, $</span><span style=”color:#0000BB; “>device</span><span style=”color:#007700; “>[</span><span style=”color:#0000BB; “>$ii</span><span style=”color:#007700; “>])) {<br>
    </span><span style=”color:#FF8000; “>        </span><span style=”color:#007700; “>return $</span><span style=”color:#0000BB; “>device</span><span style=”color:#007700; “>[</span><span style=”color:#0000BB; “>$ii</span><span style=”color:#007700; “>];<br>
    } else return </span><span style=”color:#DD0000; “>’desktop/laptop'</span><span style=”color:#007700; “>;<br>
    }<br>
    }</span></span></code></code></p>
    <h2>OOP, SPA, MVC domain style, PHP outside web doc root</h2>
    <p>Display ibrowser properties (view class methode):<br />
    <?php $trigger -> out_vew_ibrowse_params() ; ?>
    </p>

    <p>Or display one property – returned from getter: Device =
    <?php echo $trigger->get_device(); ?> </p>

    <h2>About OOP programs</h2>
    <ol>
    <li>CODE INSIDE APACHE DOC ROOT (this page script) :
    <?php kod_edit_run(
    $idx       // script_dir
    , $idxscript // script
    , MDURL); ?>
    <br>
    <br>
    INCLUDED CODE FROM OUTSIDE APACHE DOC ROOT : <br>
    <li>code behind this page script (model .pcls) :
    <?php kod_edit_run(
    dirname($codebehind)  // script_dir
    , basename($codebehind) // script
    , MDURL); ?>
    <li> code config – set up :
    <?php kod_edit_run(
    dirname($confglob)  // script_dir
    , basename($confglob) // script
    , MDURL); ?>
    <li>code helper (util) :
    <?php kod_edit_run(
    dirname($edrun)  // script_dir
    , basename($edrun) // script
    , MDURL); ?>
    <li> <pre>/**
    * This page URL $idxurl = <a href=”<?php echo $idxurl; ?>”><?php echo $idxurl; ?><a>
    *   displays o u t p u t  o f  server script $idx.DS.$idxscript =
    *   <?php echo $idx.DS.$idxscript; ?>, which contains :
    *      MODEL_fn_call from public fn __ c o n s t r u c t ( )
    *      protected_VIEW_fn_call from public fn
    *      CONTROLLER_code before c l a s s  C l i e n t
    *
    * server script i n c l u d e s scripts which are outside Apache doc root :
    *   1. config $confglob                : require_once(‘<?php echo $confglob; ?>’);
    *   2. helper (util) $edrun            : require_once(‘<?php echo $edrun; ?>’);
    *   3. code behind (class) $codebehind : require_once(‘<?php echo $codebehind; ?>’);
    *
    * $confglob contains  P H P  s e t  u p :
    *    ini_set(‘display_errors’,’2′);
    *    ERROR_REPORTING(E_ALL);
    **/ </pre>
    </ol>

    <?php
    class Client
    {
    private $IbrowserProp;

    // M O D E L :
    public function __construct()   {
    $this->IbrowserProp = new getIbrowserProp();
    } // e n d  p u b l i c  f n  _ _c o n s t r u c t ( )

    public function get_device() {
    return  $this->IbrowserProp->findDevice() ;
    } // e n d

    // V I E W :
    public function out_vew_ibrowse_params() {
    $this->vew_ibrowse_params();
    } // e n d
    protected function vew_ibrowse_params()  // public private protected
    {
    echo ‘<ol>’;
    echo ‘<li>Device = ‘    . $this->IbrowserProp->findDevice() . ‘<br/>’;
    echo ‘<li>Browser = ‘   . $this->IbrowserProp->findBrowser() . ‘<br/>’;
    echo ‘<li>userAgent = ‘ . $this->IbrowserProp->getUserAgent() . ‘<br/>’;
    echo ‘</ol>’;
    } // e n d  p u b l i c  f n  _ _c o n s t r u c t ( )

    } // e n d  c l a s s  C l i e n t

    ?>
    </body>
    </html>

  2. J:\dev_web\inc\utl\get_ibrowser_device.pcls
    <?php
    ini_set("display_errors","2");
    ERROR_REPORTING(E_ALL);
    //User agent as property of object
    class getIbrowserProp
    {
    private $userAgent;
    private $device;
    private $browser;
    private $deviceLength;
    private $browserLength;
    public function __construct()
    {
    $this->userAgent=strtolower($_SERVER[‘HTTP_USER_AGENT’]);
    //$this->userAgent=strtolower($this->userAgent);$this->device=array(‘iphone’,’ipad’,’android’,’silk’,’blackberry’, ‘touch’);
    $this->browser= array(‘firefox’,’chrome’,’opera’,’msie’,’safari’,’blackberry’,’trident’);
    $this->deviceLength=count($this->device);
    $this->browserLength=count($this->browser);
    }
    public function findDevice()
    {
    for($ii=0;$ii < $this->deviceLength;$ii ++)
    {
    if(strstr($this->userAgent,$this->device[$ii]))
    {
    //$device = $this->device[$ii];
    //if (is_null($device)) $device = ‘desktop/laptop’;
    //return $device;
    return $this->device[$ii];
    } else return ‘desktop/laptop’;
    }
    }//public function get_device() {
    //    return $this->vew_ibrowse_params();
    //} // e n dpublic function findBrowser()
    {
    for($ii=0;$ii < $this->browserLength;$ii ++)
    {
    if(strstr($this->userAgent,$this->browser[$ii]))
    {
    return $this->browser[$ii];
    }
    }
    }
    public function getUserAgent()
    {
    return($this->userAgent);
    }
    }?>
  3. J:\dev_web\inc\confglob.php
    <?php
    // J:\dev_web\inc\confglob.php - NOT WEB ACCESSIBLE
    //    - SAME LEVEL AS APACHE DOC ROOT
    ini_set("display_errors","2");
    ERROR_REPORTING(E_ALL);
    $md=realpath($_SERVER[‘DOCUMENT_ROOT’]);// 1. rel.adresses are ok for both  p a t h s  &   u r l s :
    $idxrel = str_replace($md,”, $idx); // str_replace(DS,’/’,
    $aplrel = str_replace($md,”, $apl); // str_replace(DS,’/’,// 2. u r l s  – s u b a p l,  a p l,  m d (main doc.root = Apache doc.root):
    $mdurl =’http://’.$_SERVER[“SERVER_NAME”].’:’.$_SERVER[“SERVER_PORT”];
    if (!defined(‘MDURL’)) define(‘MDURL’, $mdurl);
    $idxurl = $mdurl.str_replace(DS,’/’,$idxrel).’/’.$idxscript;
    $aplurl = $mdurl.’/’.substr(str_replace(DS,’/’,$aplrel),1);
    $imgurl = $mdurl.’/inc/img’;
  4. J:\dev_web\inc\utl\kod_edit_run.php
    <?php
    function kod_edit_run($script_dir_path, $script_name, $web_docroot_url)
    {
    $ds = DIRECTORY_SEPARATOR;
    echo <<< EOKOD
    <a href="$web_docroot_url/inc/utl/showsource.php
    ?file=$script_dir_path$ds$script_name
    &line=1&prev=10000&next=10000
    "  target="_blank">&nbsp;kod</a>
    <a href=”$web_docroot_url/inc/utl/edservertxt.php
    ?file=$script_dir_path$ds$script_name
    ” target=”_blank”>&nbsp;edit</a><a href=”$web_docroot_url/phpinfo_inc.php
    ” target=”_blank”>&nbsp;phpinfo</a>
    EOKOD;
    /* call it so:
    kod_edit_run(
    $idx       // script_dir_path
    , $idxscript // script_name
    , MDURL);    // web_docroot_url = (Apache) web server URL
    */
    }