|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mhdn2 Smarty Rookie
Joined: 12 Aug 2015 Posts: 11
|
Posted: Wed Aug 12, 2015 10:49 am Post subject: Printing tables using Smarty |
|
|
Hey there. I'm trying to get my website to print a table using Smarty. Here is the code I have so far.
Code: | $races_list = array(); // initialize array
$query = mysql_query("SELECT DISTINCT race_id FROM shifts WHERE company_id = $company_id ORDER BY race_id ASC;");
while ( $races = mysql_fetch_array($query) ) {
$races_list[$races['race_id']] = array_push(array(
'race_name' => get_race_name($races['race_id'])
));
// more PHP code
}
$smarty->assign('races', $races_list);
$smarty->display('report.html'); |
Code: | {foreach $races as $race}
<br />
<table id='table'>
<tr id='title'>
<td colspan='6'>
{$race.race_name}
</td>
</tr>
<!-- more HTML code -->
</table>
{/foreach} |
I think my array code is wrong. Any suggestions on how to fix it, or the "normal" way people do this "row by row" data building would be appreciated.
My background is actually the phpBB2 template engine (which this forum is running on : ) and that lets you send the data row by row instead of building an array first. So this array stuff is unfamiliar to me.
Thanks in advance for your help. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Aug 12, 2015 3:07 pm Post subject: |
|
|
1. Your SQL query is unsafe.
2. mysql_ family of functions is deprecated.
3. Use debug functionality instead of guesswork. |
|
Back to top |
|
mhdn2 Smarty Rookie
Joined: 12 Aug 2015 Posts: 11
|
Posted: Wed Aug 12, 2015 8:33 pm Post subject: |
|
|
1) $company_id is set by me earlier in the code. It's not a register_global.
2) What should I use instead?
3) I threw some print_r() statements in there and I am reading the error messages. That's how I know my syntax is wrong. Right now that throws "Fatal error: Only variables can be passed by reference in /home/volunteers/public_html/pages/report_smarty.php on line 64"
Also, still looking for some coding advice on how to solve the original problem :-) |
|
Back to top |
|
mhdn2 Smarty Rookie
Joined: 12 Aug 2015 Posts: 11
|
Posted: Wed Aug 12, 2015 9:42 pm Post subject: |
|
|
Got it. In case anybody was wondering, here is the solution.
Code: | $races_list = array(); // initialize array
$query = mysql_query("SELECT DISTINCT race_id FROM shifts WHERE company_id = $company_id ORDER BY race_id ASC;");
while ( $races = mysql_fetch_array($query) ) {
// creates a field in the row array not included in the MySQL query:
$races['race_name'] = get_race_name($races['race_id']);
// pushes the row array into the big array holding all the rows:
$races_list[] = $races;
// more PHP code
}
// passes the big array holding all the rows to Smarty:
$smarty->assign('races_list', $races_list);
$smarty->display('report.html'); |
Code: | {foreach item=race from=$races_list}
<br />
<table id='table'>
<tr id='title'>
<td colspan='6'>
{$race.race_name}
</td>
</tr>
<!-- more HTML code -->
</table>
{/foreach} |
I'll download some PHP books and look into mysqli. However that's a project for another day. I just wanted my template engine to work and now I've achieved that :-) |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Aug 13, 2015 12:55 am Post subject: |
|
|
PDO, not MySQLi. |
|
Back to top |
|
|
|
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
|