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

calling mysql query from template

 
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
ashusmarty
Smarty n00b


Joined: 30 Apr 2003
Posts: 4

PostPosted: Thu May 08, 2003 10:27 am    Post subject: calling mysql query from template Reply with quote

Hi,
code to call mysql query from template.
suggestion and comment invited.

function.mysql.php

Code:

<?php /*----plugin file Name:function.mysql.php || put this into plugin folder---*/
mysql_connect("127.0.0.1","root","") or die("Could not connect to HOST 127.0.0.1");
mysql_select_db("test") or die("Could not select database");

function smarty_function_mysql($params, &$smarty){
    extract($params);
    if (empty($sql)){
        $smarty->trigger_error("usage :{mysql sqlresult=list sql=''select * from gallery''}");
        return;
    }
    if (!in_array('sqlresult', array_keys($params))){
        $smarty->trigger_error(" usage :{mysql sqlresult=list sql=''select * from gallery''}");
        return;
    }
      $qtype = substr (trim($sql), 0, 6);
      $rs = mysql_query($sql);
      if($qtype=="select"){
         while($row=mysql_fetch_assoc($rs)){
            $myList[]=$row;
            $smarty->assign($sqlresult,$myList);
         }
      }
      if($qtype=="insert"){$smarty->assign($sqlresult,$rs);}
      if($qtype=="update"){$smarty->assign($sqlresult,$rs);}
      if($qtype=="delete"){$smarty->assign($sqlresult,$rs);}
}?>


index.tpl
Code:

{*----------------------index.tpl----------------------------*}
{include file="header.tpl" title=foo}
{include file="testPlugin.tpl"}


testPlugin.tpl
Code:

{mysql sqlresult=list sql="select * from dbgallery" }
{section name=addr loop=$list}
   {$list[addr].name}<hr>
   {mysql sqlresult=list2 sql="select * from dbitem where id='$list[addr].sno'"}
   {section name=add loop=$list2}
      {$list2[add].sno}-::-{$list2[add].name}<hr>
   {/section}
{/section}


database
Code:

-------------------------------
dbgallery   
sno name 
1   a      
2   b      
          
dbitem
sno id name
1   1  c
3   2  n
4   2  e
2   1  b
-------------------------------
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Thu May 08, 2003 6:10 pm    Post subject: Reply with quote

execution of SQL queries in templates is
NOT
NOT
NOT
NOT
NOT
correct as it does
NOT
NOT
NOT
NOT
NOT
follow the idea of smarty and is generally
NOT
NOT
NOT
NOT
NOT
a good idea as things like these should be done in your application logic.

sorry for the demur... Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 6:25 pm    Post subject: Reply with quote

Ahhh, I thought the Wom.Bat might be by to say hello! (hi W.B!)

Don't feel too bad or accept everything that others say. Here, see what you can make of this thread.

I personally don't think its the worst thing that can happen, and for a lot of sites, it makes things easier--to start with anyways. It would be easier to swallow if the code was split into two separate templates each laoded separately. That way you can still say your application and formatting logics are separated.

On-the-other-hand, I've yet to see bad advice from Wom.Bat Smile
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Thu May 08, 2003 7:46 pm    Post subject: Reply with quote

hehe, boots :wave: Smile

ashusmarty: I just wanted to stress that things like these assists bad coding style Wink you know... one day, you'll find yourself being greedy for seperation of presentation and application logic, even if it's more difficult and takes longer to implement what you want to achieve. What you created doesn't aim on quick "get me count(foo) from table bar", but rather on migrating essential parts of your business logic out into the template, and that's really not a good idea. You should always do this in your applications, and if you like modularity and flexibility, create a file foo.php, throw your code into it, make a template foo.tpl and include the whole stuff via {include_php file="foo.php"}... it's as simple as that, and if you force yourself to cleanly seperate things and use Smarty in it's intended way, I promise you it'll make you both happy and satisfied Wink

so far,
David
Back to top
View user's profile Send private message
ashusmarty
Smarty n00b


Joined: 30 Apr 2003
Posts: 4

PostPosted: Fri May 09, 2003 8:34 am    Post subject: limitation Reply with quote

Hi
I agree. Embarassed A bad idea .

but this is how i solved my loop in loop problem.
Note: see 'loop in loop' problem posted by me
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