0

In the controller I do this

<?php

$data = array(
    'color' => 'pink',
    'type' => 'sugar',
    'base_price' => 23.95
);
//make $color, $type, and $base_price 
//available to the view:
$this->set($data);  
?>

And in the view I directly call

<?=$color?>

But there is no thing being echo-ed. I am wrong about its use ?

  • Short tags are perfectly fine *if* you control the sever environment, *or* you are targeting PHP 5.4 and limit yourself to the now always-enabled short echo syntax. – Charles Feb 16 '12 at 08:14
  • Charles had a nice spot! Thanks Charles. – John Smith Feb 16 '12 at 08:22
  • I mean your "short" syntax leads me to delete all underscores in my real application variable names, it works. I have a good imagination :-D hahahhahaha. Thanks anyway. – John Smith Feb 16 '12 at 08:23

3 Answers3

2

Your usage of $this->set() is correct.

Most likely you do not have PHP shorttags turned on (more info). This would cause PHP to output the whole <?=$color?> block as text, which would be parsed as a malformed HTML tag and simply ignored. Check your HTML source and see what the full output is, not just what your browser is displaying.

As a remedy, <?php echo $color; ?> should work just fine.

Community
  • 1
  • 1
Farray
  • 8,290
  • 3
  • 33
  • 37
  • Thanks but it outputs only color and base_price, the type disappears. – John Smith Feb 16 '12 at 08:09
  • @JohnSmith The disappearnce of $type is strange. It could be because there is a certain variable that's clashing with $type, as far as I could guess. – mmhan Feb 16 '12 at 08:57
  • @John what version of Cake are you using? – Farray Feb 16 '12 at 15:07
  • @John Disregard my previous comment, I just checked the Book and the behavior shouldn't be different between 1.3-2.x. Most likely you use `$type` somewhere else in your view or controller and it conflicts with the value you expect to see. – Farray Feb 16 '12 at 16:52
0

As Farray pointed out, you have used the set() method incorrectly. The correct syntax is as follows:

EDIT: I misread Farrays post and indeed you have used set() correctly as it can take an associative array as it's first command. It might be worth changing the variable type to something other that $data and making use of the set method as follows:

<?php
$myarray = array(
    'color' => 'pink',
    'type' => 'sugar',
    'base_price' => 23.95
);
//make $color, $type, and $base_price 
//available to the view:
$this->set('myarray', $myarray);  
?>

Alternatively you can use a shorthand method and make use of the compact() method:

<?php
$this->set(compact('myarray'));  
?>

Inside your view, if you run pr($myarray); you should see your array neatly formatted in a recursive manner. If you do NOT see it then either:

  • There is an error in your view file that is being caught before your pr() command
  • You have PHP short tags off in your php.ini config file
  • There is a deeper underlying issue in your controller

Regards, Simon

SimonDowdles
  • 2,026
  • 4
  • 24
  • 36
-1

You guys are making it complex>Cakephp mean do coding cakefully not pain fully.Just use

<?php
$myarray = array(
    'color' => 'pink',
    'type' => 'sugar',
    'base_price' => 23.95
);
//make $color, $type, and $base_price 
//available to the view:

$this->set('viewdata', $myarray);  
?>

Grab your variable from view like this

<?php echo $viewdata['color'];

echo $viewdata['type'];


echo $viewdata['base_price'];
?>
jack
  • 473
  • 5
  • 21
  • It's hard to get more "cakeful" than using the exact example that CakePHP offers in their docs: http://book.cakephp.org/2.0/en/controllers.html?highlight=color%20pink#interacting-with-views – Farray Feb 18 '12 at 22:03
  • i left my way way of doing that..Thats all.Anyway use your methods – jack Feb 20 '12 at 06:37