SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamically display content without having to write a lot of mysql commands?

    Hi Everyone,

    I hope this is the correct forum for this question!

    I'm new to mysql/php but I've always wondered how certain websites display content on a per-location basis? I'm quite certain these websites don't write 100s or 1000s of mysql commands but I would like to know how their information is displayed.

    For example, SELECT * FROM car_info WHERE state='massachusetts' The reason why I don't believe they have unique mysql commands like this is because they would have to write 1000s of commands and it would be too time consuming as a web developer to do so. My question is, how are pages like this programmed without having to write 1000s of mysql commands? How do they "show/present" content that's unique to the page and visitor?

    http://cars.oodle.com/regions/massachusetts/
    http://cars.oodle.com/used-cars/barnstable-ma-area/
    http://cars.oodle.com/used-cars/boston-area/
    http://cars.oodle.com/used-cars/pittsfield-ma-area/

    Other examples,
    http://www.iseecars.com/used-cars/us...hode-island-ri
    http://www.iseecars.com/used-cars/us...ssachusetts-ma

    Thanks Everyone!

  2. #2
    SitePoint Mentor silver trophybronze trophy
    Mikl's Avatar
    Join Date
    Dec 2011
    Location
    Edinburgh, Scotland
    Posts
    1,541
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    You're right. They don't have a separate command for each possible location. Rather, they place the location name in a variable, and use that variable in the WHERE clause.

    So instead of this:

    SELECT * FROM car_info WHERE state='massachusetts'

    they would arrange for "massachusetts" to be stored in a variable, say $location, then execute this:

    SELECT * FROM car_info WHERE state= $location

    Does that make sense?

    Mike


  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    Thanks for the response. Yes, that makes sense but now I'm a little confused on something else. If each state/city is a $location variable then how does that specific page know to get data that relates specifically to that state/city?

    For example, in the "boston" section of cars.oodle.com/used-cars/boston-area/ hows that that specific page know how to "pull/get" data that relates to boston and not NYC, Las Vegas or even Miami? If all your using is a $location variable wouldn't your php page get confused as to what information to "show/present" to the visitor?

    This is what I think what is happening with the above pages. Please tell me if I'm correct or totally wrong.

    The page(s) inherent in a another page. Let's called this page init.php
    include once "init.php";

    In this init.php page a mysql commands takes place.
    SELECT * FROM car_info WHERE state='massachusetts'
    Then all of the mysql data is converted into $variables and is used on each indivual state/city page?

    So in other words, for every state you would need to inherent a different init.php page with a different mysql command. Am I correct or totally wrong?

    Thanks!

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You are wrong.

    If the url is cars.oodle.com/used-cars/boston-area/ then probably we're talking "pretty urls", and the 'used-cars' and 'boston-area' part will be available to the php script as variables (through url rewriting and/or the use of a router class in the code). And those variables will be used to dynamically create a query with the correct values in the WHERE conditions.

    So they only need 1 query, and put the right values in the WHERE conditions.

  5. #5
    SitePoint Mentor silver trophybronze trophy
    Mikl's Avatar
    Join Date
    Dec 2011
    Location
    Edinburgh, Scotland
    Posts
    1,541
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I completely understand the follow-up question. But, essentially, there is only one page. It receives the location as a parameter. The PHP logic then creates, on the fly, the content for the specific location. It either displays that content immediately, or (less likely) it creates a new page and redirects to it. This is basically how all data-based sites work. There is no question of the content being pre-written for each location, nor do the programmers need to even know which locations exist.

    Mike


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
  •