Am I using smarty right?

Soldato
Joined
2 May 2004
Posts
19,950
Hi,

I've been playing around with Smarty and I've come up with this test:

Code:
<?php

include 'connection.php';

require '../libs/Smarty.class.php';

$smarty = new Smarty;

$sql = "SELECT * FROM Users";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
	$username = $row['username'];
	$smarty->assign('username', $username);
}

$smarty->display('index.tpl');

?>

The problem is that it only prints the last row of the table it doesn't loop it and list all the rows even though I put it in a while like I usually do.

What am I doing wrong please?

Thanks
Craig.
 
Smarty is nice with a database abstraction layer or at least something like ez_sql, where you can do:

Code:
$users = $db->get_results('
SELECT username, homepage
FROM users
');

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

$smarty->display('users.tpl');

and then leave the presentational logic to your template:

Code:
<table>
    <tr>
        <th>Name</th>
        <th>Homepage</th>
    </tr>
{foreach from=$users item=user}
    <tr>
        <td>{$user->username}</td>
        <td><a href="{$user->homepage}">{$user->homepage}</a></td>
    </tr>
{/foreach}
</table>
 
Hmm, I have this:

Code:
require '../libs/Smarty.class.php';

$smarty = new Smarty;

$users = $db->get_results('
SELECT * FROM Users
');

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

$smarty->display('index.tpl');

But it's giving the error: Fatal error: Call to a member function on a non-object in /var/www/TPLTEST/index.php on line 9

Line 9: $users = $db->get_results('


Any ideas please?

Craig.
 
Or with your current code, just assign the results to an array called $username, rather than a single string variable:

Code:
<?php

include 'connection.php';

require '../libs/Smarty.class.php';

$smarty = new Smarty;

$sql = "SELECT * FROM Users";
$result = mysql_query($sql);
$username = array();
while($row = mysql_fetch_array($result))
{
	$username[] = $row['username'];
}

$smarty->assign('username', $username);
$smarty->display('index.tpl');

?>
And then use a Smarty section/foreach loop in your template code as rob outlined :).
 
Wicked, got it working with the array from Augmented and the foreach loop from Rob. Thanks very much everyone, now I can make proper use of template systems :D
 
Hmmm, how do I print out data from multiple fields?

I tried this:

Code:
<?php

include 'includes/connection.php';

require 'includes/Smarty.class.php';

$smarty = new Smarty;

$sql = "SELECT * FROM Users";
$result = mysql_query($sql);
$username = array();
while($row = mysql_fetch_array($result))
{
	$username[] = $row['username'];
	$username[] = $row['password'];
}

$smarty->assign('username', $username);
$smarty->display('sql_query_test.tpl');

?>

tpl file:
Code:
<table>
	<tr>
		<th>Name</th>
		<th>Password</th>
	</tr>
{foreach from=$username item=user}
	<tr>
		<td>{$user->username}</td>
		<td>{$user->password}</td>
	</tr>
{/foreach}
</table>

Problem with that one is that it doesn't print anything :(

Craig.
 
The arrow symbol -> is used for variables of objects, as per PHP's standard methods of accessing variables. $username is not an object, it is an array, so you must use the array syntax: See the Smarty Manual.

Additionally, you typically assign multiple related fields using an associative array - read up on arrays in PHP.

So, you can do:
Code:
<?php

include 'includes/connection.php';

require 'includes/Smarty.class.php';

$smarty = new Smarty;

$sql = "SELECT * FROM Users";
$result = mysql_query($sql);
$users = array();
while($row = mysql_fetch_array($result))
{
  $users[] = array(
                  'username' => $row['username'],
                  'password' => $row['password']
                  );
}

$smarty->assign('users', $users);
$smarty->display('sql_query_test.tpl');

?>
There are various ways of creating associative arrays, that is just one way. Likewise in your template:
Code:
<table>
	<tr>
		<th>Name</th>
		<th>Password</th>
	</tr>
{section loop=$users name=details}
	<tr>
		<td>{$users[details].username}</td>
		<td>{$users[details].password}</td>
	</tr>
{/section}
</table>
See the Smarty manual on section loops.
 
Last edited:
Just got it working with ez sql, but it'll probably be a quicker job for me (converting a pretty big script) using the above coding as that'd require mainly copy/pasting :)

Thanks for the help everyone!!

Craig.
 
Hmm, one more thing.

I'm needing to count like this:

Code:
// There's some code above this which specifys the ID var
$ID = $field['ID'];

$sql_one = sprintf("SELECT * FROM TableName WHERE ID = %s", quote_smart($ID);
$sql_two = sprintf("SELECT * FROM DifferentTableName WHERE ID = %s", quote_smart($ID));

$result_one = mysql_query($sql_one);
$result_two = mysql_query($sql_two);

$smarty->assign('count_one', mysql_num_rows($result_one));
$smarty->assign('count_two', mysql_num_rows($result_two));

.tpl file:
Code:
<table>
  <tr>
	<th>Count one</th>
	<th>Count two</th>
  </tr>

  <tr>
	<td>{$count_one}</td>
	<td>{$count_two}</td>
  </tr>
</table>

The above tpl file will also include the code that calls out the title etc. of the row. this result could make the table 5+++ rows so I need to somehow loop the counts so they match the row.

I just can't work out how to do that >.<

I've tried with an array & section loops like I did my other query it doesn't work :(

Hope i explained that alright :S

Thanks
Craig.
 
Back
Top Bottom