I'm writing a CodeIgniter 3 application. My goal is to have a view, which is constantly (as the code goes along) flushed with the output content. I read some answers in stackoverflow, but I am not sure, how to do this.
I have a Controller wich renders the view, in the update method.
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class Dataupdate extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->render('dataupdate_list_view');
}
public function update() {
$this->render('dataupdate_update_view');
}
}
?>
Here is the class "MY_Controller".
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
$this->data['page_title'] = 'Team Portal';
$this->data['before_head'] = '';
$this->data['before_body'] = '';
}
/**
* Render method is used to render a view using a template.
* The given template delivers the HTML header and footer.
* The view contains the actual page content.
*
* #param string $the_view The view to be rendered
* #param string $template The template to render the view
*/
protected function render($the_view = NULL, $template = 'master') {
if ($template == 'json' || $this->input->is_ajax_request()) {
header('Content-Type: application/json');
echo json_encode($this->data);
} else {
// Get current user from database
$this->load->model('user_model');
$user = $this->user_model->get_record_by_name($_SERVER['REMOTE_USER']);
// Data to pass to view
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->data['user'] = $user;
// Load view with data
$this->load->view('templates/' . $template . '_view', $this->data);
}
}
}
?>
Then I have a view, which outputs the content.
<div>
<!-- PAGE TITLE -->
<div class="page-title">
<h3>Daten Update</h3>
<div class="title_right">
</div>
</div>
<div class="clearfix"></div>
<!-- ALERTS -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Fehler<small>Kleiner Text</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content bs-example-popovers">
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<strong>Strong text.</strong>What to do now?
</div>
</div>
</div>
</div>
</div>
<!-- CONTENT -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Neues Daten Update ausführen</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<?php echo form_open('dataupdate/update'); ?>
<input type="text" name="test" />
<button>Ausführen</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
This works fine. So far.
Now, what I try to achieve is the following:
I click on the "Ausführen" (Execute) button, which submits the form to the same url as this page
Then I want a php script to execute, which continously outputs content to the page. Not all content at once, but adds output after output to the page.
I hope I made myself clear.
Any suggestions or tutorials, on how to do that?
You'll need to force the output by calling ->_display method on the output class
For example for JSON output you'll do something like this:
$response = array('status' => 'OK');
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
Same thing goes when you load a view, if you're 100% sure you won't need anything else, You may call _display manually to get the output; just be sure to call exit after that otherwise you'll end-up with a duplicated output (one from your manual call & the other from the automatic call).
Have a look at the user's guide for output class: http://www.codeigniter.com/user_guide/libraries/output.html
Related
I have a controller named : user_login_controller.php and view : user_login_view.php
code of user_login_view.php
<?php echo form_open('user_login_controller/login', 'class="form-horizontal" id="userloginform"');?>
<fieldset>
<legend>Student Login</legend>
<div class="form-group">
<div class="col-lg-6 col-md-6 col-xs-10">
<?php echo form_input(['name'=>'rno','class'=>'form-control','placeholder'=>'Roll Number'])?>
</div>
</div>
<div class="form-group">
<div class="col-lg-6 col-md-6 col-xs-10">
<?php echo form_password(['name'=>'pwd','class'=>'form-control','placeholder'=>'Password'])?>
</div>
</div>
<div class="col-lg-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<?php echo form_reset(['name'=>'Reset','value'=>'Cancel','class'=>'btn btn-primary'])?>
<?php echo form_submit(['name'=>'Submit','value'=>'Login','class'=>'btn btn-primary'])?>
</div>
</div>
</fieldset>
</form>
On submit button click I m sending control to user_login_controller/login
here is user_login_controller.php
<?php
class User_login_controller extends MY_Controller
{
public function index()
{
$this->load->view('user/user_login_view');
}
public function login()
{
echo "User login function";
}
}
?>
but it's given me 404 error.however i have both files
and when I m going through url(http://localhost:8090/project/user_login_controller/login) :then its working. and i have loaded all the necessary helpers.
$autoload['helper'] = array('url','form');
what to do now?
You should set config['base_url'] in application/config/config.php
$config['base_url'] = 'http://localhost:8090/project/';
as form_open consider url to be set to http://localhost:80/project/ or http://[::1]/project/ if you didn't set config['base_url']
Follow below steps and it will sort-out the problem,
1. View
when you opening a form tag, if you want to add several attributes, use this method.
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'userloginform');
echo form_open('user_login_controller/login', $attributes);
?>
It's clean and error less.
2. Config.php
Go to application/config/config.php file, and set
$config['base_url'] = 'http://localhost:8090/your_project_folder_name/';
3. routes.php
Go to application/config/routes.php file, and set
$route['user_login_controller/(:any)'] = "user_login_controller/$1";
$route['user_login_controller'] = "user_login_controller";
This will work. Try it and let me know.
I am new to cakephp. I am trying to build home page with carousel.But I want to make the image in the slide get called from the slides table database.
Below is my carousel code in home.ctp
<div class="mbr-box mbr-section mbr-section--relative mbr-section--fixed-size mbr-section--bg-adapted item dark center mbr-section--full-height active" style="background-image: url(<?= ($slide->thumbnail) ?>);">
<div class="mbr-box__magnet mbr-box__magnet--sm-padding">
<div class=" container">
<div class="row"><div class="col-sm-8 col-sm-offset-2">
<div class="mbr-hero">
<h1 class="mbr-hero__text">Slide 2</h1>
<p class="mbr-hero__subtext">Description</p>
</div>
<div class="mbr-buttons btn-inverse mbr-buttons--center"><a class="mbr-buttons__btn btn btn-lg btn-danger" href="#">HERE</a></div>
</div></div>
</div>
</div>
</div>
style="background-image: url(<?= ($slide->thumbnail) ?>);" is the code that i thought could call the image file from database(images/17860481920x1200-165.jpg). But all i got is a blank.
Can anyone help me. Sorry for my bad english.
Easy way:
cake bake cell Carousel
this create two files,
first CarouselCell.php in View/Cell
<?php
namespace App\View\Cell;
use Cake\View\Cell;
/**
* Carousel cell
*/
class CarouselCell extends Cell
{
/**
* List of valid options that can be passed into this
* cell's constructor.
*
* #var array
*/
protected $_validCellOptions = [];
/**
* Default display method.
*
* #return void
*/
public function display()
{
}
}
secound display.ctp in Template\Cell\Carousel folder.
In CarouselCell.php display method add:
public function display()
{
$this->loadModel('Slides');
$slides = $this->Slides->find('all');
$this->set('slides', $slides->toArray());
}
in display.ctp
<?php foreach($slides as $slide) { ?>
YOUR CAROUSEL HTML HERE
<?php } ?>
On home.ctp
<?= $this->cell('Carousel');?>
Read more: http://book.cakephp.org/3.0/en/views/cells.html
// First you pass data from the controller:
public function home() {
$slider = array('img1.jpg','img2.jpg');
$this->set('slider', $slider);
}
app/View/Pages/home.ctp
<?php print_r ($slider); ?>
For some pages of my application, I would like to use dvuhkolonchaty template. For this I would like to just use a different layout.
In the new layout except the variable $content, which displays the contents of a particular view, I would like to withdraw more other data in another column. Here is the code of the new layout:
<?php /* #var $this AdminController */ ?>
<?php $this->beginContent('/layouts/main'); ?>
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<!-- Any data -->
</div>
<div class="span8">
<?php echo $content; ?>
</div>
</div>
</div>
<?php $this->endContent(); ?>
And that's just where Any data, I would like to display a different not template information (eg, shape or edit the list of properties is constantly changing depending on the viewing record ID).
Q How can I print information in the Any data?
All your controllers will extends Controller class;
In the Controller class, you make a property like this:
public $anyData = null;
In your any actions, you can set `$this->anyData = "anything";
then in your layout, you can write :
<div class="span4">
<!-- Any data -->
<?php
if($this->anyData!=null) {
//Process $this->anyData here, echo or do something you like;
}
?>
</div>
I'm working on a AJAX sign up form which will be submitted to the Zend Framework. Right now, the form's action doesn't seem to be executing. The response text is a duplicate of the current HTML document. Can someone please inform me of what I'm doing wrong, and what the correct URL should be? I've set the project up using modules. For example, should the correct url from the example below be: '/account/register/auth'
#application.ini
resources.router.routes.register.route = /register
resources.router.routes.register.defaults.module = account
resources.router.routes.register.defaults.controller = register
resources.router.routes.register.defaults.action = index
#application/modules/accout/controllers/RegisterController.php
<?php
class Account_RegisterController extends Zend_Controller_Action{
public function init(){
$this->view->register = new Account_Form_Register();
}
public function indexAction(){
}
public function authAction(){
$request = $this->getRequest();
$form = new Account_Form_Register();
if($request->isPost()){
print('submit');
}else{
print('nothing yet');
}
}
}
?>
#application/modules/account/views/scripts/register/index.phtml
<div class="register">
<form id="registerForm">
<h2 class="formHeader">Register</h2>
<div class="floatLeft">
<?php echo $this->register->fName; ?>
</div>
<div class="remaining">
<?php echo $this->register->lName; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->uEmail; ?>
</div>
<div class="remaining">
<?php echo $this->register->aEmail; ?>
</div>
<div class="">
<?php echo $this->register->phone; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->oPassword; ?>
</div>
<div class="remaining">
<?php echo $this->register->cPassword; ?>
</div>
<div id="formButton">
<?php echo $this->register->submit; ?>
</div>
</form>
</div>
As I said, maybe you should disable the view processing when doing AJAX action in order to return a json_encode($some_data) instead of HTML content.
Try
$this->_helper->viewRenderer->setNoRender(true);
Take a look at this article
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.