SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: url tutorials

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    url tutorials

    Is it just me or is the area of url's/uri's lacking for tutorials ? Perhaps staring at my screen for hours on end, means I have the intelligence of a rock. I get it. What I don't get, is the code that makes the following possible.

    You have some text located in a database with a field named 'entry'.
    Then you want to populate a page using that text anytime some clicks on a particular link.

    Somehow the url gets made, so you can bookmark it, for that query and one or more entries are displayed.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe what you're referring to is that a parameter is passed to server side script like so

    Code:
    page.php?id=1
    The parameter of id=1 is passed.

    The script can then go the pages table and pull out the record for page 1 and return the content (html) for that page.

    URLs can also be formatted prettily so that it appears as http://domain.com/pages/page1 - This is done using mod-rewrite. That's where you should start your search if that's what you're looking for.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This sort of what I'm looking at.
    http://www.keithjbrown.co.uk/vworks/php/php_p2.php

    However, it still really doesn't address the sql retrieval and it's display.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trevor2 View Post
    However, it still really doesn't address the sql retrieval and it's display.
    URI have nothing to do with that...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Tell us how your database table is structured and someone will explain how to make links and use those url links to retrieve the text for you.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think my mental block is getting lost between the anchor tags on state_listing.php and having a non-existing Alaska.php page made, if that makes sense.

    I plan on using CodeIgniter later, but for now I want to understand how this is normally accomplished. They have their own uri segment structure based around this scheme: mysite.com/class/method.

    My fictional table:
    Code SQL:
    CREATE TABLE `my_db`.`my _table` ( 
    `res_id` BIGINT( 60 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `user_id` BIGINT( 60 ) NOT NULL ,
    `cat_id` BIGINT( 60 ) NOT NULL ,
    `state_id` TINYINT( 2 ) NOT NULL ,
    `url` VARCHAR( 60 ) NOT NULL ,
    `title` VARCHAR( 100 ) NOT NULL ,
    `description` TINYTEXT NOT NULL ,
    `rating` TINYINT( 10 ) NOT NULL ,
    `approved` BOOL NOT NULL ,
    `time` DATETIME NOT NULL ,
    `remote_ip` VARCHAR( 60 ) NOT NULL 
    ) ENGINE = MYISAM

    Sample query to return all state listings for Alaska with a state_id = 3
    Code PHP:
    $sql = "SELECT `url`, `title`, `description`, `rating` 
    FROM `my _table` WHERE `state_id` = 3 ";

    Create the anchor tags for state_listing.php
    Code HTML4Strict:
    <html>
    <body>
    <?php
    while ($row = mysql_fetch_array($rResult)) 
    {
          echo "<a href=http://localhost/" . $row['sName'] . ">" . $row['sName'] . "</a>" . "<br>";		
    }
    ?>
    </body>
    </html>

    Trouble spot.
    How to create all of the state pages like http://localhost/state_listing/Alaska.php automatically?

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If Alaska is the title in your database, then you can have a page/link called :

    /state_listing/Alaska

    OR

    /state_listing/Alaska.php

    Either of which becomes an alias of (ie gets its ACTUAL content from) this page;

    /state_listing/index.php?state=Alaska

    That is done using mod_rewrite.

    So your tasks would be to create :

    1) the links on a page somewhere
    2) the index.php page which handles $_GET['state'], extrudes all the data from wherever you keep it now and displays it nicely.

    You will need to overcome what to do about "North Dakota" because it has a space, just a matter of slugging and de-slugging the title, and maybe strtolowering it if you want.

    echo '<a href="' . str_replace( "-", " ", strtolower($state_name) )
    . '">' . $state_name . '</a>' ;

    link:
    <a href= "/state_listing/north-dakota" >North Dakota</a>

    Get all that working, handle failure and hack attempts and then:

    3) create the rewrite rule in a .htaccess file

    Is this what you are after?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •