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.
 
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.
 
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.
 
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