Jump to content

Archived

This topic is now archived and is closed to further replies.

AbydosGater

Vars within function made global but not displaying..?

Recommended Posts

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

Share this post


Link to post
Share on other sites
Declare the variables global before assigning them values.
[code]
global $pagetitle, $pagecontent;
[/code]
http://www.php.net/manual/en/language.variables.scope.php

I'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]

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.