cakePHP v2.x - displaying content out of database field - cakephp

Beginners question: Why is cakePHP not displaying the carriage return/new line and other characters when using;
<?php echo h($property['Property']['fullDesc']); ?>
I tried using
<?php echo $property['Property']['fullDesc']; ?>
but both showing output of text as one block of text instead of paragraphs.
Any help much appreciated.

The h function is just a wrapper for the php htmlspecialchars() function. It will not convert carriage returns into <br /> tags. You will need to do something like this:
echo nl2br(h($property['Property']['fullDesc']));

basic PHP...
<?php echo nl2br(h($property['Property']['fullDesc'])); ?>
nl2br() will form those newlines into <br>
Tip: you can make your bake templates include that automatically for all your textarea fields.
See http://www.dereuromark.de/2012/04/24/cake-bake-custom-templates-deluxe/

Related

cakephp 3 : How to add class in form button?

I have tried this line to add class in cakephp form button, but class is not showing in html
<?= $this->Form->button(__('Login',['class'=>'login-btn'])); ?>
How can I add class in button ?
I think your example doesn't work, because the __() Call shouldn't include the array for the options of the button. Please try the following:
<?= $this->Form->button(__('Login'),['class'=>'login-btn']); ?>
Have a try on this below:
<?php echo $this->Form->button('Login',['class'=>'login-btn']); ?>
A good reference here: Creating input elements
Update
__() is for internalization. Using this will look in to your localization file and output it's corresponding translation. In your case, you include the options inside __() which I think it will cause an error but if it didn't, it will look for it's translated version and also this means ['class'=>'login-btn'] is not considered as an option anymore.
it has to be inside an array : try this
<?= $this->Form->button(__('Login'),array('class'=>'login-btn')); ?>

Drupal 7 overriding node.tpl.php

I am trying to override the front page node, however after following the guide from the official site, it's not working.
Guide: https://www.drupal.org/node/1585528
I have taken node.tpl.php and renamed it to node--front.tpl.php and made changes to the layout, saved and cleared cache. The changes are not being displayed.
Now if I edit node.tpl.php directly it shows the changes, anyone know what I am doing wrong to override specific node templates?
EDIT:
I want to move the title below the image being displayed in the front page.
See below: Moving the title block under the content block, moves the title down as I want it to do, however how do I specify this for just the front page and not all nodes? (renaming node.tpl.php to node--front.tpl.php does not work as mentioned above)
In node.tpl.php: (title block)
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
(content block)
<div class="content clearfix"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>
To override the node template for particular node, you need to override the default node.tpl.php. For this copy the existing node.tpl.php file to node--{node_id}.tpl.php.
If you want to override the page template only for front page then you can create page--front.tpl.php file and copy the page.tpl.php file and then modify it as per your requirement.
Still you have issues, then use hook_preprocess_page() in template.php and use dpm() function to find the theme_suggestions that will give the sequence of execution of templates file.
you can use node-{nodeid}.tpl.php
It may help you.
Source: https://drupal.stackexchange.com/questions/39710/how-do-i-define-a-template-file-for-a-specific-node-id
Thanks
Samit K
samitkhulve.com

How should I handle forms in CakePHP?

I'm studying CakePHP. I read a CakePHP book, and web tutorials, but I still don't get some basic things:
I see people always create a form in View with $form->create. Can I use an HTML form like normal, or must do exactly like people do?
When a form is created in login.ctp with this code:
echo $form->create('User', array('method' => 'POST', 'action' => 'login'));
echo $form->input('email');
echo $form->input('password');
echo $form->input(array('type' => 'submit'));
echo $form->end('Login');
When I click the submit button, will the data be passed to the function login() in the Controller class?
Edited :
I tried this :
<?php
$this->Form->create("Test");
$this->Form->input("stuId",array('class'=>'inputField', 'placeholder'=>'SVxxxxxxxx'));
$this->Form->input("stuName",array('class'=>'inputField', 'name'=>'stuName'));
$this->Form->end();
?>
But it show nothing ? what is the problem :(
But it show nothing ? what is the problem :(
You have to use echo as in your first code snippet:
echo $this->Form->create("Test");
echo ...
You can use HTML for anything you want, but you'd be losing a big advantage of the CakePHP framework. The Cake HTML and form helpers help to future-proof your code. You also get the benefit of Cake's implementation of best practices in web coding. I fully recommend using those helpers.
The form data is passed to $this->request->data.
Yes, the parameters will be passed to login method.
I see $form being used in the form there, it appears you are using older version of cakephp (if $form has been instantiated with $this->Form then you are fine)
The FormHelper does lot of automagic for us and it also provides us means for added security.
I would reckon you to go with The Blog tutorial

tag cloud from database

I store tags in a table which is entered by users of my site. So basically I just need to run,
<?php echo $thing->tags; ?>
and this will return all the tags for that thing. Right now it just spits out a list of the tags like "bananas apples fruit, blueberries", where the tags are both comma and space delimited. I know that in order to create a tag cloud I need to spit out the tags to a ul and then run a jquery script on the li's. I think if I can get my tags to appear in the form,
<ul>
<li>bananas</li>
<li>apples</li>
<li>fruit</li>
<li>blueberries</li>
</ul>
Then I will be able to write a jquery script to put each of these elements in little tag cloud boxes. But I don't know how to get the tags to appear in this form. When I echo the tags can I apply some php function which will do this for me?
UPDATE: I used preg_split,
<?php $tags=preg_split("/[\s,]+/", "$things->tags"); var_dump($tags);?>
And now they are listed in an array -- comma and space delimited. My only challenge now is to surround these elements of the array in li tags.
Okay, I figured this one out on my own. After creating the array as mentioned above, I used
<?php foreach ($tags as $row) : ?>
<li><? echo $row[0]; ?></li>
<li><? echo $row[1]; ?></li>
<li><? echo $row[2]; ?></li>
<? endforeach; ?>
etc.

how to select multiple entries from a auto complete box in cakephp?

I have an auto complete box which is populated with the list of users of the application. It is working fine with the box listing the users.
But I am able to select only one user. How to select multiple users from the list ?
And also how to save the selected user's names in a variable or an array?
EDIT
I am using the built-in auto complete feature of the CakePHP framework. This is the action in the controller which generates the auto complete text box.
function autoComplete()
{
$this->set('users',$this->User->find('all',array(
'fields'=>array('User.id','User.name'),
'conditions'=>array('User.name LIKE' => $this->data['User']['name'].'%'))));
$this->layout = "ajax";
}
This is the auto_complete.ctp file
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user['User']['name']; ?></li>
<?php endforeach; ?>
</ul>
And this is the view where I have the auto complete box:
<?php echo $form->create('User', array('url' => '/forms/share')); ?>
<?php echo $ajax->autoComplete('User.name', '/forms/autoComplete');?>
<?php echo $form->end('Share');?>
In the auto complete box, I am able to select only one user name. how can I select multiple users with a comma or space separator?
I don't think the AjaxHelper can produce a multi-selection auto-complete box, it's not what it's designed to do. I'm afraid you'll have to roll your own solution. Since you're already getting a nice list via Ajax that shouldn't be too much trouble.
If you want something like the Stack Overflow tag box you can probably get by by placing a few Javascript callbacks in the Helper, if you're looking for a checkbox based list you'll need to do your own.

Resources