Joshua4550 Posted October 21, 2009 Share Posted October 21, 2009 Okay, i'm a newb to PHP, so please bare with me.... I have made a function which will set a variable depending on other variables and database data. I'm not sure whether in PHP this should even work, so if i'm TOTALLY wrong then just tell me.. okay, here's my original code that worked... // Allow the front page to display 15 recent news entries $NEWS_QUERY = 'SELECT * FROM `Sites_News` WHERE `site`=\'' . $SITENAME . '\' ORDER BY `ID` DESC LIMIT 15'; $Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error()); $Menu = mysql_fetch_array($Menu_Query); $Cat = explode(",", $Menu['category1']); if ($Cat[0] == "true") { if ($Cat[2] == "true") { $fieldset_class = "menu gold"; } else { $fieldset_class = "menu"; } $MENUS .= ' <fieldset class="' . $fieldset_class . '"> <legend>' . $Cat[1] . '</legend> <ul> '; $link = explode("^", $Menu['links1']); $url = explode("^", $Menu['urls1']); for ($i = 0; $i < count($link); $i++) { if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") { continue; } $MENUS .= ' <li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li> '; } $MENUS .= ' </ul> </fieldset> '; } Now here's when i tried to put it into a function, so i could use it later on.. (This doesn't work, it just doesn't work for some reason? :S) // Allow the front page to display 15 recent news entries $NEWS_QUERY = 'SELECT * FROM `Sites_News` WHERE `site`=\'' . $SITENAME . '\' ORDER BY `ID` DESC LIMIT 15'; $Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error()); $Menu = mysql_fetch_array($Menu_Query); function NewCat($category, $links, $urls) { $Cat = explode(",", $Menu[$category]); if ($Cat[0] == "true") { if ($Cat[2] == "true") { $fieldset_class = "menu gold"; } else { $fieldset_class = "menu"; } $MENUS .= ' <fieldset class="' . $fieldset_class . '"> <legend>' . $Cat[1] . '</legend> <ul> '; $link = explode("^", $Menu[$links]); $url = explode("^", $Menu[$urls]); for ($i = 0; $i < count($link); $i++) { if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") { continue; } $MENUS .= ' <li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li> '; } $MENUS .= ' </ul> </fieldset> '; } } Just to be clear... the database queries that appear at the very top of the code DO WORK. Anyone got any idea why my function don't work? Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/ Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Your function uses the $Menu variable from $Menu = mysql_fetch_array($Menu_Query); But its hasn't been passed to the function, also your function isn't being called or returning anything here is a totally untested example (but may work) <?php // Allow the front page to display 15 recent news entries $NEWS_QUERY = 'SELECT * FROM `Sites_News` WHERE `site`=\'' . $SITENAME . '\' ORDER BY `ID` DESC LIMIT 15'; $Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error()); $Menu = mysql_fetch_array($Menu_Query); $MENUS= NewCat($Menu, $category, $links, $urls); //call function and set returned value to $MENUS function NewCat($Menu, $category, $links, $urls) { //also passed $Menu $Cat = explode(",", $Menu[$category]); $MENUS = ""; if ($Cat[0] == "true") { if ($Cat[2] == "true") { $fieldset_class = "menu gold"; } else { $fieldset_class = "menu"; } $MENUS .= ' <fieldset class="' . $fieldset_class . '"> <legend>' . $Cat[1] . '</legend> <ul> '; $link = explode("^", $Menu[$links]); $url = explode("^", $Menu[$urls]); for ($i = 0; $i < count($link); $i++) { if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") { continue; } $MENUS .= ' <li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li> '; } $MENUS .= ' </ul> </fieldset> '; } return $MENUS; //return $MENUS } ?> EDIT: added some comments Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/#findComment-940827 Share on other sites More sharing options...
Joshua4550 Posted October 21, 2009 Author Share Posted October 21, 2009 Oh, i understand now.. I'm just so used to Java, so i'm guessing PHP functions are equivelant to String methods in Java, you have to return a value. So to call that function, i would do: $MENUS .= NewCat('categories1', 'links1', 'urls1'); (Since the variable $MENUS is in the main file which is why i was trying to use it, because i was thinking about it as if it were the same as a void in Java. So that's how i call the function, if i understand correctly? EDIT: Thanks alot, i now understand. Thanks buddy Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/#findComment-940833 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Yep.. that's correct, if solved then please click topic solved (bottom left) oh an welcome Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/#findComment-940837 Share on other sites More sharing options...
Joshua4550 Posted October 21, 2009 Author Share Posted October 21, 2009 Still seems to not work? (I changed the variable which the function returns to not confuse myself, but it doesn't work still?) <?php $Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error()); $Menu = mysql_fetch_array($Menu_Query); function NewCat($Menu, $category, $links, $urls) { //also passed $Menu $Cat = explode(",", $Menu[$category]); $string = ""; if ($Cat[0] == "true") { if ($Cat[2] == "true") { $fieldset_class = "menu gold"; } else { $fieldset_class = "menu"; } $string .= ' <fieldset class="' . $fieldset_class . '"> <legend>' . $Cat[1] . '</legend> <ul> '; $link = explode("^", $Menu[$links]); $url = explode("^", $Menu[$urls]); for ($i = 0; $i < count($link); $i++) { if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") { continue; } $string .= ' <li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li> '; } $string .= ' </ul> </fieldset> '; } return $string; } $MENUS .= NewCat($Menu, 'categories1', 'links1', 'urls1'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/#findComment-940840 Share on other sites More sharing options...
Joshua4550 Posted October 21, 2009 Author Share Posted October 21, 2009 My bad, it does. Thanks alot again, mate. Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/#findComment-940842 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Quick note/tip, Try not to put your functions in the middle of your code.. it makes it a nightmare to read later also you could get problems calling them if you loss them in a block ie <?php test(); if(false){ function test(){ echo "World"; } } ?> Fatal error: Call to undefined function test() Quote Link to comment https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/#findComment-940849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.