help returning a value from a function

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
jbraxx
Forum Newbie
Posts: 4
Joined: Fri Mar 19, 2010 1:23 am

help returning a value from a function

Post by jbraxx »

Can I get a little help with two functions? I would like my index page to get the 'page_id' from the url and use it to query a mysql database. The query would return the filepath for an include. Seems pretty straight forward, and it works when don't declare the functions and just write the code on the index page. But when I declare the functions in a separate include and then call them, I keep getting this error:

"Notice: Undefined variable: page_id in /Applications/MAMP/htdocs/rip/index.php on line 7"

Here's the index page:

<?php include("includes/functions.php"); ?>
<?php include("includes/connection.php"); ?>
<?php include("includes/header.php"); ?>
<?php
find_selected_page();

get_page_by_id($page_id);

include($page['filepath']);
?>
<?php include("includes/footer.php"); ?>

And here are the functions being called:

function find_selected_page() {
if(isset($_GET['page_id'])) {
$page_id = $_GET['page_id'];
} else {
$page_id = 1;
}
return($page_id);
}

function get_page_by_id($page_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE id=" . $page_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query ($query, $connection);
confirm_query($result_set);
if ($page = mysql_fetch_array($result_set)) {
return $page;
} else {
return NULL;
}
}

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help returning a value from a function

Post by requinix »

find_selected_page returns a value. It does not magically create variables that you can use - you have to do that part yourself.

Code: Select all

$page_id = find_selected_page();
Post Reply