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!
help returning a value from a function
Moderator: General Moderators
Re: help returning a value from a function
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();