How to show different content on the page, depending on selected id of article?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilya Kraft
    New Member
    • Jan 2011
    • 134

    How to show different content on the page, depending on selected id of article?

    Hello,

    I have a database with table that contains articles, article titles, authors of article and id of article etc. I can display all of the articles on my homepage.php But I came over following problem. Say I want to display 1 article on a different page, lets call it article.php, on my homepage.php I will have link below each article that brings user to article.php page. But I want to display different content on article.php depending on selected article so on id of selected article. So say a user clicks a link like this:(which is on homepage.php)
    Code:
    <a href="article.php?id=1">View this article</a>
    Now I need to display title,article,a uthor of article with id 1 on articles.php page. This is the problem, how do I find out when to show info of article with id 1, 2 or 3 etc...? I mean how can I know what information to display on article.php page?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Code:
    SELECT article, article_title, author
    FROM database_table_that_contains_article
    WHERE article_id = 1
    Unless I have completely misunderstood

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      The article id you are passing in your example URL can be accessed:
      Code:
      $article = $_GET['id']; // need to error check this first and stripslash at the very least.
      You can then use the variable in your SQL query as code green says.

      Cheers
      nathj

      Comment

      • ilya Kraft
        New Member
        • Jan 2011
        • 134

        #4
        Hi,

        Thnx for posts guys ))) nathj, will
        Code:
        $article = $_GET['id'];
        collect id from here >> articles.php?id=1 ?
        So I can than use something like code green mentioned to collect data from database.
        Code:
        SELECT article, article_title, author
        FROM database_table_that_contains_article
        WHERE article_id = [U]$article[/U]
        Am I right? Thnx again )))

        Comment

        • John Doe
          New Member
          • Jun 2011
          • 20

          #5
          Ilya,

          Yes, what you posted is correct.

          Just to pick up on what NathJ mentioned in his php comment, you are causing a serious issue by doing what you posted. Make sure you read up about 'SQL Injection' which is what you are making possible.

          At the very least, you need to ensure that the ID that is given is actually an integer value, eg...

          Code:
          $article = (int)$_GET['id'];

          Comment

          Working...