Inserimento di un header, nav, footer php

Jakub Lemiszewski

Utente Attivo
5 Dic 2013
119
1
0
Salve ho dei problemi nei seguenti codici a capire dove inserire un header, footer e navizione e in che modo. Ho provato diverse cose ma sono bloccato. Spero che mi possiate aiutare a capire. Grazie.
index.php
PHP:
//Error reporting
error_reporting( E_ALL );
ini_set ("display_errors", 1);
//END
include_once "models/model.class.php"; //Model class	
include_once "models/view.class.php"; //View class 	
include_once "controllers/controller.class.php"; // Controller class -> a glue 
include_once "models/page.class.php"; //Page class constructor

// $_GET $_POST $_COOKIE
$request = array_merge($_GET, $_POST);
// Controller
$controller = new Controller($request);
// 
$page = new Page (); // new page title default Forum
$page->link (array ('styles/main.css', 'styles/second.css')); // Stylesheet link array
//echo ;
echo $page->display ($controller->display());
page.class.php
PHP:
<?php
define ('SITE_NAME', 'FORUM');
define ('BASE', str_replace($_SERVER['SERVER_NAME'], '', $_SERVER['DOCUMENT_ROOT'])); // The Base Root Directory Name
define ('BASE_URI', $_SERVER['DOCUMENT_ROOT'] . '/'); // The HTML Directory Name
define ('BASE_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/'); // Website's Full URL Address
define ('DOCTYPE', 'xhtml strict');

class Page {

  private $title = '';
  private $description = '';
  private $keywords = '';
  private $robots = true;
  private $doctype = '';
  private $xhtml = true;
  private $charset = 'utf-8';
  private $include = array();
  private $jquery = array();
  private $body = '';

  function __construct ($title='') {
    if (!empty($title)) {
      $this->title = $title;
    } elseif (defined('SITE_NAME')) {
      $this->title = SITE_NAME;
    }
    if (defined('DOCTYPE')) {
      list($type, $standard) = explode(' ', DOCTYPE);
      $this->doctype ($type, $standard);
    } else {
      $this->doctype ('xhtml', 'strict');
    }
  }

  public function doctype ($type='html', $standard='strict') {
    if (in_array($standard, array('strict', 'transitional', 'frameset'))) {
      if ($type == 'html') {
        $this->xhtml = '';
        switch ($standard) {
          case 'strict': $this->doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'; break;
          case 'transitional': $this->doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'; break;
          case 'frameset': $this->doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'; break;
        }
      } elseif ($type == 'xhtml') {
        $this->xhtml = ' /';
        switch ($standard) {
          case 'strict': $this->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; break;
          case 'transitional': $this->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; break;
          case 'frameset': $this->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'; break;
        }
      }
    }
  }

  public function access ($user, $level=1) { 
    switch ($user) {
      case 'users':
        if (!isset($_SESSION['user_id'])) $this->eject('sign_in/');
        break;
      case 'admin':
        if (!isset($_SESSION['admin']) || $_SESSION['admin'] == 0 || $_SESSION['admin'] > $level) $this->eject();
        break;
      case 'others':
        if (isset($_SESSION['user_id'])) $this->eject();
        break;
    }
  }

  public function eject ($where='', $msg='') {
    if (stristr($where, BASE_URL)) {
      $url = $where;
    } else {
      $url = BASE_URL . $where;
    }
    if (ob_get_length()) ob_end_clean();
    if (empty($msg)) {
      $url = str_replace('&amp;', '&', $url);
      header("Location: $url");
    } else {
      echo '<script type="text/javascript"> var msg = confirm("' . str_replace(array('<br />', '<br>'), "\\n", addslashes($msg)) . '"); if (msg == true) { window.location = "' . $url . '"; } else { window.location = "' . $url . '"; } </script>';
      echo $msg . '<br /><br /><a href="' . $url . '">Click here to continue.</a>';
    }
    exit; 
  }

