Smarty help.

Soldato
Joined
2 May 2004
Posts
19,950
Hi,

Stuck a bit on Smarty :(

I have this:

Code:
$count = array();

	$sqlcAccounts = sprintf("SELECT * FROM Accounts WHERE Category = %s", quote_smart($field['ID']));
	$sqlcItems = sprintf("SELECT * FROM Items WHERE Category = %s", quote_smart($field['ID']));
	$sqlcAccountsR = mysql_query($sqlcAccounts);
	$sqlcItemsR = mysql_query($sqlcItems);

	$count[] = array(
			'count_accounts' => mysql_num_rows($sqlcAccountsR),
			'count_items' => mysql_num_rows($sqlcItemsR)
			);

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


And the tpl file:
Code:
		{section loop=$count name=count}
		<td>{$count[count].count_accounts}</td>
		<td>{$count[count].count_items}</td>
		{/section}

What this does for some reason is prints out the results in a row so instead of this:

Column Title | Column Title
Count result | Count result
Count result | Count result

It does this:

Column Title | Column Title
Count result | Count result | Count result | Count result
same Count result | same Count result | same Count result | same Count result

Any ideas what I can do to fix this please?

Thanks
Craig.
 
hmm whats the point in the count array and the loop in your template?

wouldn't this be sufficient?

Code:
	$sqlcAccounts = sprintf("SELECT * FROM Accounts WHERE Category = %s", quote_smart($field['ID']));
	$sqlcItems = sprintf("SELECT * FROM Items WHERE Category = %s", quote_smart($field['ID']));
	$sqlcAccountsR = mysql_query($sqlcAccounts);
	$sqlcItemsR = mysql_query($sqlcItems);

$smarty->assign('count_accounts', mysql_num_rows($sqlcAccountsR));
$smarty->assign('count_items', mysql_num_rows($sqlcItemsR));

Code:
		<td>{$count_accounts}</td>
		<td>{$count_items}</td>
 
I'm fairly certain you can use a few whitelisted functions from within Smarty, and iirc count is one of them. You can, therefore, just do:

Code:
$foo = array(1, 2, 'lol', 'whatever');

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

Code:
<p>{count($foo)} items</p>

{foreach from=$foo item=lol}
<p>{$lol}</p>

Totally untested as a result of my own laziness, though.
 
count doesn't seem to exist rob :( gives an error on the script & it isn't in the Smarty DB :(

jonno.co.uk said:
hmm whats the point in the count array and the loop in your template?

wouldn't this be sufficient?

Code:
	$sqlcAccounts = sprintf("SELECT * FROM Accounts WHERE Category = %s", quote_smart($field['ID']));
	$sqlcItems = sprintf("SELECT * FROM Items WHERE Category = %s", quote_smart($field['ID']));
	$sqlcAccountsR = mysql_query($sqlcAccounts);
	$sqlcItemsR = mysql_query($sqlcItems);

$smarty->assign('count_accounts', mysql_num_rows($sqlcAccountsR));
$smarty->assign('count_items', mysql_num_rows($sqlcItemsR));

Code:
		<td>{$count_accounts}</td>
		<td>{$count_items}</td>

There will be more than one row in the table(s) that need to be counted, when I do the above only the result it outputted for the bottom table :(

The closest I have got to making it work is with the code in my first post :( But that prints all the results out the results but in a line on each row instead of in the correct places.
 
Last edited:
Hi,

Where do the SQL queries come into the count.

I put a $field['Blah']; into an array and then into a foreach on the template in a count meaning it should count shouldn't it?

Craig.
 
Well I need to count MySQL entries according to the category ID and then output them in two columns in a table and I'm not sure how to use MySQL and the count function to do this.

Craig.
 
Back
Top Bottom