In the example above, we are using “dynamically added data”, which you will see below.
controller
public function index()
{
$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
$data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}
$data['header'] = $this->load->view('common/header',$data);
$data['posts'] = $this->home_model->find_active(3);
$data['content'] = $this->load->view('common/content',$data);
$this->load->view('layout',$data);
}
view
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="DBlog : Material Style Personal Blog Theme from Wowbootstrap">
<meta name="author" content="wowbootstrap.com">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,700|Merriweather:400,400italic,700italic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,500,400italic,700,700italic,900,900italic,300,300italic,500italic" rel="stylesheet" type="text/css">
<title><?php echo $page_title?> - CI Blog</title>
</head>
<body>
<div id="wrapper">
<?php echo $header;?>
<section class="main">
<div class="container">
<div class="row">
<div class="col-md-8">
<?php //echo $content;?>
</div>
<div class="col-md-4">
<?php //echo $right_sidebar;?>
</div>
</div>
</div>
</section>
<?php //echo $footer;?>
</div>
<script>
$.material.init();
</script>
<?php if(!empty($home_page)):?>
<script type="text/javascript">
$(window).scroll(function(){
if ($(this).scrollTop() > 1){
$('.top-navbar').removeClass('navbar-transparent');
}else{
$('.top-navbar').addClass('navbar-transparent');
}
});
</script>
<?php endif;?>
<script type="text/javascript">
$(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</body>
</html>
I am using codeigniter to develop php web site. Now I have the question.
In controller,$this->load->view('index', $data);
I want $data can also share in header.php that included in index.php.
How to do???
If i do templating on codeigniter I do like this
In Layout.php
<!--Header -->
<?php $this->load->view('path/to/header'); ?>
<!--Content -->
<?php $this->load->view($content); ?>
<!--Footer-->
<?php $this->load->view('path/to/footer'); ?>
In controller
$data['content'] = 'path/to/content/page'; # or any dynamic data with $this->model...
$this->load->view('template',$data);
In Your Controller : to load view file like this,
public function index()
{
$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
$data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}
$data['posts'] = $this->home_model->find_active(3);
// To load view files
$this->load->view('common/header',$data); // header view file
$this->load->view('common/content',$data); // body content view file
$this->load->view('layout',$data); // footer view file
}
Related
I need help for something please. I'm working on my structure, but I've some problems...
I created a page called "page-container.php" and the content is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<?php if(isset($sidebar)): ?>
<div class="main-container">
<div class="main-content">
<?php endif; ?>
<h1 class="page-title"><?= $page_title; ?></h1>
I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES
</div>
</div>
</body>
</html>
You can see the "I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES".
What I mean is that when I write the content in my other pages (for example, my registration page), I want that all the content is placed at this location, like this:
http://prntscr.com/a3oz5k
All my other pages are like this (exemple with index.php):
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
If I want to write some content in this page, I need to do like this:
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
<div id="mycontent">
<p>Heellloo</p>
</div>
But look the result: http://prntscr.com/a3p32y
In the code source this is not normal... http://prntscr.com/a3p3kg
My content is below the html and body tag and I want it to be placed just below my h3 title. How can I do please?
Simple example using ob_start(), ob_get_clean().
<?php
ob_start();
?>
<div id="mycontent">
<p>Hello</p>
</div>
<?php
$page = ob_get_clean();
require "includes/page-container.php";
?>
And then in page-container.php just do echo $page;.
Your code will be still formatted as HTML in your editor instead if it would be in a variable.
You can also split your page-container.php into two parts and include the first part before the content and the second one after.
Reference: ob_start(), ob_get_clean()
You can keep another vartiable say $page_content and use as below :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<h1 class="page-title"><?= $page_title; ?></h1>
<?= $page_content; ?>
</div>
</div>
</body>
</html>
And for some content in another page
<?php
$page_title = "Home";
$page_id = "home";
$page_content = "content";
?>
<?php require "includes/page-container.php"; ?>
`
regarding working control flow if the page has 2 html tags
<html>
.....
</html>
<html>
.....
</html>
WHICH WOULD BE EXECUTED BOTH OR THE ONLY FIRST ONE....
I am working on a cs50 project in which when i call
<?php
dump($_SERVER);
render("login_form.php", ["title" => "Log In"]);
?>
only dumps get executed whereas when
<?php
render("login_form.php", ["title" => "Log In"]);
dump($_SERVER);
?>
both gets executed
the details of dump and render function are..
function render($template, $values = [])
{
// if template exists, render it
if (file_exists("../templates/$template"))
{
// extract variables into local scope
extract($values);
// render header
require("../templates/header.php");
// render template
require("../templates/$template");
// render footer
require("../templates/footer.php");
}
// else err
else
{
trigger_error("Invalid template: $template", E_USER_ERROR);
}
}
function dump
function dump($variable)
{
require("../templates/dump.php");
exit;
}
the templates file are
dump.php
<!DOCTYPE html>
<html>
<head>
<title>dump</title>
</head>
similar are header.php
<!DOCTYPE html>
<head>
<link href="/css/bootstrap.min.css" rel="stylesheet"/>
<link href="/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="/css/styles.css" rel="stylesheet"/>
<?php if (isset($title)): ?>
<title>Mobi3: <?= htmlspecialchars($title) ?></title>
<?php else: ?>
<title>Mobi3</title>
<?php endif ?>
<script src="/js/jquery-1.10.2.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/scripts.js"></script>
</head>
<body>
<div class="container">
<div id="top">
<img alt="C$50 Finance" src="/img/logo.gif"/>
</div>
<div id="middle">
<body>
<pre><?php print_r($variable); ?></pre>
</body>
footer.php
<div id="bottom">
Copyright © M3shop
</div>
</div>
</body>
If the answer is "having two sets of html tags" you are asking the wrong question. Regardless on which one is shown (which can also vary from browser to browser).
Ok I may not have the best title , but I will try to give a better explanation.
Let's assume you are using PHP include() to structure your website:
Header.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name=keywords content="somthiefn"/>
<title>Website</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script src="Scripts/jquery-1.8.3.min.js"></script>
<link rel="icon" type="image/png" href="/images/favicon.ico" />
</head>
<body>
Footer.php
</body>
</html>
Then a sample page:
Index.php
<!--Header-->
<?php include ('includes/header.php'); ?>
<div class="content">
<!-- some content-->
</div>
<!--Footer-->
<?php include ('includes/footer.php'); ?>
Basically I just want to know if there is a way to load some script into my header section.
I would like to achieve something like ASP.NET and its master Page, where I can just add content the header. for example
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/somescript.js"></script>
</asp:Content>
Yes, you can do like this:
index.php
<?php
// Wanted page title
$title = "some";
// JS Files..
$scripts = array();
$scripts[] = '<script src="js/some.js" />';
$scripts[] = '<script src="js/some2.js" />';
$scripts[] = '<script src="js/some3.js" />';
// Include header
include ('includes/header.php');
header.php
<title><?php echo $title ?></title>
<?php echo implode("\n",$scripts) ?>
Of course the variables could be named as you want and contain any data. Main thing is that you can pass them between files like i showed.
In index.php, before you include header.php, you could set an array for your scripts like:
header.php
<?php if(!isset($scripts)) $scripts = array(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name=keywords content="somthiefn"/>
<title>Website</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script src="Scripts/jquery-1.8.3.min.js"></script>
<link rel="icon" type="image/png" href="/images/favicon.ico" />
<!-- Include dynamic scripts-->
<?php foreach($scripts in $script): ?>
<script src="<?php echo $script; ?>"></script>
<?php endforeach;?>
</head>
<body>
index.php
<?php
$scripts = array('Scripts/somescript.js', 'http://server.com/path/anoterscript.js);
?>
<!--Header-->
<?php include ('includes/header.php'); ?>
<div class="content">
<!-- some content-->
</div>
<!--Footer-->
<?php include ('includes/footer.php'); ?>
Do you want to add PHP code to the header? Then you can add your code between tags (or tags if short tags are enabled), but I would consider to just call an include between the PHP tags and have the logic in that include.
Another option could be a template engine, e. g. Smarty. In most of the templates engine you can define your own functions and call them from the templates.
You could do this:
// content.php
<?php
$head = "<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<meta name='keywords' content='somthiefn' />
<title>Website</title>
<link type='text/css' rel='stylesheet' href='css/style.css' />
<script src='Scripts/jquery-1.8.3.min.js'></script>
<link type='image/png' rel='icon' href='images/favicon.ico' />
</head>
<body>";
$foot = "\n<body>\n</html>";
?>
// index.php
<?php
include 'includes/content.php';
$dom = new DOMDocument; #$dom->loadHTML($head);
$hd = $dom->getElementsByTagName('head'); $hd = $hd->item(0);
$script = $dom->creatElement('script');
$scriptAttr = $dom->createAttribute('src');
$scriptAttr->value= 'Scripts/somescript.js'; $script->appendChild($scriptAttr);
$hd->appendChild($script);
echo $dom->saveHTML().$foot;
?>
The other option is to use variables to separate your code then only echo portions. That is what I would do. It's technologically faster. Try this:
// content.php
<?php
$headTop = "<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<meta name='keywords' content='somthiefn' />
<title>Website</title>
<link type='text/css' rel='stylesheet' href='css/style.css' />
<script src='Scripts/jquery-1.8.3.min.js'></script>\n ";
$headBottom = "\n <link type='image/png' rel='icon' href='images/favicon.ico' />
</head>
<body>";
$foot = "\n<body>\n</html>";
?>
// index.php
<?php
include 'includes/content.php';
echo "$headTop<script src='Scripts/somescript.js'></script>$headBottom$foot";
?>
You can see why you would use the second option.
This works for me. It dynamically loads title, scripts and styles.
header.php
<?php
if(!isset($scripts))
$scripts = array();
if(!isset($styles))
$styles = array();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
<link rel="stylesheet" href="css/somestyle.css" type="text/css">
<script src=somescript.js"></script>
<!-- include styles -->
<?php foreach($styles as $style): ?>
<link href="<?php echo $style; ?>" rel="stylesheet">
<?php endforeach; ?>
<!-- include scripts-->
<?php foreach($scripts as $script): ?>
<script src="<?php echo $script; ?>"></script>
<?php endforeach;?>
</head>
<body>
index.php
<?php
$title = "some dynamic title";
$scripts = array('js/somescript.js', 'http://server.com/anoterscript.js');
$styles = array('css/somestyle.css', 'css/anotherstyle.css');
require_once ('header.php');
?>
<div class="content">
<!-- some content-->
</div>
<?php require_once('footer.php'); ?>
I just installed CodeIgniter 2.1.4. I want to call several functions in a controller. I have the following code:
Controller galeria.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Galeria extends CI_Controller {
public function index()
{
$this->load->view('vgaleria');
}
public function tg()
{
$this->load->view('tgaleria');
}
}
?>
Views:
<!DOCTYPE html PUBLIC>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Galería de imágenes</title>
<style type="text/css">
</style>
<script src="<?php echo base_url(); ?>javascript/funciones.js"
language="javascript" input="type/text">
</script>
</head>
<body>
<div id="imgs">
<img src="imagenes/imagen1.jpg" id="eimg">
</div>
<input type="button" id="bant" onclick="imgav('atras')" value="Anterior"></button>
<input type="button" id="bsig" onclick="imgav('adelante')" value="Siguiente"></button>
</body>
</html>
View tgaleria.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Galería de imágenes</title>
<style type="text/css">
</style>
<script src="<?php echo base_url();?>javascript/funciones.js"
language="javascript" input="type/text">
</script>
</head>
<body>
<div id="imgs">
<img src="imagenes/imagen1.jpg" id="eimg">
</div>
<input type="button" id="bant" onclick="imginter('arrancar')" value="Arrancar">
</button>
<input type="button" id="bsig" onclick="imgparar()" value="Parar"></button>
The default controller is galeria.php.
$route['default_controller'] = "galeria";
When I type
http://example.com/codeigniter I have everything ok.
But when I type
http://example.com/codeigniter/tg.php
I obtain the error 404, url not found.
How can I fix it?
Thanks.
Have you used .htaccess to remove the index.php? if not then you must append it like:
http://example.com/codeigniter/index.php/galeria/tg
else
http://example.com/codeigniter/galeria/tg
Here, galeria being your Controller and the first segment and tg your function and the second segment of the URI.
Having a strange zend framework issue. Although it's most likely something simple, I've been unable to resolve it so far. I have scraped the bottom of stackOverflow and several other community sites, as well as the zend documentation.
Any ajaxLink that I include in my layout refuses to function. They are classed properly, with hash placeholder in href attribute, but the javascript to activate the link is not being included in the page .
echo $this->jQuery; statement in layout is also failing. Might be the cause of ajaxLink failure. I have verified that I am properly adding the jQuery view helper in my bootstrap. Have tried using both ZendX_JQuery::enableView($view); and $view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper"); methods.
Thanks in advance
Excerpts from the relevant files follow:
bootstrap.php:
protected function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('HTML4_STRICT');
$view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
->appendName('description', 'Business Club');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('SOU Business Club');
$view->setHelperPath(APPLICATION_PATH.'/views/helpers', '');
ZendX_JQuery::enableView($view);
}
dashboardLayout.phtml:
<?php echo $this->doctype();?>
<html>
<head>
<?php echo $this->headTitle();?>
<?php echo $this->headMeta();?>
<?php
echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/dashboardStyle.css');
echo $this->headLink()->prependStylesheet($this->baseUrl().'/js/jquery/css/smoothness/jquery-ui-1.8.11.custom.css');
?>
<?php
echo $this->headScript()->appendFile($this->baseUrl().'/js/paginator.js');
echo $this->jQuery();
?>
</head>
<body id="body">
<div id="wrapper">
<div><?php include "dashboardHeader.phtml"; ?></div>
<div id="bar">
Dashboard Home|
<?php
echo $this->ajaxLink('Club Roster | ',
'user/index',
array('update' => '#content',
'method' => 'post'),
array('format' => 'html')
);
?>
Google Docs|
Book Sale Management|
</div>
<div id="content" align="center">
<?php
echo $this->layout()->content;
?>
</div>
<div id="statusBar">
<?php
$userData = Zend_Registry::get('userData');
$name = $userData->name;
$role = $userData->role;
$status = 'Logged in as: '.' Name: '.$name.' Role: '.$role;
echo $status;
?>
<br />
Logout
</div>
<div><?php include "dashboardFooter.phtml"; ?></div>
</div>
</body>
</html>
HTML <head> output:
<head>
<title>SOU Business Club - Member Dashboard</title>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" >
<meta name="description" content="Business Club" >
<link href="/css/dashboardStyle.css" media="screen" rel="stylesheet" type="text/css" >
<link href="/js/jquery/css/smoothness/jquery-ui-1.8.11.custom.css" media="screen" rel="stylesheet" type="text/css" >
<link href="/css/dashboardStyle.css" media="screen" rel="stylesheet" type="text/css" >
<script type="text/javascript" src="/js/paginator.js"></script>
</head>
In your bootstrap you need to specify where jQuery is, either on your local machine or a CDN. Locally you use:
$view->jQuery()->setLocalPath(PATH);
Or you could use a CDN such as Google's: http://code.google.com/apis/libraries/devguide.html#jquery