Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:41:33+00:00 2026-05-30T09:41:33+00:00

I’ve started a project in CakePHP. I have no experience of it, most of

  • 0

I’ve started a project in CakePHP. I have no experience of it, most of my experience is with Symfony2.

Right now, I have built a view file, a controller, and a model for use with this project and it’s database. In one controller I want to pass one record from the database to the view for use on the homepage. However, I’m unable to pass the record as I get this error:

Notice (8): Undefined variable: content [APP\View\Contents\home.ctp,
line 42]

Here is my controller code:

function home() {
    $content = $this->Content->query("SELECT title, content FROM content WHERE id = 1;");
    $this->set('pagecontent',$content); 
}

And here is the code I’m using to display the data in the view file:

<?php $content['content']; ?>

What have I missed?


EDIT:
I have changed the <?php $content['content']; ?> to <?php echo $pagecontent['Content']['content']; ?> inline with the changes outlined in the answer from Ross. However, there is now this error:

Notice (8): Undefined index: Content [APP\View\Contents\home.ctp, line
43]

I have used the <?php echo debug($this->viewVars); ?> and this is the output from that:

 app\View\Contents\home.ctp (line 42) Array (
     [pagecontent] => Array
         (
             [0] => Array
                 (
                     [content] => Array
                         (
                             [title] => <h1>This is a title</h1>
                             [content] => <p>this is some test text</p>
                         )

                 )

         )

 )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:41:34+00:00Added an answer on May 30, 2026 at 9:41 am

    $this->set('pagecontent',$content); – this is where you are setting view variables.

    You are trying to access a variable called $content, when in fact it is stored as $pagecontent.

    Also based on Cake conventions, the key content should actually be Content, so your output should probably be:

    <h1><?php echo $pagecontent['Content']['title']; ?></h1>
    <div class="page-content">
        <?php echo $pagecontent['Content']['content']; ?>
    </div>
    

    You can always do debug($this->viewVars); to see exactly what you have access to.


    Seeing the output helps; the error is due to how cake is returning the data.

    This is likely due to you using query() rather than find() or read(). (also explains why your content key is lowercase – query() uses the table name, rather than the model name.

    Try:

    $content = $this->Content->find('first', 
                                  array('fields' => array('title', 'content'),
                                        'conditions' => array('Content.id' => 1)
                                       )
                                   );
    
    $this->set('pagecontent', $content); 
    
    
    // view
    echo $pagecontent['Content']['title'];
    echo $pagecontent['Content']['content'];
    

    should now work.

    The alternative is to just access the data through the array index:

    echo $pagecontent[0]['content']['title']

    which isn’t as “cake” like.

    to expand = query() returns data in a different structure than using find or read. There’s other ways you can do this too.

    $this->Content->id = 1;
    $this->set('pagecontent', $this->Content->read(array('title', 'content')));
    

    should also be valid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.