Jump to content


Photo

Vars within function made global but not displaying..?


  • Please log in to reply
4 replies to this topic

#1 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 21 September 2006 - 04:10 PM

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...
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;
	};
};

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
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 21 September 2006 - 04:40 PM

Declare the variables global before assigning them values.
global $pagetitle, $pagecontent;
http://www.php.net/m...ables.scope.php

I'd recommend you simply return an array with the relevant information rather than setting them global.

function pageNav()
{
   $pageData['title'] = $row[..]
   $pageData['content'] = $row[...];
   
   return $pageData;
}

$pageData = pageNav();
echo $pageData['title']."<br />\n";
echo $pageData['content']."<br />\n";


#3 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 21 September 2006 - 04:45 PM

ehh, im not that good and dont really understand that :P
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 21 September 2006 - 05:05 PM

ehh, im not that good and dont really understand that :P

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"
http://www.php.net/m...ning-values.php

#5 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 21 September 2006 - 05:14 PM

Ah ok yeah i get it now! Thanks its all working now!
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users