Passing database info into URL

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?


Re: Passing database info into URL

This is basic php stuff:

   case 1:
   /* ... others here .. */


Re: Passing database info into URL


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" ">
        <?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');?>

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?

Re: Passing database info into URL

On Thu, 2006-04-13 at 06:24 -0700, wrote:
Quoted text here. Click to load it

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:

$result = mysql_query("SELECT linkID, linkName from linktable");

navigation.php would then do something like this:

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.


Re: Passing database info into URL

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.,, etc)?

Re: Passing database info into URL

On Thu, 2006-04-13 at 12:32 -0700, wrote:
Quoted text here. Click to load it

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

Re: Passing database info into URL

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.

Scott wrote:
Quoted text here. Click to load it

Re: Passing database info into URL wrote:
Quoted text here. Click to load it

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.

Site Timeline