View previous topic :: View next topic |
Author |
Message |
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Sun Jan 21, 2007 2:34 pm Post subject: Inserting data from a MySQL database into a Smarty template |
|
|
Hi everyone,
I have read somewhere before about how to do this but cannot find it again . 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. |
|
Back to top |
|
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Sun Jan 21, 2007 4:31 pm Post subject: |
|
|
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 |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Mon Feb 05, 2007 3:49 pm Post subject: |
|
|
TGKnIght,
The code in the tutorial in you signature doesn't work. At least I can't get it to work.
I get the error:
Code: |
Call to undefined function assign_md_array()
|
Have fun from,
Royal_Smartiness_15. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Tue Feb 06, 2007 11:44 am Post subject: |
|
|
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.
Have fun from,
Royal_Smartiness_15. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Wed Feb 07, 2007 2:13 pm Post subject: |
|
|
Yes I put it in the PHP code, and yes it was surrounded with <?php and ?>. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Tue Apr 03, 2007 1:25 pm Post subject: |
|
|
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. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Tue Apr 03, 2007 2:10 pm Post subject: |
|
|
OK I put the code into my script and refreshed but nothing happened. |
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Wed Apr 04, 2007 6:00 pm Post subject: |
|
|
TGKnIght...are you still there? |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
Royal_Smartiness_15 Smarty Rookie
Joined: 29 Dec 2006 Posts: 16
|
Posted: Wed Apr 04, 2007 10:23 pm Post subject: |
|
|
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.
Also when I enter {$sitename} into my tpl file it just displays the word Array. |
|
Back to top |
|
|