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

Insert Function not returning the value on tpl file

 
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
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Fri Apr 24, 2015 12:10 am    Post subject: Insert Function not returning the value on tpl file Reply with quote

I am using the latest version 3.1.21. I have a insert function, when I call it on my template, it does not show. What could be the reason?

my function
Code:
   function insert_get_status($fetch){
      global $con;
      $Query = "SELECT `status` FROM `pay` WHERE `id` = ".$con->qstr($fetch[id])." LIMIT 1";
      $results = $con->Execute($Query);
      
      if($fetch['id'] == "0"){
         $pay = "Reversed";
      } else {
         $pay = $results->fields['status'];
      }
      return $pay;
   }


my template call:

Code:
{insert name=get_status value=fetch id=$results[i].id assign=stat}{$stat}


Please help
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Apr 24, 2015 2:05 am    Post subject: Reply with quote

Add a var_dump($results); to your function to check what your query returns.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Fri Apr 24, 2015 2:17 am    Post subject: Reply with quote

U.Tews wrote:
Add a var_dump($results); to your function to check what your query returns.


It throws me this error for var_dump

Quote:
Fatal error: Can't use function return value in write context in /home/....


by the way, same function shows up on my admin theme, but NOT on front theme and few admin theme pages. I am unable to understand the issue. When I echo the sql query, its perfectly querying the data.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Apr 24, 2015 11:56 am    Post subject: Reply with quote

Have you done it like this

Code:
   function insert_get_status($fetch){
      global $con;
      $Query = "SELECT `status` FROM `pay` WHERE `id` = ".$con->qstr($fetch[id])." LIMIT 1";
      $results = $con->Execute($Query);
      var_dump($results);
       
      if($fetch['id'] == "0"){
         $pay = "Reversed";
      } else {
         $pay = $results->fields['status'];
      }
      return $pay;
   }


Other thing you can do is set $smarty->debugging = true;
Check in the debug window if $stat has any value assigned.
If you call {insert} in a subtemplate add {debug} at the end of the template file instead.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Fri Apr 24, 2015 6:53 pm    Post subject: Reply with quote

When I used {debug}, I got the following values:

Code:

Smarty_Variable Object (3)
->value = null
->nocache = true
->scope = "file:dashboard.tpl"


When I used the var_dump($results); as suggested, it is a perfect query

Code:
object(ADORecordSet_mysql)#798 (29) { ["databaseType"]=> string(5) "mysql" ["canSeek"]=> bool(true) ["dataProvider"]=> string(6) "native" ["fields"]=> bool(false) ["blobSize"]=> int(100) ["sql"]=> string(77) "SELECT `status` FROM `pay` WHERE `id` = '1' LIMIT 1" ["EOF"]=> bool(true) ["emptyTimeStamp"]=> string(6) " " ["emptyDate"]=> string(6) " " ["debug"]=> bool(false) ["timeCreated"]=> int(0) ["bind"]=> bool(false) ["fetchMode"]=> int(1) ["connection"]=> object(ADODB_mysql)#4 (88) { ["databaseType"]=> string(5) "mysql" ["dataProvider"]=> string(5) "mysql" ["hasInsertID"]=> bool(true) ["hasAffectedRows"]=> bool(true)



I dont understand why am i getting a null value in my debug console.

The same function is called and showing on my admin page, result is as expected but not on front user page.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Apr 26, 2015 3:54 am    Post subject: Reply with quote

As you can see in the var_dump $result->fields is false and not an array.
So the query did fail.

As $result->fields['status'] is undefined it's value is returned as null.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Sun Apr 26, 2015 4:21 am    Post subject: Reply with quote

Okay, thanks, I will look into it deeply now.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Jun 08, 2015 6:25 pm    Post subject: Reply with quote

Am back to the issue again, now I found out the real reason why it's not been working on my templates.

My directory structure is like this.

My PHP file
root/admin/test.php

My Insert function directory path
root/functions/function.php

My template file path
root/template/theme/admin/admin-sub/test.tpl

In theme/admin/ folder, my main template file is in root/template/theme/admin/ folder and I include all sub-template files in admin-sub folder.

Which ever insert function being called in root/template/theme/admin/ folder, it is working, where as if I call the function from root/template/theme/admin/admin-sub/ folder, then the insert function wont work.

Does smarty have any restrictions on deep directories?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jun 09, 2015 5:09 pm    Post subject: Reply with quote

You did not read my previous reply.

The var_dump of $result in you insert function show that the query is not returning a valid result.
$result->fields is false.

$result->fields['status'] is undefined -> your function returns a null value.

This has nothing to do with your folder structure.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Tue Jun 09, 2015 7:30 pm    Post subject: Reply with quote

The same very function works in 3rd level directory but not on 5th directory when I include 5th directory template file in 3rd directory template file and call the insert function.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jun 09, 2015 8:18 pm    Post subject: Reply with quote

Did you check the var_dump $result->fields value for all cases?
Please do that first.
Why does the query fail. With $result->fields == false;
In your {insert} tag you use two parameter. value = fetch ???? it does not pass a variable. You pass also id , but your function has only one parameter.
Something is screwed up.

The directory levels should not be a problem.
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