  public function title ($title='') {
    if (!empty($title)) $this->title = $title;
    return $this->title;
  }

  public function description ($description) {
    $this->description = $description;
  }

  public function keywords ($keywords) {
    $this->keywords = $keywords;
  }

  public function robots ($robots) {
    if (is_bool($robots)) $this->robots = $robots;
  }

  public function charset ($charset) {
    $this->charset = $charset;
  }

  public function link ($link, $prepend=true) {
    if (!is_array($link)) $link = array($link);
    if ($prepend) {
      $this->include = array_merge($link, $this->include);
    } else {
      foreach ($link as $value) $this->include[] = $value;
    }
  }

  public function jquery ($code, $oneliner=true) {
    if ($oneliner) $code = $this->oneliner($code);
    $this->jquery[] = $code;
  }
  
  public function body ($body) {
    $this->body = $body;
  }

  public function url ($action='', $url='', $key='', $value=NULL) {
    $protocol = ($_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
    if (empty($url)) $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    if ($action == 'ampify') return $this->ampify($url);
    $url = str_replace ('&amp;', '&', $url);
    if (empty($action) && empty($key)) { // clean the slate
      $url = explode('?', $url);
      return $url[0]; // no amps to convert
    }
    $fragment = parse_url ($url, PHP_URL_FRAGMENT);
    if (!empty($fragment)) {
      $fragment = '#' . $fragment; // to add on later
      $url = str_replace ($fragment, '', $url);
    }
    if ($key == '#') {
      if ($action == 'delete') $fragment = '';
      elseif ($action == 'add') $fragment = '#' . urlencode($value);
      return $this->ampify($url . $fragment);
    }
    $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
    $url = substr($url, 0, -1);
    $value = urlencode($value);
    if ($action == 'delete') {
      return $this->ampify($url . $fragment);
    } elseif ($action == 'add') {
      if (strpos($url, '?') === false) {
        return ($url . '?' . $key . '=' . $value . $fragment); // no amps to convert
      } else {
        return $this->ampify($url . '&' . $key . '=' . $value . $fragment);
      }
    }
  }

  public function display ($content='') {
    $html = '';
    $type = ($this->xhtml) ? 'xhtml' : 'html';
    $frameset = false;
    if (strpos($content, '<frame ') !== false) { // Then this is a frameset ...
      $frameset = true;
      $this->doctype($type, 'frameset');
    } elseif (strpos($this->doctype, 'frameset') !== false) { // If we're here then it's not ...
      $this->doctype($type, 'transitional');
    }
    $html .= $this->doctype . "\n";
    $html .= '<html';
    if ($this->xhtml) $html .= ' lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"';
    $html .= '>' . "\n";
    $html .= '<head>' . "\n";
    $html .= $this->meta_tags();
    $html .= $this->include_scripts();
    $html .= '</head>' . "\n";
    $html .= ($frameset) ? '<frameset' : '<body';
    if (!empty($this->body)) $html .= ' ' . $this->body;
    $html .= '>';
    $html .= "\n  " . trim($content);
    $html .= ($frameset) ? "\n</frameset>" : "\n</body>";
    $html .= "\n</html>";
    if (!$this->xhtml) $html = str_replace(' />', '>', $html);
    return $html;
  }

  private function meta_tags () {
    $tags = '  <meta http-equiv="content-type" content="text/html; charset=' . $this->charset . '" />' . "\n";
    $tags .= '  <title>' . $this->title . '</title>' . "\n";
    $description = (!empty($this->description)) ? $this->description : $this->title;
    $keywords = (!empty($this->keywords)) ? $this->keywords : $this->title;
    $tags .= '  <meta name="description" content="' . $description . '" />' . "\n";
    $tags .= '  <meta name="keywords" content="' . $keywords . '" />' . "\n";
    if (!$this->robots) $tags .= '  <meta name="robots" content="noindex, nofollow" />' . "\n";
    return $tags;
  }

  private function include_scripts () {
    $scripts = $this->combine_scripts($this->sort_scripts($this->include));
    if (!empty($this->jquery)) {
      $this->jquery = array_unique($this->jquery);
      $scripts .= '  <script type="text/javascript">$(document).ready(function(){' . "\n  ";
      $scripts .= implode("\n  ", $this->jquery);
      $scripts .= "\n  })</script>\n";
    }
    return $scripts;
  }
  
  private function sort_scripts ($array) { // used in $this->include_scripts()
    $array = array_unique($array);
    $scripts = array();
    foreach ($array as $script) {
      $parts = explode('.', $script);
      $ext = array_pop($parts);
      $name = implode('.', $parts);
      switch ($ext) {
        case 'ico': $scripts['ico'] = $script; break;
        case 'css': $scripts['css'][] = $name; break;
        case 'js': $scripts['js'][] = $name; break;
        default: $scripts['other'][] = $script; break;
      }
    }
    return $scripts;
  }
  
  private function combine_scripts ($sorted) { // used in $this->include_scripts()
    if (empty($sorted)) return '';
    $scripts = array();
    if (isset($sorted['ico'])) {
      $scripts[] = '<link rel="shortcut icon" type="image/x-icon" href="' . $sorted['ico'] . '" />';
    }
    if (isset($sorted['css'])) {
      foreach ($sorted['css'] as $script) {
        $scripts[] = '<link rel="stylesheet" type="text/css" href="' . $script . '.css" />';
      }
    }
    if (isset($sorted['js'])) {
      foreach ($sorted['js'] as $script) {
        $scripts[] = '<script type="text/javascript" src="' . $script . '.js"></script>';
      }
    }
    if (isset($sorted['other'])) $scripts = array_merge($scripts, $sorted['other']);
    return '  ' . implode("\n  ", $scripts) . "\n";
  }
  
  private function oneliner ($code) {
    return preg_replace('/\s(?=\s)/', '', str_replace(array("\r\n", "\r", "\n"), ' ', $code));
  }

  private function ampify ($string) { // used in $this->url
    return str_replace(array('&amp;', '&'), array('&', '&amp;'), $string);
  }

}
view.class
PHP:
<?php
class View{

