So I have my site templates complete and I know that I have the right database connection because I can register, but I am having problems with dynamic pages.

I originally started the site with all the pages in their own unique separate PHP file, but I ran into problems with updating content. My great idea I got from a friend was to do dynamic pages. Trust me, I was working on this for 2 days with still no result. I have researched at least 50 sites on dynamic pages, and my code seems sound, but I don't know wtf the problem is.

I am using functions, or trying to, to help with simplifying the task.

Basically I only need 3 files to do this: my db connection file, my index template, and my functions.inc.php file.

I know this may seem like a stupid topic, but I am pulling my hair out with this since I am kinda a n00b at this.

Here is my index.php file, header and footer in separate files.

<?php include ("header.php"); ?>

	<?php page_content(); ?>

<?php include ("footer.php"); ?>

Here is my header.php file, don't mind the description part.

<?php
	$id=(int)$_GET['id'];
	if (empty($id))	$id = "1";
	include "functions.inc.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title><?php page_title ?></title>

  <meta name="description" content="Tech Club is your source to learn about modern day and future technologies. We do projects relating to website design and development, programming, games, and of course computers.">
  <meta name="keywords" content="<?php page_keywords(); ?>">

  <meta name="copyright" content="Copyright Seth Moon - 2010">
  <meta name="author" content="Seth Moon">
  <meta name="email" content="webmaster@ohstech.info">
  <meta name="Charset" content="UTF-8">
  <meta name="Distribution" content="Global">
  <meta name="Rating" content="General">
  <meta name="Robots" content="INDEX,FOLLOW">
  <meta name="Revisit-after" content="7 Days">
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

  <link rel="stylesheet" type="text/css" href="styles/style.css" />

  <script type="text/javascript" src="js/niftycube.js"></script>
  <script type="text/javascript">
  window.onload=function(){
	Nifty("div#header","big-top");
	Nifty("div#content","big-bottom");
	}
  </script>
</head>
<body>
  <div class="container">
  <div id="header">
	<a href="index.php"><img src="images/logo.png"></a>
  </div><!-- End Header -->
  <div id="menu">
	<ul>
	  <li><a href="index.php"<?php if ($id=="1") {
		echo " class=\"active\""; }?>>Home</a></li>
	  <li><a href="index.php?id=2"<?php if ($id=="2") {
		echo " class=\"active\""; }?>>Web Building</a></li>
	  <li><a href="index.php?id=3"<?php if ($id=="3") {
		echo " class=\"active\""; }?>>Computers</a></li>
	  <li><a href="index.php?id=4"<?php if ($id=="4") {
		echo " class=\"active\""; }?>>Modern Tech</a></li>
	  <li><a href="forum/index.php">Forum</a></li>
	</ul>
  </div><!-- End Menu -->


  <div id="content">

and lastly, here is my functions.inc.php file

<?php
	include_once "scripts/dbconnect.php";
	// Collects data from pages table
	$data = mysql_query("SELECT * FROM pages WHERE id='$id'");
	//put all page data into an array
	$info = mysql_fetch_array($data);
	
	//make variables for each part
	$title = $info['page_title'];
	$keywords = $info['keywords'];
	$content = $info['page_content'];

function page_title() {
	echo $title;
    }

function page_keywords() {
	echo $keywords;
    }

function page_content() {
	echo $content;
    }
?>

Recommended Answers

All 4 Replies

Hi,

Your script will not produce any output because the echo statement refers to a global variable inside the function page_content, which $content is a local variable to the function.

function page_content() {
	global $content;
	echo $content;
  }

This may produce the result as you expected. Find this Variable Scope in php to know more.

commented: Saved me!! +0

Thank you so much!!!! +1 rep for that buddy.
Just want some clarification...
Alright, so if my understanding is correct, the variables were restricted to what was in between the "<?php" and "?>" on the functions.inc.php and could not be processed outside of it?

Hi,

Here the process is the difference between the local (which the variable declared inside the function scope) and global variable (accessing outside variable to the function).
When you're trying to call a global variable to your function you must specify the global keyword else it does not show the output and result will be empty.

Alrighty then, thanks for the reply. I;m going to mark this as answered thanks to your expert help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.