php - Function istops process -
this question has answer here:
i have process works fine on it's own fails render info within function.
this works:
if ($totalrows_menu > 0) { echo "<ul>"; while($row_menu = mysql_fetch_array($menu)) { echo "<li>" . $row_menu['m3menu_item'] . "</li>"; } echo "</ul>"; } but doesn't:
function m3menu() { if ($totalrows_menu > 0) { echo "<ul>"; while($row_menu = mysql_fetch_array($menu)) { echo "<li>" . $row_menu['m3menu_item'] . "</li>"; } echo "</ul>"; } } echo m3menu();
as lex says variables not available in scope of function. can either pass them function when call it, or can use global
also, function echoing rather returning html there no need echo m3menu(); should call function m3menu();, or return html function , echo out value.
method 1
function m3menu( $totalrows_menu, $menu ) { if ($totalrows_menu > 0) { echo "<ul>"; while($row_menu = mysql_fetch_array($menu)) { echo "<li>" . $row_menu['m3menu_item'] . "</li>"; } echo "</ul>"; } } m3menu($totalrows_menu, $menu); method 2
function m3menu() { global $totalrows_menu; global $menu; if ($totalrows_menu > 0) { echo "<ul>"; while($row_menu = mysql_fetch_array($menu)) { echo "<li>" . $row_menu['m3menu_item'] . "</li>"; } echo "</ul>"; } } m3menu();
Comments
Post a Comment