Undefined Variable...

necm1

Member
Jan 27, 2014
111
16
Good day dear community, I currently have a small problem. I always get the following error message: Notice: Undefined variable: index in C: \ xampp \ htdocs \ tpl \ ghost \ index.tpl.php on line 51

I use the template system of Quckstar. I have in my index.php the following code:
Code:
<?php

/**
* @author Madly
* @copyright 2014
*/

include("config.php");

//Index Template laden
$tpl->page("index");
$tpl->display();
$index = new Main($mysqli);
?>

And in my index.tpl.php (line 51):
Code:
<?php
foreach($index->getAllNews("2") as $n) {
?>
<article class="post tag">
<header class="post-header">
<h2 class="post-title"><a href="/post/<?php echo $n['postID']; ?>"><?php echo $n['postTitle']; ?></a></h2>
<section class="post-meta">
<img class="author-img" src="<?php echo $n['postAuthorImage']; ?>" alt="Author image"/>
<span class="author-name"><?php echo $n['postAuthor']; ?></span>
&bull;
<time datetime="<?php echo date("d M Y", $n['postDate']); ?>"><?php echo date("d M Y", $n['postDate']); ?></time>
</section>
</header>
<section class="post-excerpt">
<?php echo $n['postShortText']; ?>
</section>
<footer class="more-links">
<a class="read-more button-save" href="/post/<?php echo $n['postID']; ?>">Weiterlesen</a>
</footer>
</article>
<?php } ?>

TPL System:
Where is the mistake? : S

EDIT: Funny, the define BLOG_SITENAME he gets from the config. : S
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
PHP:
$index->getAllNews("2")

$index isn't defined, you are defining the variable after the template is loaded, it has to be defined before.
 

Vanish

Rising Java Developer
Dec 8, 2013
630
94
Use this
PHP:
<?php
/**
* @author Madly
* @copyright TPL System by Quackstar
*/
class Template
{
    public $theme;
    public $page;
    public $content;
    public $vars;
  
    public function __construct()
    {
        $this->vars = array();
    }
  
    public function theme($theme)
    {
        $this->theme = $theme;
    }
  
    public function page($page)
    {
        $this->page = $page;
    }
  
    public function param($key, $variable)
    {
                $this->vars["[" . $key . "]"] = $variable;
    }
  
    public function display()
    {           
                ob_start();
                require "tpl/" . $this->theme . "/" . $this->page . ".tpl.php";
        $this->content = ob_get_contents();
                foreach ($this->vars as $key => $value)
        {
                        $this->content = str_replace($key, $value, $this->content);
                }
                ob_end_clean();
        echo $this->content;
    }

?>
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
Change
PHP:
$tpl->page("index");
$tpl->display();
$index = new Main($mysqli);
to
PHP:
$index = new Main($mysqli);
$tpl->page("index");
$tpl->display();
 

GarettM

Posting Freak
Aug 5, 2010
833
136
Add this and post the results
PHP:
global $index:
echo var_dump( $index );
exit;
Also for this project I'd advise against a template engine as your limiting your self
 

necm1

Member
Jan 27, 2014
111
16
Add this and post the results
PHP:
global $index:
echo var_dump( $index );
exit;
Also for this project I'd advise against a template engine as your limiting your self

Could you possibly get Skype online?
EDIT:
 
Last edited:

Users who are viewing this thread

Top