Forum Moderators: coopster
For example...
function GetUserProfile($userID){
$query = 'SELECT * FROM users WHERE userID='.$userID;
.......
blah blah blah
.......
$user = mysqli->fetch_assoc();
return $user;
}
function FormatUserProfile($user, $showDesc = 'false'){
$profile = '<h1>'.$user['userName'].'</h1>';
if($showDesc){
$profile .= '<div>'.$user['description'].'</div>';
}
return $profile;
}
...
So if i had a function to solely gather information, and another function to solely format that gathered information.
Mainly because I will be showing the same data on different pages, but Different pages show different data, like a search would only bring up the users name, where as the users profile page would bring up the username and the description for example.
Is that good practice, or is there a better way to do this?
So for example, let's say you have data that you get back from a database query, you could do something like this:
$smarty->assign('results', $obj->fetchData());
And then when you are ready to print that data in a formatted way, you would use a template file that could look something like the following:
<ul>
{foreach from=$results item=result}
<li>{$result}</li>
{/foreach}
</ul>
And now it would print the data in a nice list. Then if you needed the same data on a different page displayed differently, you can do this, using another template similar to the one above.
In any case, it doesn't have to be Smarty that you use, but I prefer, IMO, to separate the "data" from the "formatting".