I wish to add some script to a view and have it que and load at the bottom of the page within the default layout. At the moment it runs before query is loaded and causes an error.
I am using cakephp 2.5
Is there a way to buffer it?
myview.ctp:
<script type="text/javascript">
FWDEVPUtils.onReady(function(){
FWDEVPlayer.useYoutube = "yes";
FWDEVPlayer.videoStartBehaviour = "pause";
new FWDEVPlayer({
//main settings
instanceName:"player",
parentId:"player",
mainFolderPath:"content"
});
});
default.ctp
echo $this->Html->script('//code.jquery.com/jquery-1.10.2.min.js');
echo $this->Html->script('bootstrap.min');
echo $this->Html->script('FWDEVPlayer');
echo $this->fetch('script');
echo $this->Js->writeBuffer();
In myview.ctp:
<?php $this->start('script'); ?>
<script type="text/javascript">
FWDEVPUtils.onReady(function(){
FWDEVPlayer.useYoutube = "yes";
FWDEVPlayer.videoStartBehaviour = "pause";
new FWDEVPlayer({
//main settings
instanceName:"player",
parentId:"player",
mainFolderPath:"content"
});
});
<?php $this->end(); ?>
You script will then be included in your layout default.ctp where you wrote:
echo $this->fetch('script');
Related
I am trying to use meta tags and description in cakephp 3. It is working so far, but I am facing the problem that I canĀ“t put it in the of my page, because my .ctp files are rendered in my default.ctp
So for example I have my menu, footer etc. in my default.ctp and my page for the faq is in the faq.ctp How can I push those
<?php
echo $this->Html->meta('description','enter any meta description here');
?>
metadescription in the head tag? Do I need a template language like smarty and use blocks?
In layout:
<?php
echo $this->Html->meta('description',$description);
?
in your faq.ctp or faq() method:
<?php
$this->set('description','enter any meta description here');
?
add this in your ctp file (there is NO echo )
<?php $this->Html->meta('description', 'some text here', ['block' => true]);?>
and this line into your layout
<?php echo $this->fetch('meta');?>
see also similar question with title: $this->set('title', 'Title Name'); not working in CakePHP 3.x
Controler
public function search() {
$this->Paginator->settings = $this->paginate;
$this->loadmodel('Usermgmt.User');
if ($this->request -> isPost()) {
$this->User->set($this->data);
$keyword=$this->data['Doctors']['search'];
//$this->loadmodel('Usermgmt.User');
$cond=array('OR'=>array("User.username LIKE '%$keyword%'","User.email LIKE '%$keyword%'", "User.first_name LIKE '%$keyword%'", "User.last_name LIKE '%$keyword%'"));
//$result = $this->paginate('User',array('conditions'=>$cond));
$result = $this->paginate('User',array($cond));
$this->set('result', $result);
}
}
View
<?php
if (!empty($result)) { $sl=0;
foreach ($result as $row1) {
//print_r($row1);
$sl++; ?><div style="width:100%;display:inline-block;">
<div style="float:left">
<?php
//echo $row1['id'];
echo $this->Html->link($this->Html->image('../files/user/photo/'.$row1 ['User']['photo_dir'].'/'.$row1 ['User']['photo'], array('width' => '180', 'height' => '180')),
array('controller'=>'Profiles','action'=>'index',$row1['User']['id']),
array('escape' => false));
?>
</div>
<div>
<?php echo h($row1['User']['first_name'])." ".h($row1['User']['last_name'])."</br>";
echo h($row1['User']['username'])."</br>";
echo h($row1['User']['email'])."</br>";
echo h($row1['User']['mobile'])."</br>";
echo h($row1['UserGroup']['name'])."</br>";
?></div>
<div style="clear:both;"></div>
</div>
<?php }?>
<?php echo $this->Paginator->prev('previous'); ?>
<?php echo $this->Paginator->numbers(); ?>
<?php echo $this->Paginator->next('Next'); ?>
<?php }?>
here am search the user name or user details like fname, email like and display in view page
here i get output with pagination like 1 2 3 4 only first page displays when i click next page that shows empty pages may be $result getting unset how to solve this ??
The variable result is only sometimes set
if ($this->request->isPost()) {
...
$result = $this->paginate('User',array($cond));
$this->set('result', $result);
}
The variable result is only set for POST requests - clicking a link is not a post request, therefore the result variable is undefined.
Ensure you are paginating a GET request
There are several solutions, but the simplest solution to "How to paginate post data" is to not do so. Change your search form to use GET, and ensure the get parameters persist when paginating a request.
At the very least the controller code needs to call paginate and set for the variables in the view to exist irrespective of how the controller action was reached.
$this->redirect called from any controller method seems to result in a blank screen error, despite turning debug to 2 in my core.php.
In my AdminAppController, located in App/Plugin/Admin/Controller, was this piece of code originally, in my beforefilter():
public function beforeFilter() {
parent::beforeFilter ();
$rusername = $this->Auth->user ( "Username" );
if ($rusername != "admin") {
$this->redirect ( "/" );
return;
}
This results was an admin wide blank screen error, regardless of username. By commenting out the redirect, it allowed me to enter the admin area again.
However, now whenever I use $this->redirect I get blank screen errors.
For example here is my Prices view through my Product controller:
<?php echo $this->Session->flash('flash');?>
<?php echo $this->Form->create('Product', array('action' => 'change')); ?>
<fieldset>
<h3>Products</h3>
<table>
<?php
foreach($products as $k=>$v){
echo $this->Form->hidden("Product.{$k}.id", array('value'=> $v["Product"]['id']));
echo $this->Form->input("Product.{$k}.name", array('value' => $v["Product"]["name"], 'disabled' =>'disabled'));
echo $this->Form->hidden("Product.{$k}.slug", array('value'=>$v["Product"]['slug']));
echo $this->Form->hidden("Product.{$k}.description", array('value'=>$v["Product"]['description']));
echo $this->Form->hidden("Product.{$k}.cateID", array('value'=>$v["Product"]['cateID']));
echo $this->Form->input("Product.{$k}.price", array('value' => $v["Product"]['price']));
echo $this->Form->hidden("Product.{$k}.photo", array('value'=>$v["Product"]['photo']));
echo $this->Form->hidden("Product.{$k}.photo_dir", array('value'=>$v["Product"]['photo_dir']));
echo $this->Form->hidden("Product.{$k}.active", array('value'=>$v["Product"]['active']));
echo $this->Form->hidden("Product.{$k}.views", array('value'=>$v["Product"]['views']));
echo $this->Form->hidden("Product.{$k}.created", array('value'=>$v["Product"]['created']));
echo $this->Form->hidden("Product.{$k}.modified", array('value'=>$v["Product"]['modified']));
}
?>
</table>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
My Products controller contains this method:
public function prices(){
$products = $this->Product->find ("all");
$this->set ("products", $products );
}
And this method is called from this view:
public function change(){
//create() resets model, precaution
$this->Product->create();
$data = $this->request->data;
$this->Product->saveMany($data['Product']);
$this->Session->setFlash( "Prices Saved.");
$this->redirect ( '/' );
return;
}
On submitting a change of price (which does save and alter the database, the redirect line causes the screen to go blank, with no errors, or any other indication of what has gone wrong.
Error logs show no sign of what is happening.
Thanks.
Apologies for the late Answer.
After much struggle it turned out to simple.
AdminAppController included some whitespace before an opening
I am a totally noob at CakePHP and have been dealing with this site update from traditional php to CakePHP. In the old website there was a folder for javascript and in there, there was a file called paging.js, then, at then end of where i was doing the table loops and stuff you would add this
echo '<div id="pageNavPosition"></div>
<div> </div>';
echo "<script type='text/javascript'><!--
var pagerAdmin = new PagerAdmin('results', 5);
pagerAdmin.init();
pagerAdmin.showPageNav('pagerAdmin', 'pageNavPosition');
pagerAdmin.showPage(1);
//--></script>";
}
In that website works perfectly, it shows only 5 records and with pagination. But now here in cake PHP I have thi code in my view but its not picking up that portion of code
<?php
echo '<div><h3>Admin - Update Data</h3><p>To update data click on name</p></div>';
print("<table id='results'><tr><th>ID</th><th>Image</th><th>Name</th><th>Price</th><th>Description</th></tr>");
$i =0;
foreach ($dishes as $key => $dish):
print("<tr>");
print("<td width='20px'>");
echo '<p style="font-size:14px; color:blue; padding:0;">'.$dishes[$i]['Dish']['id'].'</p>';
echo '<input type="hidden" name="'.$dishes[$i]['Dish']['dish_name'].'" value="'.$dishes[$i]['Dish']['id'].'" id="DishDishId" />';
print("</td>");
print("<td width='10px'>");
print("<img class='custom_rate' alt='' src='/myweb/img/".$dishes[$i]['Dish']['dish_image']."' />");
print("</td>");
print("<td width='100px'>");
print("<a href='index.php?form_type=admin_manager&id=".$dishes[$i]['Dish']['id']."'>".$dishes[$i]['Dish']['dish_name']."</a> ");
print("</td>");
print("<td width='100px'>");
print($dishes[$i]['Dish']['dish_price']);
print("</td>");
print("<td width='100px'>");
print($dishes[$i]['Dish']['dish_disc']);
print("</td>");
print("</tr>");
$i++;
endforeach;
print("</table>");
if($i> 5)
{
echo '<div id="pageNavPosition"></div>
<div> </div>';
echo "<script type='text/javascript'>" ;
echo "var pagerAdmin = new PagerAdmin('results', 5);";
echo 'pagerAdmin.init();';
echo "pagerAdmin.showPageNav('pagerAdmin', 'pageNavPosition');";
echo 'pagerAdmin.showPage(1);';
echo '</script>";';
}
?>
Any idea how i can make this work again? .Any help would be much appreciated.
Thanks
For anyone who runs into this issue, you just need to create a separate .js file and place it in the webroot/js folder with your js code. Then reference it as follows
echo $this->Html->script('myjasfile.js');
I have tried many times to use this plugin and I failed.
I am following documentation, but it does not work for me.
I am posting the simple code here, to know what wrong I am doing.
1-I put this plugin in this folder app/plugins
2- I add TinyMce helper to articles_controller
<?php
class ArticlesController extends AppController {
// good practice to include the name variable
var $name = 'articles';
// load any helpers used in the views
var $helpers = array('Html', 'Form','TinyMce.TinyMce');
/**
* index()
* main index page of the formats page
* url: /formats/index
*/
function index(){
// get all formats from database where status = 1
$articles = $this->Article->find("all") ;
$this->set('articles', $articles);
}
function admin_add() {
// if the form data is not empty
if (!empty($this->data)) {
// initialise the format model
$this->Article->save($this->data);
// set a flash message
$this->Session->setFlash('The Format has been saved');
// redirect
$this->redirect(array('action'=>'index'));
} else {
// set a flash message
$this->Session->setFlash('The Format could not be saved. Please, try again.','default', array('class' => 'flash_bad'));
}
}
}
?>
3- in the view file articles/admin_add.ctp I added the editor
// i think the problem in this code
<?php $this->TinyMce->editor(array(
'theme' => 'advanced'
)); ?>
<div class="formats form">
<?php echo $form->create('Article');?>
<fieldset>
<legend>Add a article</legend>
<?php
// create the form inputs
echo $this->Form->input('title');
echo $this->Form->input('content'); ?>
</fieldset>
<?php echo $form->end('Add');?>
</div>
<ul class="actions">
<li><?php echo $html->link('List Articles', array('action'=>'index'));?></li>
</ul>
You need to put tinymce files into your js assets
Then you have to add into section of your layout.
Then you'll need to init tinymce according to example provided on tinymce website (ex: full tinymce layout) and configure it according to your requirements.
I'd personally would not rely on such cake plugins, when actions required to get things working are not many and they are simple enough.