	// 
	private $path = 'templates';
	// 
	private $template = 'default';

	//
	private $_ = array();

	//
	public function assign($key, $value){
		$this->_[$key] = $value;
	}


	public function setTemplate($template = 'default'){
		$this->template = $template;
	}

	public function loadTemplate(){
		$tpl = $this->template;
		// 
		$file = $this->path . DIRECTORY_SEPARATOR . $tpl . '.php';
		$exists = file_exists($file);

		if ($exists){
			
			ob_start();

			include $file;
			$output = ob_get_contents();
			ob_end_clean();
				
			return $output;
		}
		else {
			return 'could not find template';
		}
	}
}
controll.class
PHP:
<?php
class Controller{

	private $request = null;
	private $template = '';
	private $view = null;

	
	public function __construct($request){
		$this->view = new View();
		$this->request = $request;
		$this->template = !empty($request['view']) ? $request['view'] : 'default';
	}

	public function display(){
		//$view = new View();
		switch($this->template){
			case 'entry':
				$this->view->setTemplate('entry');
				$entryid = $this->request['id'];
				$entry = Model::getEntry($entryid);
				$this->view->assign('title', $entry['title']);
				$this->view->assign('content', $entry['content']);
				break;
				
			case 'default':
			default:
				$entries = Model::getEntries();
				$this->view->setTemplate('default');
				$this->view->assign('entries', $entries);
		}
		
		return $this->view->loadTemplate();
	}
}
model.class
PHP:
<?php
//
class Model{

