Hey aerosmith,

I'm afraid your code is just a little bit off in a few places. Also,
while I'm here, as you haven't specified a table for your model to use
then it will assume the table is called `recentposts`.

class Recentpost extends AppModel (recentpost.php)
{
  var $name = 'Recentposts';
}

This should be :

var $name = 'Recentpost';

Now, for your controller :

class Recentposts extends AppController
{
  var $name = 'Recentposts';

      function recent(){
              $this->set('recentposts', $this->requestAction('/posts/index',
array('return')));
      }
}

From the look of this I'm assuming you have another controller called
Posts and a relating model called Post. Is this correct? As you have
requested the action '/posts/index', this tells Cake to look for the
Posts controller and the index action. Does this make sense?

Also, if this element is going to exist on all pages (not just the
Recentposts area) then you are going to need to pass that variable to
the view somewhere else e.g. the app_controller.php file at the /app
root.

What might be even better is placing the code you have created for
your element and putting it in the index.thtml view for Posts. Then in
your recentposts element you can simply call
requestAction('posts/index');

And finally, your element. The only thing that would change here is
Recentposts to Recentpost as that is the name of your model (when
accessing the array that is).

<table>
  <tr>
      <th>Id</th>
      <th>Title</th>
      <th>Created</th>
  </tr>

 <!-- Here's where we loop through our $posts array, printing out
post info -->
  <?php foreach ($recentposts as $post): ?>
  <tr>
      <td><?php echo $post['Recentposts']['id']; ?></td>
      <td>
          <?php echo $html->link($post['Recentposts']['title'],
"/posts/view/".$post['Recentposts']['id']); ?>
      </td>
      <td><?php echo $post['Recentposts']['created']; ?></td>
  </tr>
  <?php endforeach; ?>
      </table>

You have called the element correctly, as long as the element file is
in /views/elements/ and is called recentposts.thtml.

Now, I think that pretty much before you get a handle on Cake you're
going to have to spend some more time in the manual or trying some of
the tutorials. What you've done here is good and show's somewhat of an
understanding of how things work in the Cake MVC framework. However,
there are a few things you don't seem to have a handle on and this
will slow you up dramatically.

The documentation for 1.1.x.x is good and should be the starting point
for any budding Cake developer. The guys here in the group can only
help so much, and it is important to have done as much research and
study possible on your own so when it comes to the really hairy
problems there are people to help you out.

HTH,
mikee

On 18/01/07, aerosmith <[EMAIL PROTECTED]> wrote:

The above code is not working ... Anybody help me to find the right
solution?

On Jan 17, 2:27 pm, "aerosmith" <[EMAIL PROTECTED]> wrote:
> Thanks Mike,
>
> Let me show my code I may have missed something.
> This is the model for the element
> <?php
>
> class Recentpost extends AppModel (recentpost.php)
> {
>     var $name = 'Recentposts';
>
> }?>
>
> The following is the controller: (recentposts_controller.php)
>
> <?php
>
> class Recentposts extends AppController
> {
>     var $name = 'Recentposts';
>
>         function recent(){
>                 $this->set('recentposts', $this->requestAction('/posts/index',
> array('return')));
>         }
>
> }?>
>
> this is the element code:  (recentposts.thtml)
>
> <table>
>     <tr>
>         <th>Id</th>
>         <th>Title</th>
>         <th>Created</th>
>     </tr>
>
>    <!-- Here's where we loop through our $posts array, printing out
> post info -->
>     <?php foreach ($recentposts as $post): ?>
>     <tr>
>         <td><?php echo $post['Recentposts']['id']; ?></td>
>         <td>
>             <?php echo $html->link($post['Recentposts']['title'],
> "/posts/view/".$post['Recentposts']['id']); ?>
>         </td>
>         <td><?php echo $post['Recentposts']['created']; ?></td>
>     </tr>
>     <?php endforeach; ?>
>         </table>
>
> This is how I included the element in my default.thtml
> <?php echo $this->renderElement('recentposts');?>
>
> Is this the right way to add a recent posts block in my
> default.thtml????
>
> Thanks
>
> On Jan 16, 7:25 pm, "Mikee Freedom" <[EMAIL PROTECTED]> wrote:
>
> > morning mate,
>
> > Have you gone through the manual yet?
>
> > The blog tutorial is good, but I've got to recommend some serious
> > reading time with the manual and the Bakery.
>
> > Elements are not necessarily attached to any particular model or
> > controller, just pieces of code that may be repeated throughout your
> > site. If you would like to print news information you need to pass
> > that news data to the view before it is accessible at that level.
>
> > What you might be looking for is requestAction on another controller.
> > But that is another story entirely.
>
> > Have a bit of a look in the manual for elements and controllers /
> > requestAction and I think you should find what you neeed.
>
> > cheers,
> > mikee
>
> > On 17/01/07,aerosmith<[EMAIL PROTECTED]> wrote:
>
> > > Hi,
>
> > > I've spent the last week searching for some good "phpframework".
> > > Finally I dropped by cakephp.org and within 2 hours I did the blog
> > > tutorial. Every thing seems clear.
>
> > > Now I have a question regarding including blocks that contains dynamic
> > > data into my default.thtml.
>
> > > Here's what I've done.
>
> > > I've created an element in views/elements/ called latestnews.php
>
> > > <div class="block newsblock" id="newsblock">
> > >         <h2>Latest News</h2>
> > >         <div class="content">
> > >                 <ul>
> > >                         <?php foreach ($news as $newsitem): ?>
> > >                                 <li><?php echo 
$html->link($newsitem['News']['title'],
> > > "/news/view/".$newsitem['News']['id']);?></li>
> > >                         <?php endforeach; ?>
> > >                 </ul>
> > >         </div>
> > > </div>
>
> > > did not seem to work.
> > > the question is:
> > > Do I need to create a model and controller for each element? or I can
> > > just use a controller previously made for a page view?
> > > Is there a place where I can see a more advanced example than the blog
> > > tutorial?
>
> > > Your reaply is highly appreciated
> > > Raffi Hovhannesian
> > >http://www.hovhannesian.com


>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to