[smarty] database
Hoe vang ik array op in mijn template? want met section en foreach gebeuren er hele rare dingen ..
Gewijzigd op 01/01/1970 01:00:00 door Robin de Vries
welke rare dingen?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
// load language
include("lang/".$this->m_sLanguage."/".$this->m_sModule.".php");
// set-up database connection
$oDb = new database;
// getting news from database, order by posting date
$sSql = "SELECT * FROM ".prefix."news ORDER BY datetime ASC";
echo $sSql;
$sResult = $oDb->sql_query($sSql);
$aNews = $oDb->sql_array($sResult);
// assigning vars and arrays
$smarty->assign(news,$aNews);
// display
$smarty->display('news/news.tpl');
?>
// load language
include("lang/".$this->m_sLanguage."/".$this->m_sModule.".php");
// set-up database connection
$oDb = new database;
// getting news from database, order by posting date
$sSql = "SELECT * FROM ".prefix."news ORDER BY datetime ASC";
echo $sSql;
$sResult = $oDb->sql_query($sSql);
$aNews = $oDb->sql_array($sResult);
// assigning vars and arrays
$smarty->assign(news,$aNews);
// display
$smarty->display('news/news.tpl');
?>
en dit is de template:
Code (php)
1
2
3
4
5
2
3
4
5
<h1>{$LANG_NEWS_NEWS}</h1>
{section name=mijnsectie loop=$news}
{$news[mijnsectie].subject}
{/section}
{section name=mijnsectie loop=$news}
{$news[mijnsectie].subject}
{/section}
en dit de out-put:
1 2 1 W W
ofwel, van ieder veld het 1e teken ...
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
// load language
include("lang/".$this->m_sLanguage."/".$this->m_sModule.".php");
// set-up database connection
$oDb = new database;
// getting news from database, order by posting date
$sSql = "SELECT * FROM ".prefix."news ORDER BY datetime ASC";
echo $sSql;
$sResult = $oDb->sql_query($sSql);
$aNews = $oDb->sql_array($sResult);
// assigning vars and arrays
$smarty->assign(news,$aNews);
// display
$smarty->display('news/news.tpl');
?>
// load language
include("lang/".$this->m_sLanguage."/".$this->m_sModule.".php");
// set-up database connection
$oDb = new database;
// getting news from database, order by posting date
$sSql = "SELECT * FROM ".prefix."news ORDER BY datetime ASC";
echo $sSql;
$sResult = $oDb->sql_query($sSql);
$aNews = $oDb->sql_array($sResult);
// assigning vars and arrays
$smarty->assign(news,$aNews);
// display
$smarty->display('news/news.tpl');
?>
Dat is jou code, nu zie ik op lijn 15 een klein foutje. Je moet het woord news nog in deze ' tags zetten (hoe die dingen heten weet ik niet).
Dus dan krijg je:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
// load language
include("lang/".$this->m_sLanguage."/".$this->m_sModule.".php");
// set-up database connection
$oDb = new database;
// getting news from database, order by posting date
$sSql = "SELECT * FROM ".prefix."news ORDER BY datetime ASC";
echo $sSql;
$sResult = $oDb->sql_query($sSql);
$aNews = $oDb->sql_array($sResult);
// assigning vars and arrays
$smarty->assign('news',$aNews);
// display
$smarty->display('news/news.tpl');
?>
// load language
include("lang/".$this->m_sLanguage."/".$this->m_sModule.".php");
// set-up database connection
$oDb = new database;
// getting news from database, order by posting date
$sSql = "SELECT * FROM ".prefix."news ORDER BY datetime ASC";
echo $sSql;
$sResult = $oDb->sql_query($sSql);
$aNews = $oDb->sql_array($sResult);
// assigning vars and arrays
$smarty->assign('news',$aNews);
// display
$smarty->display('news/news.tpl');
?>
aangepast, maar haalt m'n probleem nog steeds niet weg, de output moet welcome zijn ipv 1 2 1 W W
ofwel 2x id, 2x begin v/d datum, 2x user, 2x subject, 2x bericht :P...
Dan is de array niet goed. Verder kan ik geen andere oplossing vinden.
toch vaag want als ik hem voor de template parser met een simpele while lus uitvoer in de php-code gaat alles wel goed :S
Array (
[0] => 1
[id] => 1
[1] => 2009-02-15 11:52:21
[datetime] => 2009-02-15 11:52:21
[2] => 1
[user] => 1
[3] => Welcome
[subject] => Welcome
[4] => Welcome at celp framework. We are still working at the framework but the design is already online :).
[text] => Welcome at celp framework. We are still working at the framework but the design is already online :).
)
maar toch zou hij wel meer als 1 teken mogen weergeven...
ps. gebruik assoc ipv array om je rijen te fetchen
Gewijzigd op 01/01/1970 01:00:00 door Terence Hersbach