Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing database info into URL

0 views
Skip to first unread message

maxv...@gmail.com

unread,
Apr 13, 2006, 3:05:56 AM4/13/06
to
Hi,

I have been scouring the Internet for a good read on creating webpages
whose address depends on data retrieved from a from via GET method OR a
MySQL database ... but counldn't find anything decent.

For example, want to create ten pages about 10 different items.
Because I want the header, the footer, the navigation to remain the
same from page to page, I'd like each page to NOT be a separate html
file, but be referenced by something like
http://<mysitename>/items/index.php?item_ID=1. Kind of like separate
WordPress posts.

How would I do something like that and where would I read about it?

Thanks.

fletch

unread,
Apr 13, 2006, 4:09:32 AM4/13/06
to
This is basic php stuff:

header()
switch($_POST['item_ID'])
{
case 1:
includefile('somecontent.php');
break
/* ... others here .. */
default:
includefile('indexcontent.php');

}
footer()

Jerry Stuckle

unread,
Apr 13, 2006, 8:52:56 AM4/13/06
to

Not too hard - basically, you have a structure similar to:

Code to fetch data from database based on the id
Page header information
Generated data from the database
Page footer information

The reason the code is at the start is often times you might want to change the
<title> contents to reflect the item being retrieved.

As for examples - they're all over the net. Basically anything which uses a
database for the back end does it.

Of course, if the list of items is fixed, you can create 10 pages for them, but
use one header and one footer file, including them in each of the other 10
files. That way you have common headers and footers.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

maxv...@gmail.com

unread,
Apr 13, 2006, 9:24:28 AM4/13/06
to
Thanks.

I'll get real specific now. Suppose I have this layout in index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
<?php require('includes/header.php');?>
<?php require('includes/navigation.php');?>
<?php require('includes/left.php');?>

.. some database retrieval code goes here, e.g. mysql_query ("SELECT
...");

<?php require('includes/centerright.php');?>
<?php require('includes/far_right.php');?>
<?php require('includes/footer.php');?>
</body>
</html>

How would I write the <a href> tags in the 'navigation include file' to
point to individual pages by using the "?id=1" , "?id=2" ... "?id=n"
method in the URL?

Scott

unread,
Apr 13, 2006, 3:02:23 PM4/13/06
to

If navigation.php is going to create links based off of info that you
get from the database, you need to query the database before including
the file.

For example:

<?php
$result = mysql_query("SELECT linkID, linkName from linktable");
require('includes/navigation.php');
?>

navigation.php would then do something like this:

<?php
while($row = mysql_fetch_assoc($result)) {
echo '<a href="yourPage.php?id='.$row['linkID'].'">';
echo $row['linkName'].'</a><br />'."\n";
}
?>

Of course, if navigation.php is the only file using the results from the
database query, you might as well put the query in that file.

Scott

maxv...@gmail.com

unread,
Apr 13, 2006, 3:32:10 PM4/13/06
to
Awesome! Thanks!

What makes the browser display different pages when a new 'linkID' is
called in the address line?

WordPress is a good example ... Specifically what makes WordPress show
different posts when the 'id' is changed in the address line (e.g.
wordpressblog.com/?p=1, wordpressblog.com/?p=2, etc)?

Scott

unread,
Apr 15, 2006, 11:44:27 AM4/15/06
to

Nothing, unless you do something with it through the use of $_GET.

maxv...@gmail.com

unread,
Apr 17, 2006, 3:05:00 PM4/17/06
to
Thanks, guys.

I finally figured it out. If I wrap make my navigation include file
into FORM tags, use METHOD="GET", set up links to point to "?id=n", and
pull the content part out based on query SELECT content WHERE id =
$_GET, then I will get exactly what I was looking for.

0 new messages