Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > FRIENDLY URL'S


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Friendly URL's
By Jaisen Mathai - 2003-10-01 Page:  1 2 3 4

A pinch of PHP

Ok. We saw what we need to do in order to have Apache behave properly. Now we can unleash the PHP beast in each of us to generate useful content. We will go ahead and stick to our articles example. Let's say you have an site database with an articles table. Here's the table structure:


CREATE TABLE `articles` (
  `
id` tinyint(3) unsigned NOT NULL auto_increment,
  `
title` varchar(75) NOT NULL default '',
  `
article` text NOT NULL,
 
PRIMARY KEY  (`id`)
);

?>

Ok. Just make sure you don't have more than 256 articles.

Now, let's decide on how we want the urls to look. Actually, let me decide. http://yoursite.com/article/xii looks pretty damn cool. Now we just need to write the parse the REQUEST_URI, query our database, and then display the article. Sounds easy enough, right? Let's get to work.


<?php
  $article_roman
= substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1, strlen($_SERVER['REQUEST_URI'));
 
$article_id    = Numbers_Roman::toNumber($article_roman);

 
$result        = mysql_query("SELECT * FROM articles WHERE id = {$article_id}") or die(mysql_error());
 
$article_data  = mysql_fetch_assoc($result);

  echo
"Title: {$article_data['title']}<br /><br />";
  echo
$article_data['article'];
?>

?>

Let's go over this code line by line. The first two lines just parse out the REQUEST_URI and get the last part of it (the roman numeral). Then it converts that to a numeric value using Pear's Numbers_Roman class.

The next two lines query the database for the article with the appropriate id.

The last two lines echo the title and content of the article.

Pretty easy, no?



View Friendly URL's Discussion

Page:  1 2 3 4 Next Page: Making friends is fun

Copyright 2003 Jaisen Mathai. All rights reserved.


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.