function not functioning

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

function not functioning

Post by .Stealth »

hi, i have a problem with a function not working, ive stared at this function for hours, but nothing says im the problem.

this function generate the page requested by getting the data from the database and putting it into variables which are the echoed out in the page where neccassary.

but for some reason it wont work, if i copy the code in the function directly into the place where i call the function it works, but if i put the function name there to call it i just get the following error:

notice: undefined variable in page.php on line 64 (line 64 being where i am outputting the variable)
i get a big list of these notices everywhere i have tried to output the variables of the function.


here is the bit that calls the function providing the page number is set:

Code: Select all

if(isset($_GET['page'])){
											$page_id =(int)$_GET['page'];
											generate_page();
											}
											else{
												$content = "<p><strong>Error:</strong> the page you requested is unavliable,
												it is possible it has been delete or you have followed an incorect link.</p>\n";
												}

here is the function:

Code: Select all

function generate_page(){


					$sql = @mysql_query("SELECT title, description, 
					keywords, content FROM pages 
					WHERE id='$page_id';");
					
					
					if(!$sql){
					echo "<p>Error: Unable to retrieve page info from database.<br /><br /> Details:" .
			 		mysql_error() . "</p>\n";
					}

					$data = mysql_fetch_array($sql);
					if(!$data){
					echo "<p>Error: Unable to retrieve page info from database.<br /><br /> Details:" .
					 mysql_error() . "</p>\n";
					}

					$title 			= $data['title']; //page title
					$description 	= $data['description']; //page description for meta tags
					$keywords 		= $data['keywords']; //keywords for meta tags
					$content		= $data['content']; //page content

					}


thanks for any help.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

http://us.php.net/manual/en/language.va ... .scope.php

The variable $page_id inside the function is considered a local variable, and thus has no value. You need to pass the ID to the function as a parameter for it to have a value, or declare the variable $page_id as a global inside your function (the former being the better idea).

Code: Select all

$page_id =(int)$_GET['page'];
generate_page($page_id);
// snip
function generate_page($page_id){ 
// snip
}
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

thanks for that, i have no made the function as you said but these variables still have no value in page.php

Code: Select all

function generate_page($page_id){

// snip


                                        $title    = $data['title']; //page title
                                        $description    = $data['description']; //page description for meta tags
                                        $keywords             = $data['keywords']; //keywords for meta tags
                                        $content                = $data['content']; //page content


// snip
what method could i use to make the above variables have the value of $data['field_name'] in the page that the function is called?.

i have tried setting the variables at the top of page.php like this:

Code: Select all

<?php
/**
 * @author Jack H
 * @copyright 2007
 * @file page.php
 */
 
$title 			= '';
$description 	= '';
$keywords 		= '';
$content 		= '';
?>
then using this iside the function:

Code: Select all

GLOBALS['title']		= $data['title']; //page title
					GLOBALS['description'] 	= $data['description']; //page description for meta tags
					GLOBALS['keywords'] 	= $data['keywords']; //keywords for meta tags
					GLOBALS['content']		= $data['content']; //page content

i used the GLOBALS['global_var'] based on the suggestion in the php manual but only got this result:

Parse error: syntax error, unexpected '[' include/functions.inc.php on line 111


line 111 being:

Code: Select all

GLOBALS['title']		= $data['title']; //page title
made a right mess havnt i lol

thanks for your help so far
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

GLOBALS without a $ in front is treated as if it is a custom definition. Put the $ in front ($GLOBALS), and it should work properly.
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

yeah it worked, cant believe it, i have been trying to fix this without needing to ask anybody for a good few hours but it defeated me lol

thank you for all your help TheMoose.

at least i know now for next time :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You may want to stay away from the $GLOBALS array. It will help you maintain focus on your scope, which is what is causing your problems in this case.
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

ohh dont say that lol

im quite new at php and from looking at the manual linked for me that is the only option i can think of.

is there an easier way?

thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The way that moose told, along with using a return value.

Code: Select all

<?php
function generate_page($page_id = 0) { 
  if ($page_id == 0) {
    // This should be your own error handler
    die('You must enter a value.');
  }
  
  $sql = "SELECT title, description, keywords, content FROM pages WHERE id=$page_id"; 
  
  if (!$result = mysql_query($sql)) {
    // Again your own error handler goes here
    die('Could not execute the query.');
  }

  $data = array();
  while ($row = mysql_fetch_array($result)) {
    $data[] = $row;
  }

  return $data; // which is an array of the row data from the table
}

$data = generate_page($page_id);
if (empty($data)) {
  die('There was an error fetching the page data.');
}

$title        = $data['title']; //page title 
$description  = $data['description']; //page description for meta tags 
$keywords     = $data['keywords']; //keywords for meta tags 
$content      = $data['content']; //page content 
?>
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

You can just return the array of the data, and use that array instead of setting them inside the GLOBALS array.

IE:

Code: Select all

function generate_page($page_id){
	$sql = @mysql_query("SELECT title, description, keywords, content FROM pages WHERE id='$page_id';");
	if(!$sql) {
		echo "<p>Error: Unable to retrieve page info from database.<br /><br /> Details:" . mysql_error() . "</p>\n";
	}
	$data = mysql_fetch_array($sql);
	if(!$data){
		echo "<p>Error: Unable to retrieve page info from database.<br /><br /> Details:" . mysql_error() . "</p>\n";
	} else
		return $data;
}
This way you can do something like $page_data = generate_page($page_id), and use the $page_data array whenever you need that data. ($page_data['title'], for instance).

EDIT: Everah beat me to basically the exact same concept! :)
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

ohh right i see.

so the key to this bit is in the "return" bit.

ive never used return before so didnt know i could do that.

thanks alot for your input Everah and TheMoose, you've helped alot :D
Post Reply