Jump to content

Vars within function made global but not displaying..?


AbydosGater

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
Link to comment
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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.