first of all I'm a newbie in web development so...
I don't know if there is a way to do the following (neither if it's a best practice).
I have a CodeIgniter + Angularjs application where there is a view contains a Bootstrap tab (which is also a angular app):
[view_general_params.php]
<!DOCTYPE html>
<html lang="it">
<head>
</head>
<body ng-controller="HouseController">
<div class="container">
<ul class="nav nav-tabs" id="myTab">
<li class="active">Home</li>
<li>Profile</li>
<li>Messages</li>
<li>Settings</li>
</ul>
<div class="tab-content">
</div>
</div>
</body>
</html>
Now, since each tab must contains a lot of controls (Textboxes etc..) I'm asking myself if there is a way to split it in different files loaded by the controller in this way:
public function details($house_id)
{
$user = $this->session->userdata('user');
if (is_null($user)) {
$this->session->sess_destroy();
redirect('login/index');
}
else {
$data = $this->create_data_array($user);
$data['house'] = $this->houses_model->get_house($house_id);
$data['title'] = "House details";
$this->load->view('head_navbar', $data);
$this->load->view('house_details_view', $data);
$this->load->view('view_general_params', $data);
$this->load->view('view_house_params', $data);
$this->load->view('view_panel_params', $data);
$this->load->view('view_statistics', $data);
}
}
where each others views contain data for a single tab-page:
[view_general_params.php]
<!DOCTYPE html>
<html lang="it">
<head>
</head>
<body>
<div class="container">
<div class="tab-content">
<div class="tab-pane active" id="general">
general controls...
</div>
</div>
</div>
</body>
</html>
Yes you can. In Codeigniter you can use a view as a template. Take these steps:
create a folder in your views directory, call it "Templates".
application\views\templates\
create each template the way you want and place it inside the templates folder:
application\views\templates\head_navbar.php
application\views\templates\house_details_view.php
application\views\templates\view_general_params.php
In your controller, you want to call these templates with a third paramater set to TRUE, then passing this to as an array value. This array will then be sent to your main view, like this:
public function details($house_id) {
$user = $this->session->userdata('user');
if (is_null($user)) {
$this->session->sess_destroy();
redirect('login/index');
} else {
$data = $this->create_data_array($user);
$data['house'] = $this->houses_model->get_house($house_id);
$data['title'] = "House details";
$data_to_final_view = array(
"view1" => $this->load->view('head_navbar', $data, TRUE),
"view2" => $this->load->view('house_details_view', $data, TRUE),
"view3" => $this->load->view('view_house_params', $data, TRUE),
"view4" => $this->load->view('view_panel_params', $data, TRUE),
"view5" => $this->load->view('view_statistics', $data, TRUE)
);
$this->load->view('view_general_params', $data_to_final_view);
}
}
Now, in your view_general_params view, you can place the template's content anywhere you want:
<div class="container">
<ul class="nav nav-tabs" id="myTab">
<li class="active">Home</li>
<li>Profile</li>
<li>Messages</li>
<li>Settings</li>
</ul>
<div class="tab-content1">
<?= $view1 ?>
</div>
<div class="tab-content2">
<?= $view2 ?>
</div>
... etc..
</div>
Related
i want to show total records in a database in dashboard-home.php .
i am getting error of undefined index
how to make proper links to pages to get contents in a same page i mean different pages in a same div.
<!DOCTYPE html>
<html>
<head>
<title>SIS Dashboard</title>
<link rel="stylesheet" type="text/css" href="../css/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body id="dashbody">
<div class="row dashboard-head">
<div class="container">
<div class="col-md-2">
<h2>DASHBOARD</h2>
</div>
<div class="tpad navbar-right">
<div class="col-md-8">
<p><b><?php echo "Welcome " .$_SESSION['user']; ?></b></p>
</div>
<div class="col-md-4 logout-a">
Logout
</div>
</div>
<div class="row db-wrapper">
<div class="col-md-3 left-panel">
<ul>
<li>User Panel</li>
- View Users<hr>
<li>Gallery Panel</li>
- View Gallery<hr>
<li>News Panel</li>
- View All News<hr>
<li>Events Panel</li>
- View Events
</ul>
</div>
<div class="col-md-9 right-panel">
<?php
if($_GET['dash-home']){
include'dashboard-home.php';
}
if(isset($_GET['gallery'])){
include'pages/gallery_panel.php';
}
if(isset($_GET['news'])){
include'news_panel.php';
}
if(isset($_GET['view_news'])){
include'view_news.php';
}
if(isset($_GET['edit_news'])){
include'edit_news.php';
}
?>
</div>
</div>
</div>
</div>
</body>
</html>
Note that, your URL is dashboard.php, no query string with dashboard.php?dash-home=1, it means this condition can't be work and you will get the Undefined Index notice as you are getting:
if($_GET['dash-home']){
And if you check your code you are already using isset() for other input so you need to add isset() here also:
if(isset($_GET['dash-home'])){
As per your dashboard link:
<h2>DASHBOARD</h2>
You can't get the value from dash-home because you didn't use value of dash-home, you can use like:
<h2>DASHBOARD</h2>
One more important point, $_SESSION needs session_start() at top level and i didn't find this function in your file. This is just side note, you need to take care of this.
Update:
If you want to display an included file in default than you need to use else part and as per your comment you need to use one include file as per condition, try this:
<?php
if(isset($_GET['dash-home'])){
include 'dashboard-home.php';
}
elseif(isset($_GET['gallery'])){
include 'pages/gallery_panel.php';
}
elseif(isset($_GET['news'])){
include 'news_panel.php';
}
elseif(isset($_GET['view_news'])){
include 'view_news.php';
}
elseif(isset($_GET['edit_news'])){
include 'edit_news.php';
}
else{
include 'dashboard-home.php';
}
?>
I would recommend to use switch instead of if-else
You are checking for $_GET['dash-home'] but you need to check for $_GET['page']
Try:
if(isset($_GET['page']))
{
switch ($_GET['page'])
{
case 'dash-home':
include 'dashboard-home.php';
break;
case 'gallery':
include 'pages/gallery_panel.php';
break;
and so on.....
}
}
I have this html code
<body>
<div class="wrapper">
<div class="banner">
</div>
<div class="nav_bar">
<ul>
<li>Home</li>
<li>News</li>
<li>Registration FAQs</li>
<li>How to Register and Rules</li>
<li>Register school</li>
<li>Register pupil</li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
</div>
<div class="content_area">
**<?php echo $content; ?>**
</div>
<div class="clear"></div>
</div>
<div class="footer">
<div>
</div>
</div>
</body>
now once the user clicks the links i want to load the content depending on which link he has clicked as you can see inn the code above. I can load the content as long as its all html but now that php tags are included i find it hard. how do i load the content with the following data?
<div id="form_input">
<?php
echo form_open('form/data_submitted');
// Show Name Field in View Page
echo form_label('User Name :', 'u_name');
$data= array(
'name' => 'u_name',
'placeholder' => 'Please Enter User Name',
'class' => 'input_box'
);
echo form_input($data);
// Show Email Field in View Page
echo form_label('User email:', 'u_email');
$data= array(
'type' => 'email',
'name' => 'u_email',
'placeholder' => 'Please Enter Email Address',
'class' => 'input_box'
);
echo form_input($data);
?>
</div>
// Close Form
<?php echo form_close();?>
is it possible?
I would suggest the following:
Your Controller:
class Site extends CI_Controller
{
public function home()
{
$arrViewData = array
(
"content" = $this->load->view("site/home","",true)
);
$this->load->view("index", $arrViewData);
}
}
your view called i.e. index
<html>
<body>
<div class="wrapper">
<div class="banner">
</div>
<div class="nav_bar">
<ul>
<li>Home</li>
<li>News</li>
<li>Registration FAQs</li>
<li>How to Register and Rules</li>
<li>Register school</li>
<li>Register pupil</li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
</div>
<div class="content_area">
<?= $content; ?>
</div>
<div class="clear"></div>
</div>
<div class="footer">
<div>
</div>
</div>
</body>
</html>
your view called home.php in views/site/
hello i'm home
with this method i think you can achieve what you want if i understand you correctly
as a side note: this is an example - but you should be able to see the point
You might want to consider breaking your view html into a smaller "snippets". CodeIgniter (CI) makes it easy to load them in the order you want and then "show" them all at once.
One possible breakdown of views into snippets follows.
header_view.php
<!DOCTYPE HTML>
<html>
<head>
<title>
<?= (isset($page_title)) ? $page_title : "Rapids Riders"; ?>
</title>
In the next view snippet I put some URLs in the links to serve as examples using the CI controller/function syntax. Adjust to your needs. To my thinking "home", "news" and "register" are separate controllers. I will focus on the Registration controller in a bit.
If you're not familiar with <?= it is shorthand for <?php echo.
body_start_view.php
<body>
<div class="wrapper">
<div class="banner">
</div>
<div class="nav_bar">
<ul>
<li><a href=<?= base_url("home"); ?>>Home</a></li>
<li><a href=<?= base_url("news"); ?>>News</a></li>
<li><a href=<?= base_url("register/faq"); ?>>Registration FAQs</a></li>
<li><a href=<?= base_url("register/help"); ?>>How to Register and Rules</a></li>
<li><a href=<?= base_url("register/school"); ?>>Register school</a></li>
<li><a href=<?= base_url("register/pupil"); ?>>Register pupil</a></li>
<li><a href=<?= base_url("home/about"); ?>>About Us</a></li>
<li><a href=<?= base_url("home/contact"); ?>>Contact Us</a></li>
</ul>
</div>
footer_view.php
<div class="clear"></div>
</div>
<div class="footer">
<div>
</div>
</div>
</body>
</html>
These three view snippets are easily reused by any controller on the website. A controller based "helper" function is shown below (in the registration controller) to demonstrate implementing a sort of "templating" scheme.
The example form html will be in a file called pupil_form_view.php and would be used (in my example) for the register/pupil page.
When using an MVC app structure you typically want to set as much "data" as possible in the controller and then pass it to the view. The various vars in this file will be defined and set in the controller calling this view.
pupil_form_view.php
<div id = "form_input">
<?php
echo form_open($form_action);
// Show Name Field in View Page
echo form_label($name_field['label'], $name_field['name']);
echo form_input($name_field);
// Show Email Field in View Page
echo form_label($email_field['label'], $email_field['name']);
echo form_input($email_field);
?>
</div>
// Close Form
<?php echo form_close(); ?>
Register.php is a controller that handles multiple pages - faq, help, school, pupil
class Register extends CI_Controller
{
function __construct()
{
parent::__construct();
}
//pupil will show the pupil registration form
public function pupil()
{
$view_file = "pupil_form_view"; //the file name of a view to load
$data['page_title'] = "Pupil Registration";
$data['form_action'] = 'form/data_submitted';
//form fields info
$data['name_field'] = array(
'label' => 'User Name: ',
'name' => 'u_name',
'placeholder' => 'Please Enter User Name',
'class' => 'input_box'
);
$data['email_field'] = array(
'type' => 'email',
'name' => 'u_email',
'placeholder' => 'Please Enter Email Address',
'class' => 'input_box'
);
//output a page
$this->render_page($view_file, $data);
}
//This function handles all the repetitive view loading code.
//Just pass it the name of a view file and some data to use in the views
//The default value for $view_data means you don't HAVE TO pass this argument if not needed
protected function render_page($content, $view_data = NULL)
{
//notice the use of 'method chaining' for load functions
$this->load
->view('header_view', $view_data)
->view('body_start_view')
->view($content) //$view_data vars will be visible to this view file
->view('footer_view');
}
public function faq()
{
$data['page_title'] = "Frequently Asked Questions";
$view_file = "faq_view";
//other data setting code if needed
$this->render_page($view_file, $data);
}
public function school()
{
$data['page_title'] = "School Registration";
$view_file = "school_form_view";
//other data setting code
$this->render_page($view_file, $data);
}
public function help()
{
$data['page_title'] = "How to Register";
$view_file = "help_view";
//other data setting code
$this->render_page($view_file, $data);
}
function index()
{
//just load the help page when users navigate to example.com/register
$this->help();
}
}
I know I went overboard here. Hope this is useful.
I solved it by nesting views that is calling $this->load->view($content); within my template view instead of <?php echo $content;?> then on the controller function i loaded the content like this
$data['content']='view_name_here'; // this loads content with a view
$this->load->view('my_template',$data); //loaded my template with $content
I have a welcome controller with index function:
public function index()
{
$this->load->view('templates\header');
$this->load->view('welcome_message');
$this->load->view('templates\footer');
}
Header view code goes like following:
<header>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="<?php echo base_url() ?>">
Test App
</a>
</div>
</div>
</div>
</header>
It works perfectly fine so far.
I also want to add following code in my navigation bar (with in header tags) as a menu items but only if the user session is active. So if a user is logged in than only he/she should be able to see the menu item else the user should only see a navbar with brand name "Test App".
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<i class="fa fa-tachometer"></i> Dashboard
</li>
<li>
<a href="<?php echo base_url('admin/logout') ?>"><i class="fa fa-sign-out"></i> Logout
</a>
</li>
</ul>
</div>
I also have a helper which check and return true or false if the user is logged in or not and i am using it inside the controller. I am not comfortable using that inside the view because than i have to echo the menu items and adjust a lot of double/single quotes to make that work. Is there a neat way to do this ?
Code for login helper:
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('username');
if ( !isset($user) )
{
return false;
}
else
{
return true;
}
}
you can choose what to display according to the user session from controller.
For example:
if($this->session->userdata('username')<>NULL){
$data['logged_in'] = 1;
}else{
$data['logged_in'] = 0;
}
$this->load->view('templates\header',$data);
In view:
if($logged_in==1){
//header with logged in options
}else{
//header with normal options.
}
Why don't you use session_status()?
On its documentation one of the users posted in comments universal function for checking if session is started:
<?php
/**
* #return bool
*/
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
// Example
if ( is_session_started() === FALSE ) session_start();
?>
I'm trying to put my login form in the header which resides within layout.phtml
I've created my form object in Application/Module.php
<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Users\Form\LoginForm;
class Module
{
public function onBootstrap(MvcEvent $e)
{
.
.
.
$form = new LoginForm();
$form->get('submit')->setValue('Login');
$application = $e->getParam('application');
$viewModel = $application->getMvcEvent()->getViewModel();
$viewModel->form = $form;
}
.
.
.
}
This is so that the form is available for every page (as layout.phtml is used to render every page)
For my layout.phtml file:
<?php echo $this->doctype(); ?>
<?php
$form = $this->layout()->form;
//\Zend\Debug\Debug::dump($form);
$form->prepare();
$form->setAttribute('action', $this->url(NULL, array('controller' => 'Users', 'action' => 'login')));
?>
<html>
<head>
<?php echo $this->headTitle('Budget') ?>
<?php echo $this->headMeta()
->appendName('viewport', 'width=device-width, initial-scale=1, maximum-scale=1')
->appendHttpEquiv('X-UA-Compatible', 'IE=edge') ?>
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
->prependStylesheet($this->basePath() . '/css/style.css')
->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
->prependStylesheet($this->basePath() . '/css/bootstrap.min.css') ?>
</head>
<body>
<div class="header">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
Lift share</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Search</li>
<li>Offer a lift</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php echo $this->form()
->setAttribute('class', 'navbar-form')
->setAttribute('role', 'form')
->openTag($form); ?>
<div class="form-group">
<?php echo $this->formLabel($form->get('email')
->setLabelAttributes(array('id' => 'login_email'))
->setLabelAttributes(array('class' => 'sr-only'))
->setLabelAttributes(array('placeholder' => 'Enter email'))
); ?>
<input class="form-control" id="exampleInputEmail2" placeholder="Enter email" type="email">
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2">Password</label>
<input class="form-control" id="exampleInputPassword2" placeholder="Password" type="password">
</div>
<button type="submit" class="btn btn-default">Sign in</button>
<?php echo $this->form()->closeTag(); ?>
</ul></div><!-- /.navbar-collapse -->
</div>
</nav>
</div>
<?php echo $this->content; ?>
<?php echo $this->inlineScript() ?>
<!-- Scripts -->
<?php echo $this->headScript()
->prependFile($this->basePath() . '/js/bootstrap.min.js')
->prependFile($this->basePath() . '/js/jquery.min.js')
->prependFile($this->basePath() . '/js/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath() . '/js/html5shiv.js','text/javascript', array('conditional' => 'lt IE 9',))
; ?>
</body>
</html>
I'm getting the error - Fatal error: Call to undefined method Zend\Form\View\Helper\Form::setAttribute() in /var/www/budget_zend/module/Application/view/layout/layout.phtml on line 55
I understand that the form helpers that were available in my other views are perhaps not available in the layout view. But how do I put a form in the layout.phtml file then? Also, seems a bit strange that I have to define this in different ways - say, for example, I at some point want to move the form from the header (within the layout.phtml file) to the main content area - this means I need to change stuff at the backend to make a front end update. Doesn't seem right. Can someone please inform me how I should be doing things.
Thanks
I understand that the form helpers that were available in my other views are perhaps not available in the layout view
This is not the case. Layouts and views can access exactly the same view helpers.
I think you are confusing the form view helper ($this->form()) with the instance of your Login form ($this->form, or $form since you've assigned it to that variable). The form view helper doesn't have a setAttribute() method, hence the error. You probably mean something more like this:
$form->setAttribute('class', 'navbar-form')
->setAttribute('role', 'form');
echo $this->form()->openTag($form);
I think it's cleaner to write a custom view helper then assigning the Form trough the bootstrap event every time.
Assuming you'd place the ViewHelper php file in Application/src/View/YourFormHelper.php you would need to configure it within the module_config.php like so:
'view_helpers' => array(
'invokables'=> array(
'yourFormHelper' => 'Application\View\Helper\YourFormHelper',
),
),
If your Form requires a service you might asweel want to create a factory which inject's those rather then just having a invokable without dependencies.
Within your View Helper just configure your forms as usual. But keep in mind You'll need to extend the AbstractHelper and implement the __invoke() method.
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class PageChunk extends AbstractHelper
{
public function __invoke()
{
//build form and add elements/inputfilters
return $form;
}
}
You now could implement some kind of rendering so It would just return the form html tag's etc.
For explanatory reasons we'll just attach the form in our layout like so and the form should be displayed in the layout correctly.
// within your layout.phtml
...
$form = $this->yourFormHelper();
...
// open tag/close tag, formRow etc
I am designing my own site. First I created a template with header.php and footer.php I put them in the includes folder. So, everytime I am going to make a new page like "about page" all I have to do is to call them using the code below:
<body>
<?php include("includes/header.php"); ?>
<?php include("includes/footer.php"); ?>
</body>
</html>
Here is the code in my header.php
<div id="headwrapper">
<header>
<div id="logo"><img src="images/adlogo.png"/></div>
<div id="homefeature">622x82</div>
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>portfolio</li>
<li>Blogs</li>
<li>Contact</li>
</ul>
</div>
</header>
</div><!--end of headwrapper-->
But my index.php and my about page have the same header image. Is there a way or code that can accomplish the work for different header images for every page? Example on index.php I want to have image1 and to my about page I want to have image2. That's all thank you by the way I am not using a wordpress my site is not a wordpress.
Another solution.
In header.php you call function $_SERVER['PHP_SELF'] for get page name and then selected image for that page. You could modify like this
<?php
$imageName = "";
if($_SERVER['PHP_SELF'] == "index.php")
{
$imageName = "images/1.png";
}
elseif($_SERVER['PHP_SELF'] == "aboutus.php")
{
$imageName = "images/2.png";
}
?>
<div id="headwrapper">
<header>
<div id="logo"><img src="'".<?=$imageName?>."'"/></div>
<div id="homefeature">622x82</div>
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>portfolio</li>
<li>Blogs</li>
<li>Contact</li>
</ul>
</div>
</header>
</div><!--end of headwrapper-->
Given your structure, I think you can set your header file like this:
<body>
<?php $header_image = "about_us_header.jpg"; ?>
<?php include("includes/header.php"); ?>
<?php include("includes/footer.php"); ?>
</body>
and reference the variable in your header:
<div id="headwrapper">
<header style="background: url(<?php echo $header_image; ?>);">
But wait I thought of a better way
you can do it in css like this:
<body class="aboutus">
<?php include("includes/header.php"); ?>
<?php include("includes/footer.php"); ?>
</body>
and in your css stylesheet:
body.aboutus header {
background: url(whatever.jpg);
}