Why am i getting this Smarty error?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Xephon
Forum Newbie
Posts: 20
Joined: Sat May 15, 2004 10:58 pm

Why am i getting this Smarty error?

Post by Xephon »

the error
Fatal error: Call to a member function on a non-object in C:\Apache2\htdocs\yorov3\functions.php on line 21
if i make the posts diplay code into a function like this

index.php

Code: Select all

<?
  session_start();
  require_once("libs.inc.php");
  include 'db.php';
  include 'functions.php';



  if(isset($_SESSION['username'])){
  $username = $_SESSION['username'];
  }

display_news();

  if(empty($_SESSION['username']))
  {
         $smarty->assign("logged_in", 0);
  } else {
         $smarty->assign("logged_in", 1);
  }

     $smarty->display("index.html");

  ?>
and functions.php

Code: Select all

<?

  function redirect($url) {
                echo "<script language="Javascript">
                     window.location="$url"
                     </script>";
  }
  
  function display_news()
  {
      $sql = mysql_query("select * from news");
         if(mysql_num_rows($sql) >= 1)
         {
             $news = array();
             while($rows = mysql_fetch_array($sql, MYSQL_ASSOC))
             {
                 array_push($news, $rows);
             }
             $smarty->assign("news", $news);
         } else {
             $smarty->assign("news", 0);
         }
  }
I get that error

but i dont get it when i dont put the display post code as a function in an included file. like this:

Code: Select all

<?
  session_start();
  require_once("libs.inc.php");
  include 'db.php';
  



  if(isset($_SESSION['username'])){
  $username = $_SESSION['username'];
  }

  $sql = mysql_query("select * from news");
     if(mysql_num_rows($sql) >= 1)
         {
             $news = array();
             while($rows = mysql_fetch_array($sql, MYSQL_ASSOC))
             {
                 array_push($news, $rows);
             }
             $smarty->assign("news", $news);
         } else {
             $smarty->assign("news", 0);
      }

  if(empty($_SESSION['username']))
  {
         $smarty->assign("logged_in", 0);
  } else {
         $smarty->assign("logged_in", 1);
  }

     $smarty->display("index.html");

  ?>
what am i doing wrong?
Xephon
Forum Newbie
Posts: 20
Joined: Sat May 15, 2004 10:58 pm

Post by Xephon »

ok got it fixed on my own, but i dont know if it is the right way to do it or not

i added

Code: Select all

global $smarty
to my function

and now it looks like this:

Code: Select all

function display_news()
  {
      global $smarty;
      $sql = mysql_query("select news.title, news.text_body, news.post_date, users.username from users,
                          news where news.user_id = users.user_id");
         if(mysql_num_rows($sql) >= 1)
         {
             $news = array();
             while($rows = mysql_fetch_array($sql, MYSQL_ASSOC))
             {

                 array_push($news, $rows);
             }
            $smarty->assign("news", $news);
         } else {
             $smarty->assign("news", 0);
         }

  }
is there anything wrong with doing it like this? any security concerns or other such things? if so please tell me what is wrong and how to do it the right way.
Post Reply