dynamically loading content codeigniter - php

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

Related

I want to ask about custom widget in Yii2 advanced template

I have created a custom widget on my yii2 advanced project
I create new dir name components in backend dir
after that, I create new dir again in components dir name views
in backend/components I create new PHP file name SideBWidget.php
<?php
namespace backend\components;
use yii\base\Widget;
use yii\helpers\Html;
use common\models\Content;
class SideBWidget extends Widget{
public function run(){
$models = Content::findAll([
'c_pkey'=>0,
]);
$this->render('sideb',[
'model' => $models
]);
}
}
?>
in backend/components/views I create sideb.php
<div id="sidebar-nav" class="sidebar">
<div class="sidebar-scroll">
<nav>
<ul class="nav">
<?php foreach($model as $row): ?>
<li><i class="lnr lnr-alarm"></i> <span><?php echo $row->c_name; ?></span></li>
<?php endforeach; ?>
</ul>
</nav>
</div>
</div>
and I call the widget in views/layout/main.php like this, and I also use the widget path
use backend\components\SideBWidget;
<?= SideBWidget::widget() ?>
but when I run there is nothing and no error message. Where is the problem?
You must add return statement in run() function of widget:
return $this->render('sideb',[
'model' => $models
]);

Codeigniter + Boostrap : split tab-content in multiple files

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>

Zend Framework 2 - how to use form helpers in layout.phtml

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

Display two views at once and change url on change

I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.

Displaying another controller's view inside a view

I'm using cakephp and I want the login view for the controller 'users' to be displayed on the default.ctp for the layout controller. I can I do this?
For example:
<div id="leftNav">
<div id="login-block" class="block">
<?php
//render users/login here
?>
<ul>
<li>Login</li>
<li>Register</li>
<li>Logout</li>
</ul>
</div>
</div>
I would make it an element - they are designed for re-usable code chunks that can be used anywhere.
To summarize the functionality, in your view you'd use:
<?php echo $this->element('login'); ?>
and put your "login-block" div login stuff in
/app/views/elements/login.ctp
If you don't use elements for that and you already have the login view you can use it like this
<?php echo $this->requestAction('/users/login'); ?>

Categories