AbydosGater Posted September 21, 2006 Share Posted September 21, 2006 Hi ok well first the background, the function is within my config file which is included within the file i am having problems with, and the page i have another function already running to connect to the database,Now here is the code to my function...[code]function pagenav(){ if (isset($_GET['id'])) { $result = mysql_query("SELECT * FROM ben_pages WHERE id='$_GET[id]'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $pagetitle = $row['title']; $pagecontent = $row['page']; //echo $pagetitle ; //echo "<br />"; //echo $pagecontent ; } else { $result = mysql_query("SELECT * FROM ben_pages WHERE id='index'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $pagetitle = $row['title']; $pagecontent = $row['page']; global $pagetitle, $pagecontent; //echo $pagetitle; //echo "<br />"; //echo $pagecontent; };};[/code]Now if i uncomment the echos there, I do get the variables to display!, But i have that function running on another page, but i have the page displaying $pagetitle , $pagecontent at another point outside the function, but it isnt displaying, how come? i have them set to global? anyone any ideas?, any help atall would be great?Thanks Quote Link to comment Share on other sites More sharing options...
shoz Posted September 21, 2006 Share Posted September 21, 2006 Declare the variables global before assigning them values.[code]global $pagetitle, $pagecontent;[/code]http://www.php.net/manual/en/language.variables.scope.phpI'd recommend you simply return an array with the relevant information rather than setting them global.[code]function pageNav(){ $pageData['title'] = $row[..] $pageData['content'] = $row[...]; return $pageData;}$pageData = pageNav();echo $pageData['title']."<br />\n";echo $pageData['content']."<br />\n";[/code] Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted September 21, 2006 Author Share Posted September 21, 2006 ehh, im not that good and dont really understand that :P Quote Link to comment Share on other sites More sharing options...
shoz Posted September 21, 2006 Share Posted September 21, 2006 [quote author=AbydosGater link=topic=108934.msg438807#msg438807 date=1158857110]ehh, im not that good and dont really understand that :P[/quote][code]function add($a, $b){ $total = $a +$b; return $total; //"return $a + $b" would also achieve the same result.}$aVarl = add(5, 1);print $aVar //Prints "6"[/code]http://www.php.net/manual/en/functions.returning-values.php Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted September 21, 2006 Author Share Posted September 21, 2006 Ah ok yeah i get it now! Thanks its all working now! Quote Link to comment 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.