Jump to content
MakeWebGames

smarty templating system


Recommended Posts

Posted

my code for a sectioned loop

 

$data = array('name' => 'tom', 'phone' => '55a35-3425');
$data = array('name' => 'tommy', 'phone' => '55a35-3425');

$smarty->assign('users', $data);

 

my template side

 

<table>
{section name=mysec loop=$users}
{strip}
  <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
     <td>{$users[mysec].name}</td>
     <td>{$users[mysec].phone}</td>
  </tr>
{/strip}
{/section}
</table>

 

any idea why it shows nothing?

Posted

Re: smarty templating system

Try

<table>
{foreach from=$users name="users"}
  <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
     <td>{$users.name|htmlspecialchars}</td>
     <td>{$users.phone}</td>
  </tr>
{/foreach}
</table>
Posted

Re: smarty templating system

yeah I seen that. but i need to loop through records in a mySQL DB and I cna't really put them all into the array at once by doing that.

Posted

Re: smarty templating system

Then you will do something like this..

 

<?php
include_once 'smarty.configfile.php';
include_once 'somethingelse.php';
//this is a function i use for inside db class.
function GetAll($query) {
$rs = $this->Execute($query);
if( !is_resource($rs) )
	return false;
$rows = array(); 
while ($row = mysql_fetch_assoc($rs)) {
	$rows[] = $row;
}
mysql_free_result($rs);
return $rows;
}
//Then you do something like..
$users = $db->GetAll('SELECT * FROM `users`');
$smarty->assign('users', $users);

 

<table>
{foreach from=$users name="users"}
  <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
     <td>{$users.name|htmlspecialchars}</td>
     <td>{$users.phone}</td>
  </tr>
{/foreach}
</table>

 

Hope it helps.. XD

Posted

Re: smarty templating system

Or maybe something like..

$users = array();
$sql = mysql_query('SELECT * FROM `users`;');
if ( mysql_num_rows($sq) > 0 ) {
while ( $row = mysql_fetch_assoc($sql) )
       $users[] = $row;
}
$smarty->assign('users', $users);

But im not 100% sure tho. lol

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...