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

Inserting data from a MySQL database into a Smarty template
Goto page 1, 2  Next
 
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
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Sun Jan 21, 2007 2:34 pm    Post subject: Inserting data from a MySQL database into a Smarty template Reply with quote

Hi everyone,

I have read somewhere before about how to do this but cannot find it again Sad . Anyway I want to know how I could use Smarty to access a MySQL database and extract specific data from it, then insert that data into the tpl files which would then be displayed in the users browser.

I don't know if i've explained it in an understandable way, but it is basically what I want to do lol. Laughing
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Sun Jan 21, 2007 4:31 pm    Post subject: Reply with quote

in your php code just query the database, make a (multi dimensional) array and use assign() to send it to the template. In your template you can loop over the array.
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Jan 22, 2007 2:30 pm    Post subject: Reply with quote

Hi, I have a brief walk through, check the links in my signature.
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Mon Feb 05, 2007 3:49 pm    Post subject: Reply with quote

TGKnIght,

The code in the tutorial in you signature doesn't work. At least I can't get it to work. Sad

I get the error:
Code:

Call to undefined function assign_md_array()


Have fun from,
Royal_Smartiness_15.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Feb 05, 2007 6:04 pm    Post subject: Reply with quote

.... Well... look at the error message you posted... what do u think it means?
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Tue Feb 06, 2007 11:44 am    Post subject: Reply with quote

Well I think that it could mean something like maybe the function assign_md_array(); is none existent.

But then if that is the case then PHP must not be working correctly because I can see the function clearly in my file.

I don't think very much though so I'm undoubtedly wrong lol. Very Happy

Have fun from,
Royal_Smartiness_15.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Tue Feb 06, 2007 6:21 pm    Post subject: Reply with quote

Did you put the function into the Smarty template or into your PHP file? Needs to be in the PHP file to work..

Also make sure you surround your php code with <?php ?>
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Wed Feb 07, 2007 2:13 pm    Post subject: Reply with quote

Yes I put it in the PHP code, and yes it was surrounded with <?php and ?>.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Wed Feb 07, 2007 2:33 pm    Post subject: Reply with quote

Well... This is a PHP issue and not a Smarty issue and I'm not gonna just keep stabbing in the dark until I figure out your problem...

Why not show your work and explain what you've tried...
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Tue Apr 03, 2007 1:25 pm    Post subject: Reply with quote

Sorry I haven't responded in ages but I've been busy with other commitments.

Anyway I am trying to extract some config data from a MySQL database and insert it into a smarty template.

test.lib.php:
Code:

<?php
class TestClass {
var $sql = null;
var $tpl = null;
var $error = null;

function TestFunc(){
$this->sql =& new TestClass_SQL;
$this->tpl =& new TestClass_Smarty;
}

function getConfig(){
$this->sql->query("SELECT * FROM `config`");
return $this->sql->record;
}

function displayIndex($config = array()){
$this->tpl->assign('sitename', $config);
$this->tpl->display('default/index_body.tpl');
}
}
?>


index.php
Code:

<?php
define('TEST_DIR', './');
define('SMARTY_DIR', 'includes/');
include(TEST_DIR . 'includes/test_setup.php');
$test =& new TestClass;

$_page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'main';

switch($_page){
case 'main':
default:
$test->displayIndex($test->getConfig());
break;
}
?>


index_body.tpl:
Code:

{include file="default/header.tpl" title="$sitename / Test Page"}
<td class="content">
<p>News</p>
</td></tr>
{include file="default/footer.tpl"}


And yes I have checked and there is nothing wrong with the connection to the MySQL database.

But as far as I can tell, it should work...it doesn't spit out any errors to me?

Thanks for your time, Royal_Smartiness_15. Wink
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Tue Apr 03, 2007 1:43 pm    Post subject: Reply with quote

Try adding this somewhere in the beginning of your script

Code:

ini_set('display_errors',1);
//error reporting is wierd, it represents the setting as a number
//E_ALL = 2047
//E_ALL & ~E_NOTICE = 2039
ini_set('error_reporting', 2039);

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Tue Apr 03, 2007 2:10 pm    Post subject: Reply with quote

OK I put the code into my script and refreshed but nothing happened. Sad
Back to top
View user's profile Send private message
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Wed Apr 04, 2007 6:00 pm    Post subject: Reply with quote

TGKnIght...are you still there?
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Wed Apr 04, 2007 9:14 pm    Post subject: Reply with quote

You put that into the beginning of your index.php right?

Make sure to empty out your cache and compiled directories..

Try doing an echo "TEST"; in your index.php??
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Royal_Smartiness_15
Smarty Rookie


Joined: 29 Dec 2006
Posts: 16

PostPosted: Wed Apr 04, 2007 10:23 pm    Post subject: Reply with quote

Yes I emptied out my cache and yes I added it to the top of the index.php and yes the page still loads normally, except that the MySQL stuff still don't work. Sad

Also when I enter {$sitename} into my tpl file it just displays the word Array. Confused
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
Goto page 1, 2  Next
Page 1 of 2

 
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