	//
	private static $entries = array(
		array("id"=>0, "title"=>"1", "content"=>"first entry"),
		array("id"=>1, "title"=>"2", "content"=>"second entry"),
		array("id"=>2, "title"=>"3", "content"=>"fird entry")
	);

	//
	public static function getEntries(){
		return self::$entries;
	}

	//
	public static function getEntry($id){
		if(array_key_exists($id, self::$entries)){
			return self::$entries[$id];
		}else{
			return null;
		}
	}
}
 
Discussioni simili
Autore Titolo Forum Risposte Data
K inserimento immagini tra header e pagine WordPress 4
Dexter_90 [AIUTO!] Problema Inserimento Menu Fireworks nell'Header WordPress 11
D Inserimento video mp4 HTML e CSS 0
L inserimento form dati multipli ? PHP 0
G Appicazione HTML per inserimento dai in Database Access Microsoft HTML e CSS 0
J Form inserimento dati in database Ajax 1
D modificare questo codice per inserimento in text e non in tabella jQuery 1
R [C#] Automatizzare un inserimento di un Post su un Gruppo Facebook .NET Framework 0
M Aiuto con inserimento immagini WordPress 6
elpirata Impedire inserimento data di oggi e date passate jQuery 39
A Inserimento dati nel database tramite form + altre operazioni PHP 18
W Email conferma inserimento Classic ASP 0
S Inserimento multiplo non richiesto PHP 2
P inserimento icone social tramite html HTML e CSS 1
L form multipla php sql,errore in inserimento MySQL 0
Alex_70 Inserimento dati a cascata PHP 204
T Da xsd a xml ed inserimento dati in excel XML 0
M Problema inserimento parole con apostrofo nel db PHP 5
C [RISOLTO]Inserimento variabile php in input html PHP 20
P Access Inserimento data. MS Access 4
L Inserimento dettagli in una maglia Photoshop 2
S [PHP] email con inserimento dati nel database PHP 23
beatle [Photoshop] problema inserimento immagini Photoshop 1
G [Javascript] Errore inserimento dati Backend Node.js e workbench Javascript 1
B [PHP] Creare PDF dopo inserimento dati form PHP 4
C [PHP] Form con Inserimento dati dalla maschera e un menù a discesa che prende i dati dal db PHP 1
C [PHP] Form inserimento più menù a discesa PHP 9
M [PHP] Problemi su inserimento array nel db PHP 7
gandalf1959 [PHP] Inserimento di più righe non funziona come mi aspetto... PHP 2
E Inserimento dati da PHP in tabella MySQL PHP 5
E Form inserimento dati con JavaScript Javascript 0
D [MS Access] problemi con inserimento campo in una maschera MS Access 6
G inserimento csv in tabella mysql; problema con struttura PHP 11
M [Joomla] Inserimento wow.js in template Joomla 4
K [PHP] Inserimento dati database con postgres PHP 2
K [PHP + MYSQL ] Inserimento dati in database da form dinamico PHP 13
A [PHP] Inserimento url dinamici in pagina html PHP 3
spider81man [PHP] Alert Box per confermare Inserimento o Cancellazione dato. PHP 4
gandalf1959 [MySQL] Inserimento multiplo da form multirighe MySQL 22
A [RISOLTO]Inserimento Immagini da pc a MySql PHP 15
M Inserimento dati checkbox multipli in db da ajax a php PHP 1
S [PHP] inserimento su DB da tabella PHP 29
P [PHP] Problema inserimento nuove chiavi in array PHP 2
paloppa Inserimento data su database MYSQL PHP 2
webimage [PHP] Non inserimento in tabella PHP 19
N [Java] codice per inserimento sql Java 0
T [HTML] inserimento nav HTML e CSS 8
G [HTML] Problemi con inserimento immagini HTML e CSS 7
C [HTML] Inserimento feed/rss di Mr.Webmaster sul proprio sito HTML e CSS 1
M Inserimento Array prelevato da Database in Php in un altra tabella mysql PHP 0

Discussioni simili