SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 41
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating dynamic web pages...?

    Hi,

    I have the following record in a table in my database...

    sql table structure -- id | file_url | title | page_content

    72 | dining/menus.php | Menus - Dining | <h1>Welcome...</h1>


    Now is there a way(using .htaccess and some PHP trickery) that when the user visits www.test.com/dining/menus.php they retrieve this data, without having a physical folder(dining) and file on the server for 'dining/menus.php' etc.


    Many thanks for any help.

  2. #2
    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)
    Using a .htaccess file containing this:
    Code:
    RewriteEngine on
    
    RewriteRule ^dining/?$ index.php [QSA]
    RewriteRule ^dining/([a-zA-Z]+)$ index.php?type=$1&cat=dining [QSA]
    A rough example which shows:

    ALL traffic to /dining will go to index.php
    Then, traffic to /dining/menus will be sent to index.php?type=menus&cat=dining

    So that you can build all your logic into index.php, and your dining menu can have the even smarter, seo friendlier url /dining/menus

    Read more here.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the speedy reply Paul.

    I have a number of other categories including 'dining', is there an easy way to do this?

    So I may have 'activities/index.php', 'activities/play.php', 'accommodation/beds.php' etc.

  4. #4
    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)
    Yes, read that link I gave you, I am pretty confident it explains everything you need to know.

    BTW dont you want clean urls like:

    'activities/index', 'activities/play', 'accommodation/beds' etc.

    Work out the $_GET calls for your smart index.php file, then any problems you have with mapping one thing to another ....

    Be aware that index.php should be doing a lot of checking about what is allowed:

    eg
    PHP Code:
    $categories = array( "accommodation","activities" );

    if (! 
    in_array$_GET['cat'], $categories ) ) {// send away} 

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaaah yes, clean URL's sounds a good idea.

    So I should verify my categories and pages, compare them to the values in the SQL table?

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess if it were 'activities/index', I would prefer just 'activities/' on it's own.

    Thanks for your help so far, I'm off to test it

  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)
    Quote Originally Posted by invision2 View Post
    Aaaah yes, clean URL's sounds a good idea.

    So I should verify my categories and pages, compare them to the values in the SQL table?
    Exactly right.

    index.php should get the allowed values, either hand written, from an ini file or from your database, as you say.

    re index, well if that is synonymous with index.php then, yes you'd generally leave it off. Logically your sense the absence of a particular variable meaning "show the menu" or "show all options" etc.

    I would prefer just 'activities/' on it's own.
    Watch out for that too, trailing slash:

    in your .htaccess file:
    /([a-zA-Z]+)/?

    That would optionally permit activities/

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for your patience with this.

    OK, for a test I did the following:

    index.php?cat=accommodation

    PHP Code:
    $categories = array( "accommodation","activities" );

    if (! 
    in_array$_GET['cat'], $categories ) ) {// send away} 

        
    echo 'not ok!';
        

    } else {

        echo 
    'ok!';


    which when visited said 'ok!' which is a good start.

    However, how do I then get it to have 'www.test.com/accommodation' in my address bar?

    How do I get my PHP page and MySQL to do this?

    I'm currently using the htaccess file:

    PHP Code:
    RewriteEngine on

    RewriteRule 
    ^accommodation/?$ index.php [QSA]
    RewriteRule ^accommodation/([a-zA-Z]+)$ index.php?type=$1&cat=accommodation [QSA

    Many thanks for your help with this.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups, can you help with the above? I'd love to get a basic bit working just now, then try and expand upon it.

  10. #10
    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)
    This should work ...

    However, how do I then get it to have 'www.test.com/accommodation' in my address bar?
    You links say: test. com/accommodation

    The browser address is test. com/accommodation

    But behind the scenes, apache tells PHP ...

    .htaccess
    Code:
    RewriteEngine on
    
    RewriteRule ^([a-zA-Z]+)/?$ index.php?cat=$1 [QSA]
    index.php
    PHP Code:
    $categories = array( "accommodation","activities" );

    if (! 
    in_array$_GET['cat'], $categories ) ) {// send away} 

        
    echo 'not ok!';
        

    } else {

        echo 
    'ok!, now load ' $_GET['cat'] . ' details from your database';
    //eg
    $sql 'select things from theTable where cat = "' GET['cat'] ."';

    echo 
    $sql ;



  11. #11
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for helping out. I think it's almost ready.

    I have several pages in 'accommodation'.
    What is the best way to have say 'accommodation/bedrooms' as a working URL that retrieves data from the correct record in my table.
    Should I use a unique identifier or the page title?

    What would my htaccess and SQL query look like?

    I'm sorry I'm just a little confused by it, you've been very helpful so far.

  12. #12
    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)
    Yes.

    Anyhow, the principle is get your single index.php file working as it should, fiddle around with the unfriendly links first, because don't forget you are trying to also make it turn urls like

    /far_hills_lodge/accommodation

    into

    ?id=far_hills_lodge&cat=accommodation

    And index.php may then need to discombobulate far_hills_lodge back to id = 23 in order to join up all your other tables ...

    Off Topic:

    Ah, now theres a thorny issue, why does it have to do that?

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I'm a bit confused now.

    I think I'm making it more confusing than it should be, sorry.

    I have a record in the sql table with the title 'Bedrooms'. It is in the category 'Accommodation'.
    How do I then display the url as accommodation/bedrooms?

  14. #14
    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)
    Well I saw you wrote id therefore I guessed that was the id of a place.

    Anyhow, to get this information out traditionally what would you do?

    index.php?type=bedrooms&cat=accommodation

    I mean, I cant work out when bedrooms is not a type of accommodation ... but there we are.

    Get index.php working and tell us the _GET parameters and someone will help you how to rewrite the url, at least you must understand roughly how its done now.

    Its tricky to get your head round at first, but if each element is correctly designed you should have :

    mod_rewrite
    PHP
    your database schema

    each taking the correct amount of responsibility.

    Your question started off about mod_rewrite, but it could end up delving more into your database schema, and that is natural (IMO).

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups,

    Many thanks for your persistence.

    Here's an idea of my database schema:

    ‘page’ table structure:
    id | filename | title | html | category
    4 | index | Accommodation – Home | <p>Test</p> | 2
    72 | bedrooms | Bedrooms | <p>Test 2</p> | 2

    ‘category’ table structure:
    id | name
    2 | accommodation

    So when the user visits www.test.com/accommodation/bedrooms I would like to retrieve the data in the second row above(id = 72).

    Can you please suggest how I do this with index.php and Mod_ReWrite ?

    I will also have a www.test.com/news which will display a list of news items, taken from the SQL table. How would you suggest I do this? Should I store the PHP (that queries the db) in the html column of the table?

    Thanks once more for your help and patience.

  16. #16
    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)
    your challenge is to turn a url like this:

    index.php?name=accommodation&filename=bedrooms

    into a select in index.php which does something like this:

    Code:
    $sql = 'SELECT html 
    FROM 
    page as p
    LEFT JOIN
    category as c 
    ON
    c.id = p.category
    WHERE 
    p.filename = "' . $_GET['filename'] .'"
    AND
    c.name = "' . $_GET['name'] ."' ;
    Where, clearly, you will have to frisk name and filename to make sure they only contain what you expect, a-z or A-Z etc.

    I am also supposing you want to display the 'html' field, and that 'filename' and 'category' combinations in the page table are unique. (inevitably the id field then becomes redundant )

    When that is working, then you can concentrate on the .htaccess file which should look something like this:
    Code:
    RewriteEngine on
    
    RewriteRule ^([a-zA-Z]+)/[(a-zA-Z]+)/?$ index.php?name=$1&filename=$2 [QSA]
    which turns this:
    /accommodation/bedrooms

    into this:
    index.php?name=accommodation&filename=bedrooms

    Then later you can deal with how to handle the nice plain

    /accommodation

    leading to:

    index.php?name=accommodation&filename=index
    or
    index.php?name=accommodation [i.e. absence of filename]

  17. #17
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply!!!

    OK, think I've taken a massive leap in the good direction.

    When I visit index.php?filename=bedrooms&category=accommodation and it displays the correct HTML for that page.

    PHP Code:
    $category $_GET['category'];
    $filename $_GET['filename'];

    $sql "SELECT html 
    FROM 
    page as p
    LEFT JOIN
    pagecat as c 
    ON
    c.id = p.category
    WHERE 
    p.filename = '
    $filename'
    AND
    c.name = '
    $category'"
    However, I'm having problems with the .htaccess.

    Whenever I use:
    PHP Code:
    RewriteEngine on

    RewriteRule 
    ^([a-zA-Z]+)/[(a-zA-Z]+)/?$ index.php?name=$1&filename=$[QSA
    I get a 'missing file error' page. Do you know why this would be?

  18. #18
    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)
    The .htaccess and index.php files should both be in the top public directory of test.com in order for what i posted to work.

  19. #19
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups, thanks again for the reply.

    I tried putting both the .htaccess and index.php in the root directory, but still get a 'missing file error' page as if htaccess is trying to look for a real directory 'accommodation'.

    Can you possibly test it at your end?

  20. #20
    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)
    Try this:

    RewriteEngine on

    RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?name=$1&filename=$2 [QSA]

    Duh, I mixed up the brackets, though this gave me a 500 error ...

  21. #21
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe it is now working I visit www.test.com/accommodation/bedrooms and it displays a page.

    You've been a massive help so far Cups.


    I'm using :

    PHP Code:
    RewriteEngine on

    RewriteRule 
    ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?category=$1&filename=$[QSA
    and:

    PHP Code:
    <?

    $category 
    $_GET['category'];
    $filename $_GET['filename'];

    $sql "SELECT html 
    FROM 
    pages as p
    LEFT JOIN
    pagecat as c 
    ON
    c.id = p.category
    WHERE 
    p.filename = '
    $filename'
    AND
    c.name = '
    $category'";

    echo 
    $sql;

    $res mysql_query($sql);

    $row mysql_fetch_array($res);

    echo 
    $row['0'];

    ?>
    Last edited by invision2; Jan 14, 2009 at 08:17.

  22. #22
    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)
    Well it works on my local machine. Can you turn on mod_rewrite logging and see if that gives you any more info, sorry, running out of ideas now ... mod_rewrite is pretty new to me too - I just know a couple of tricks, thats all.

    Try this first or else mod_rewrite logging

  23. #23
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works great now, apologies, I edited the post above ^

    There was just a couple of other queries I also had....



    1) When I visit www.test.com/accommodation I get an error - how do I fix this?

    2) How complex is it to allow hyphens so you could have 'things-to-do' as a category or filename?

    3) Is there a way to have PHP stored in a field in my mysql db and output it in one of the pages?

  24. #24
    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)
    1) create another rule before the previous one.
    2) add hyphens to the set [a-zAZ] ie [a-zA-Z-]
    3) that sounds like another posting or, a search of this forum

  25. #25
    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)
    Quote Originally Posted by invision2 View Post
    1) When I visit www.test.com/accommodation I get an error - how do I fix this?
    see post #10


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
  •