Now, let make function flexible for usage in frontend and admin zone:
PHP Code:
function article_read($article_id, $frontend = false)
{
$sql = "SELECT article_name, summary, author, article_content, notes, date_added, status";
$sql .= " FROM articles";
$sql .= " WHERE articleID = '".$article_id."'";
if($frontend)
$sql .= " AND Status = '1'";
$res = my_query($sql);
if(mysql_num_rows($res) != 0)
{
$num = mysql_fetch_assoc($res);
return $num;
}
else
{
return false;
}
}
$frontend is an optional parametar for the above function, which means that function could be call with or without it. When it's called without it (in admin zone), it will assign false to $frontend by default.
Now when calling funtion in frontend, we will call it this way:
PHP Code:
$article = article_read($article_id, true)
This way we are sure that visitors don't have access to articles which are not approved.
This is also a good example for seeing purpose of writting functions.
Bookmarks