<?php


function smarty_function_db_fetchrows( $params, &$smarty )
{
	extract( $params );
	$table = !empty( $table ) ? trim( $table ) : "";
	$items = !empty( $items ) ? trim( $items ) : "";
	$condition = !empty( $condition ) ? trim( $condition ) : "";
	$orderby = !empty( $orderby ) ? trim( $orderby ) : "";
	$count = !empty( $count ) ? intval( $count ) : "";
	$varname = !empty( $varname ) ? trim( $varname ) : "";
	$necessaryParams = array( "table", "items", "count", "varname" );
	$paramCount = count( $necessaryParams );
	$i = 0;
	for ( ;	$i < $paramCount;	++$i	)
	{
		if ( !isset( $necessaryParams[$i] ) )
		{
			$smarty->trigger_error( "db_FetchRows: missing '".$necessaryParams[$i]."' parameter" );
		}
		else
		{
			$$necessaryParams[$i] = trim( $$necessaryParams[$i] );
		}
	}
	$sqlCondition = "";
	$condition = trim( $condition );
	if ( !empty( $condition ) )
	{
		$sqlCondition = "WHERE (".$condition.")";
	}
	$sqlOrderby = "";
	$orderby = trim( $orderby );
	if ( !empty( $orderby ) )
	{
		$sqlOrderby = "ORDER BY ".$orderby;
	}
	$sqlLimit = "";
	if ( !empty( $count ) )
	{
		$sqlLimit = "LIMIT ".$count;
	}
	global $db;
	global $gDb;
	$table = $gDb['prefix'].$table;
	$sql = "SELECT ".$items." FROM {$table} {$sqlCondition} {$sqlOrderby} {$sqlLimit}";
	$arr = $db->fetchrows( $sql );
	$smarty->assign( $varname, $arr );
	unset( $arr );
}

?>
