Passing database info into URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maxvalery@gmail.com

    Passing database info into URL

    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

    #2
    Re: Passing database info into URL

    This is basic php stuff:

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

    }
    footer()

    Comment

    • Jerry Stuckle

      #3
      Re: Passing database info into URL

      maxvalery@gmail .com wrote:[color=blue]
      > 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.
      >[/color]

      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.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • maxvalery@gmail.com

        #4
        Re: Passing database info into URL

        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.dt d">
        <html>
        <head></head>
        <body>
        <?php require('includ es/header.php');?>
        <?php require('includ es/navigation.php' );?>
        <?php require('includ es/left.php');?>

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

        <?php require('includ es/centerright.php ');?>
        <?php require('includ es/far_right.php') ;?>
        <?php require('includ es/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?

        Comment

        • Scott

          #5
          Re: Passing database info into URL

          On Thu, 2006-04-13 at 06:24 -0700, maxvalery@gmail .com wrote:[color=blue]
          > 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.dt d">
          > <html>
          > <head></head>
          > <body>
          > <?php require('includ es/header.php');?>
          > <?php require('includ es/navigation.php' );?>
          > <?php require('includ es/left.php');?>
          >
          > .. some database retrieval code goes here, e.g. mysql_query ("SELECT
          > ...");
          >
          > <?php require('includ es/centerright.php ');?>
          > <?php require('includ es/far_right.php') ;?>
          > <?php require('includ es/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?
          >[/color]

          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("SE LECT linkID, linkName from linktable");
          require('includ es/navigation.php' );
          ?>

          navigation.php would then do something like this:

          <?php
          while($row = mysql_fetch_ass oc($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

          Comment

          • maxvalery@gmail.com

            #6
            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.
            wordpressblog.c om/?p=1, wordpressblog.c om/?p=2, etc)?

            Comment

            • Scott

              #7
              Re: Passing database info into URL

              On Thu, 2006-04-13 at 12:32 -0700, maxvalery@gmail .com wrote:[color=blue]
              > 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.c om/?p=1, wordpressblog.c om/?p=2, etc)?
              >[/color]

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

              Comment

              • maxvalery@gmail.com

                #8
                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:[color=blue]
                > On Thu, 2006-04-13 at 12:32 -0700, maxvalery@gmail .com wrote:[color=green]
                > > 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.c om/?p=1, wordpressblog.c om/?p=2, etc)?
                > >[/color]
                >
                > Nothing, unless you do something with it through the use of $_GET.[/color]

                Comment

                Working...