Creating a templated website, need help...

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
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

Creating a templated website, need help...

Post by WithHisStripes »

Heya,
I'm a junior PHP programmer and I'm trying to build my first templated website using custom written PHP.

I want data to be retrieved from either a MySQL db or a file to be included into a targeted div but only when the user clicks the link tied to that data.

For example, I click on 'home' and the data in the 'content' div is replaced with the 'home' page data. Same thing happens when the user clicks on 'about us' or whatever. Does that make sense? If so can anyone help? Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are talking about dynamic content. This is, for the most part, why developers get into server side coding.

There are a number of ways to do what you want to do. Without getting overly complicated, one way is to store your page content in a database, then, based upon the page you are on, grab that content and serve it to the template. It is not that hard to do. I would say try some local tests first, then ask questions as you go along.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

Post by WithHisStripes »

Great, thanks for your reply. Okay, I know how to retrieve data from my db and all that stuff, what I don't know is how to retrieve that on click and display it into a targeted div. Can you help me there? Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here's an idea...

Say the page you are on is 'aboutus.php'. You could have a row in your table that looks like this:

page_id - 27
page_name - aboutus
page_title - About our company
page_content - <p>A little about out company.</p>

In your code you could do something like this:

Code: Select all

<?php
$page = basename(__FILE__); // will yield aboutus.php
$page = str_replace('.php', '', $page); // will yield aboutus

$sql = "SELECT * FROM `my_pages` WHERE `page_name` = '$page' LIMIT 0, 1";
// ... snip ...
?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo $page_content; ?>
</body>
</html>
This will rely on a system of files for each page in your web site. This is not a preferred method, as maintainability becomes very hard after a few new pages are added. Of course, you could always go with a page controller of sorts, so that everything is fed through one page and a query string tells the app what page to load:

Code: Select all

<?php
// Be sure the validate this before throwing at the database
// That is up to you to learn how to do
$page = !empty($_GET['page']) ? $_GET['page'] : 'index';

$sql = "SELECT * FROM `my_pages` WHERE `page_name` = '$page' LIMIT 0, 1";
// ... snip ...

// Add is some contingency if the page is not in the database, so you can roll to index or a 404 page
?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo $page_content; ?>
</body>
</html>
Of course, there are several sophisticated ways of doing this, but this is the general aspects of it.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

Post by WithHisStripes »

Cool. Thanks a ton for that. As I read through it though I don't see where you tie it into occur when the user clicks on the navigation element 'aboutus' ... did I miss something? Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I am trying to leave some of that for you ;)

Using the first example I showed you, navigation could be something like:

Code: Select all

<a href="aboutus.php">About us</a> | <a href="contactus.php">Contact us</a>
Where each link calls an actual page on the server. Using the second example you would do something like:

Code: Select all

<a href="index.php?page=aboutus">About us</a> | <a href="index.php?page=contactus">Contact us</a>
Where each link calls index.php (or whatever page you use to control the page forwarding) passing it a querystring var of page.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

Post by WithHisStripes »

Haha, cool. Thanks again. Just FYI that was the only thing I didn't know how to do but I appreciate your encouraging my learning.

But I suppose you're just suggesting I write the link with the html to target it within the link itself? <a href="example.html" target="example_id">Example</a> ?

Cool, I guess that is what I'm looking for. Thanks my friend!



While we've been talking I just used some simply javascript to call a file into a targeted div. In case anyone else wants to know here's what I did.

Code: Select all

function ahah(url, target) {
  document.getElementById(target).innerHTML = 'Loading the data, one moment...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function load(name, div) {
	ahah(name,div);
	return false;
}

And then I used a simple link:

Code: Select all

<a href="example.html" onclick="load('example.html','col_2');return false;">Example</a>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you trying to avoid posting to the server?
Post Reply