Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Can't create TPL Function from PHP Code

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
alex809
Smarty n00b


Joined: 20 Dec 2015
Posts: 4

PostPosted: Sun Dec 20, 2015 7:13 am    Post subject: Can't create TPL Function from PHP Code Reply with quote

Hi guys,
So here's my issue.

I have created a function to run an SQL query, output the data and that seems fine, Problem is i can't do anything with it now since i can't make the output to the templates work.

PHP Code:
Code:

   function getCategories() {
      try {
         foreach($this->pdo->query("SELECT categories.cat_id, categories.cat_name, categories.cat_description FROM categories") as $row) {
            $rows[] = $row;
         }
      } catch(PDOException $e) {
         print "Error!:" . $e->getMessage();
         return false;
      }
      return $rows;
      $smarty = new Smarty();
      $smarty->assign('categories', $rows);
   }


Which simply returns this: http://i.imgur.com/VyzGTqv.png

Replacing return $rows; with return $smarty->assign('categories', $rows); simply renders a blank page.

I'm using this in the template: {$categories.cat_name}

Can anyone help me fix this? Lol, It's driving me nuts.

I'm trying to rewrite a vulnerable crappy forum script using Smarty so it's a base worth trying to develop a script off of. It's literally just categories, topics and a login / register system atm that I'm trying to rewrite using Smarty.

Thanks Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 21, 2015 1:03 am    Post subject: Reply with quote

You can't do it because you are doing it wrong.
You're creating a Smarty object inside a class method, which is then lost when your method exits. This is wrong on so many levels, I don't even know where to start. May be this presentation will point just a few mistakes in your design.
Also, your query code is wrong, too.
What happened to the fetchAll() ?
Not to mention the bogus try-catch block.
If you are not going to fix the exception you've caught, then don't catch it!…
Back to top
View user's profile Send private message
alex809
Smarty n00b


Joined: 20 Dec 2015
Posts: 4

PostPosted: Mon Dec 21, 2015 2:28 am    Post subject: Reply with quote

AnrDaemon wrote:
You can't do it because you are doing it wrong.
You're creating a Smarty object inside a class method, which is then lost when your method exits. This is wrong on so many levels, I don't even know where to start. May be this presentation will point just a few mistakes in your design.
Also, your query code is wrong, too.
What happened to the fetchAll() ?
Not to mention the bogus try-catch block.
If you are not going to fix the exception you've caught, then don' catch it!…


Maybe because i don't know PHP and I'm trying to learn?
I've based that code off of the Smarty V2 Sample App...

Just telling me its all wrong and sending me to a link doesn't help anybody... Perhaps explaining what I've some wrong and showing how its done correctly would fix my noob ass mistake..
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 21, 2015 1:40 pm    Post subject: Reply with quote

If you are learning PHP, then go get a book.
Your main mistake is so basic, it even covered in the documentation.
Back to top
View user's profile Send private message
alex809
Smarty n00b


Joined: 20 Dec 2015
Posts: 4

PostPosted: Mon Dec 21, 2015 5:18 pm    Post subject: Reply with quote

AnrDaemon wrote:
If you are learning PHP, then go get a book.
Your main mistake is so basic, it even covered in the documentation.


If your here being a prick maybe you should get a life? It is covered in the docs. http://www.smarty.net/sampleapp3 scroll down to getEntries.

So if your not going to help me then please gtfo. Damn trolls.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 21, 2015 6:24 pm    Post subject: Reply with quote

The example is wrong. Whoever wrote it should be set on fire.
Back to top
View user's profile Send private message
alex809
Smarty n00b


Joined: 20 Dec 2015
Posts: 4

PostPosted: Mon Dec 21, 2015 8:07 pm    Post subject: Reply with quote

AnrDaemon wrote:
The example is wrong. Whoever wrote it should be set on fire.


Lol, So are you gonna actually help me or are you gonna complain about my code and complain about the example code...
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Dec 21, 2015 8:20 pm    Post subject: Reply with quote

Read my first post.
I already explained all your mistakes in detail.
If you want ME to TEACH YOU PHP, then pay for my time.
Or get a book already.

P.S.
Also, the example you linked does not have the first issue (the actual problem), only minor issues like stupid use of PDO, or bogus try-catch block.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP