Showing an input in a view depending on role - cakephp

I have a fat view (add) with a lot of inputs (it's a form).
For a role , there is a user_id input that is necessary, but for another role it isn't necessary. Here it comes my question, I know that elements are used for this kind of things, but I would be duplicating a lot of code if I make a view with this input and another one without it. Is there any way to just replace a line of code depending on the role? How would it be?
Thank you very much in advance, have a nice day!

What's wrong with a good old if?
<?php if ($theRoleOfTheUser == 'someSpecificRole') : ?>
<div><?php echo $this->Form->input(…); ?></div>
<?php endif; ?>

Related

Trouble getting feature image from WordPress database

I built a homepage for my WordPress site from scratch, copying the style of the theme Reflex (which I actually bought). I'm doing this because this way the site is faster, and uses a lot less bandwidth.
Original site and my build
But, I'm having trouble getting the featured images, I wrote some quick code just to work it out:
<img src="'.$thumb_img.'-200x200.jpg">
I add "-200x200" at the end of the url which I got from the database. I understand this is not the best way to do this, so the question is: what's the best practice here?
Also: am I right in rewriting WordPress' frontend for performance? Is there a better approach or solution that you can think ok?
Thanks in advance!
Try this:
<?php the_post_thumbnail( array(200, 200) ); ?>
or, if you are not in the loop, try this:
<?php
$post_id = 54; //the post id that you want to display
echo get_the_post_thumbnail( $post_id, array(200, 200) );
?>
Also, please see: https://codex.wordpress.org/Function_Reference/the_post_thumbnail
and http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

cakephp print a boolean

Beginners question in cakePHP, it drives me mad, I tried so much but can't get this working in cakePHP.
I want to print 'Train station nearby' if the boolean is set.
The database has the field 'train' bit(1) when doing a
<?php echo h($property['Property']['train']); ?>
it shows 1 but in the database it is 0, WHY is it printing 1 instead of 0
<?php if($property['Property']['train'] == true ) echo 'Train station nearby'; ?>
This output works all the time, but of course it is not true!
Anyone please, is this something to do with the fact it is a bit(1) field or am I doing something wrong. Please keep in mind I am a beginner, and I did try a lot of php examples with booleans, but just can't work out why it is not working for me in cakePHP.
Thanks for looking into this.
If you are using mysql use tinyint(1) for the field which is emulated as boolean.
According to this ticket bit type fields are currently not supported by CakePHP. Instead, use the boolean type for boolean values.

How to use multiple $content_for_layout

I am new to cakephp and I think I've run into my first problem because I need 2 $content_for_layout in my default layout, like this:
<div id="left_collumn">
<?php echo $content1_for_layout ?>
</div>
<div id="right_column">
<?php echo $content2_for_layout ?>
</div>
I've searched this group and found a thread that suggest using this : http://bakery.cakephp.org/articles/rtconner/2007/08/28/anything_for_layout-making-html-from-the-view-available-to-the-layout and even though I followed the instructions for some reason this is what I always get :http://img38.imageshack.us/img38/3246/sshot1duh.jpg
Do you have any idea why this is happening?
I am not sure what you are trying to do, but maybe you could use an element instead...
http://book.cakephp.org/view/1081/Elements
Read about plugins, thats not the way! Lear to make your own plugin!
Yeah you should use elements instead. Try to go with the rules of the framework whenever you are working on it. There is no need to change the way how Cake PHP handles it, rather using the solutions which framework offers. Use Elements.
http://book.cakephp.org/view/1081/Elements

$session->flash()

I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();

problem baking view in cakephp

I've created a custom view template (see link below), which I'm using to bake my views in cakephp.
http://book.cakephp.org/view/789/Modify-default-HTML-produced-by-baked-templates
Everything works fine, but if I try to incorporate an element in my view template, such as
<?php echo $this->element('menu'); ?>
I get an error while baking the view. If I comment out this line, baking works fine.
This is not a huge problem as I can easily comment out the line and edit the views after baking them - I'm just curious why this happens.
EDIT
The error I get in bake is something like "Notice: Undefined property: ViewTask::$element in C:\xampp\xampplite\htdocs\company\app\vendors\shells\templates\views\index.ctp on line 87 ('menu')" - I guess it must be a problem with bake trying to interpret $this->element('menu') rather than just echoing it.
You're exactly right:
"I guess it must be a problem with
bake trying to interpret
$this->element('menu') rather than
just echoing it."
So echo it, e.g.
<?php echo "<?php echo \$this->element('menu'); ?>"; ?>

Resources