0

So I'm working on this project, and I'm at the point of the pagination system. Whenever a page is called, it goes the following way:

The index page searches a page, according to the $_GET variable:

if (isset($_GET['page']))
{
    $tpl->parsePage($_GET['page']);
}
    else
{
    $tpl->parsePage('index');
}

Then the pages' name goes trough the parsePage function in my tpl class:

public function parsePage($pagename)
    {
        $pageid = $this->getPageIdByName($pagename);
        $page = $this->shorts(new Page($pageid));
        return $page;
    } 

It gets the pages' ID by using a function, and then creates a new page. In the page class, the pages' content is fetched from my database:

public function __construct($pageid)
    {
        $pageid = $pageid['id'];
        $query = DB::$conn->prepare('SELECT * FROM pages WHERE id = :id LIMIT 1');
        $query->bindParam(':id', $pageid, \PDO::PARAM_INT);
        $query->execute();
        $pageInfo = $query->fetch(\PDO::FETCH_ASSOC);

        $this->pageid = $pageInfo['id'];
        $this->title = $pageInfo['title'];
        $this->authors = $pageInfo['authors'];
        $this->contents = $pageInfo['contents'];
        $this->publish_time = $pageInfo['publish_time'];
        $this->edit_time = $pageInfo['edit_time'];
        $this->edits = $pageInfo['edits'];
        $this->editor = $pageInfo['editor'];
        $this->hidden = $pageInfo['hidden'];
        $this->parent = $pageInfo['parent'];
        $this->category = $pageInfo['category'];
    }

then the $this->contents data goes through a function which replaces all parameters with their assigned values:

private function shorts($content)
{
    if(isset($_SESSION['user']))
    {
        $this->parameter = array_merge($this->parameter, $this->user);
    }
    $this->parameter = array_merge($this->parameter, $this->language, $this->system);
    return $this->output = str_replace(array_keys($this->parameter), array_values($this->parameter), $content->contents); // Line 34

}

And this is where it goes wrong.. When I use this way (with $content->contents) the output is the following:

Notice: Array to string conversion in C:\xampp2\htdocs\application\classes\class.tpl.php on line 34

First thing that popped into my mind: $content['contents'], but then the output is the following:

Fatal error: Cannot use object of type C_Red\Template\Page as array in C:\xampp2\htdocs\application\classes\class.tpl.php on line 34 

My knowledge stopped here...

I've marked line 34 in the shorts function. I hope that the question is clear enough to understand, but I didn't really know how to describe it, since it's a pretty vague error.

Thanks.

1 Answer 1

1

$content is the object. $content->contents is an array.

So $content['contents'] won't work, because $content is an non-iterable object. I don't know the structure of the Page class, but according to what you're saying, maybe this will work:

$content->contents['contents']
Sign up to request clarification or add additional context in comments.

5 Comments

When I var_dump on $content->contents it returns": string(1736) "Some lorem ipsum", which would point out that $content->contents isn't an array right?
The only arrays are in the constructor and in the shorts function, which are in the main post.
Can you execute a var_dump on $this->output too? Before line 34.
Returns NULL. I didn't define $this->output yet
The last thing I can think of is that $this->parameter is a multi-dimensional array => each value in array_values delivers also an array that triggers the notice. Can you var_dump $this->parameters? If this is not the case, I also ran out of options...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.