Note that there are some explanatory texts on larger screens.

plurals
  1. POdisplaying data using a query in a controller - cake php framework
    primarykey
    data
    text
    <p>Hy guys, I started to study cake php framework (version 2.0) and when i finished to read the blog tutorial i tried to do some experiments in particular my problem is "Is it possible to create a query in the model then execute it in the controller and in the end display the result of the query in a view in this way?</p> <p>this is the file post.php(the model):</p> <pre><code>&lt;?php class Post extends AppModel { public $name='Post'; } ?&gt; </code></pre> <p>this is posts_controller(the controller):</p> <pre><code>&lt;?php class PostsController extends AppController { public $helpers = array ('Html','Form'); public $name = 'Posts'; public $name = 'Articles'; function index() { //$this-&gt;set('posts', $this-&gt;Post-&gt;find('all')); $sql="select * from posts"; $this-&gt;set('Articles',$this-&gt;Post-&gt;query($sql)); } } ?&gt; </code></pre> <p>The Question is if I declare for the second time $name I obtain an error from cake, in this case, Which is the correct name to set a variable that contain a posts arrays (the databse is the same of the blog tutorial) and the second question is How can I display the data obtained from the query in the index.ctp?? in the example I iterate in this way </p> <pre><code>&lt;?php foreach ($posts as $post): ?&gt; </code></pre> <p>and to obtain an element I have to write </p> <pre><code>&lt;?php echo $post['Post']['title']?&gt; </code></pre> <p>but in my case?</p> <p>And Is it possible to declare a function tha returns a result of a query in a model, then call it in a controller and display the data in a someview.ctp?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload