ive been trying to build a content management system and have written functions for the menu, actual page content and other stuff.
i echoed out the html and content in the form of links and structure from within the function. A friend told me it is bad practice to echo anything out in a function and it should be returned?
Trouble is returning it is giving me some really weired results:
this is an example for the navigation.
first is the original:
function navigation($cid){
include("dbconnect.php");
if(!empty($cid)){
$sql=mysql_query("select * from Content where Cat_ID='{$cid}'",$connection);
echo "<fieldset class=\"scat\"><legend>Sub Categories</legend>\n";
echo "<ul class=\"nav\">\n";
while($nav=mysql_fetch_array($sql)){
echo"
<li><a href=\"index.php?cid={$nav['Cat_ID']}&pid={$nav['Page_ID']}\">{$nav['menu']}</a></li>\n
";
}
echo "</ul>\n";
echo "</fieldset>\n";
}elseif(empty($cid)){
category("list","","sub_cat");
}
}
this is the return example. It is really simplified just for example:
function main_nav(){
$con=dbconnect();
$sql=mysql_query("select * from content where Cat_ID='2'",$con);
while($row=mysql_fetch_array($sql)){
$dd= $row;
}
return $dd;
}
to call the function with the return i did this:
<?php
include("functions.php");
$row2=main_nav();
foreach($row2 as $row){
echo $row['Page_ID']."<br>";
}
?>
it is givving me some weired results - very inconsistent, or if i change it and rather put $row into $dd it only returns one value?
any pointers?