Ok

I have the very basics of includes. I can include images etc in to my pages no problem.

I am now adding an articles section. One idea I had, would be to have the articles included in to the original page. So it would go like so.

User clicks article 1 link.
Article 1 is brought to the page, and interpreted as HTML (So it remains formatted)

A. Is this possible?
B. If yes, can you give me a few tips/clues?

    You can include files to php script with include() function. For example you could have predefined header.php and footer.php for all your articles and article_1.html, ..., article_n.html containing article bodies. You can easily create your index page:

    <?php
     include 'header.php';
     // menu
     echo '<a href="index.php?aid=1">1st article</a><br />';
     ...
     echo '<a href="index.php?aid=n">n-th article</a><br />';
    
     // includeing article
     if( file_exists('article_'.(int)$_GET['aid'].'.htm') ){
      include 'article_'.(int)$_GET['aid'].'.htm';
     }
     else{
       echo 'Choose an article from menu';
     }
    
     include 'footer.php';
    ?>
    
      konrad wrote:
      <?php
      
       if( file_exists('article_'.(int)$_GET['aid'].'.htm') ){
        include 'article_'.(int)$_GET['aid'].'.htm';
       }
      
      

      That part is the only bit I'm not understanding that well. I get the general idea, it's checking to see if the file is real first, which is good. Then I think it's fetching it, then including it.

      I'm just not great with the syntax. I will attempt this tomorrow, and I'll post back here for help.

      Thanks very much for the reply. Any chance you could throw me a net? (You know, the age old, give a man a fish, he eats for a day, give him a net, he eats for life)

        Yes, he's checking to see if the file exists before it's included. Would it look less foreign to you if written like so:

        if( file_exists('article_' . $_GET['aid'] . '.htm') ){ 
          include 'article_' . $_GET['aid'] . '.htm'; 
        } 

        ? If so, all you need to realize is that konrad is simply using the b[/b] part to typecast the variable. What I mean by that is he wants to force PHP to interpret 'aid' as an integer, not as a string or anything else.

        Why? Because that way, you know people aren't messing with the query string and trying to fool your script into including other files. If all they're allowed to do is change the numbers, they'll have a hard time including other files.

        Instead of typecasting using b[/b], you could actually use a function such as [man]intval[/man] like so:

        if( file_exists('article_' . intval($_GET['aid']) . '.htm') ){ 
          include 'article_' . intval($_GET['aid']) . '.htm'; 
        } 

          Ok, this might sound stupid. But I'm asuming "aid" would be replaced by my filename? (Article ID, I guessed) and I could change .htm to certain other extensions if I wanted to?

          EDIT: On looking closer at it - just the " 1 " or the " n " would be replaced with a filename/article number, yes? So according to this script, I should name a file something like

          article_1.htm

          If i'm right, the next question is this. In this code, it's refering to "index.php"

          My articles are will be appearing on a page "articles.php" so I figure I Can just swap out index.php with articles.php no problems. What I want to know is can I pull articles from within a directory called articles? I've tried replacing article with
          /articles/article
          and I've tried also replacing it with the full URL like so

          'http://www.paint-zone.co.uk/articles/article_'

          EDIT: I've decided that's there's no point in pulling from a directory, instead do as you guys said, call it index and put the articles section IN a directory. Thanks!

          Next problem:

          I have 3 articles so far, and for some reason, it's not pulling just 1 of them (Called "what.htm"

          Any basic ideas as to what could be causeing this? Looking at the code, I can't see anything. I'll post back in a min with the code.

          // Begginer menu
          // Could be an include if needed
          echo '<a href="index.php?aid=what">What is paintball?</a><br />';
          echo '<a href="index.php?aid=lesson1goggles">Lesson 1: Goggles</a><br />';
          echo '<a href="index.php?aid=lesson2glovesshoes">Lesson 2: Gloves and Shoes</a><br />';
          
          // includeing article 
          if( file_exists('article_' . $_GET['aid'] . '.htm') ){
            include 'article_' . $_GET['aid'] . '.htm';
          } 
          
           else{
             echo 'Choose an article from menu';
          }

          EDIT: Haha, I simply missed the "article_ " prefix.

          Thanks guys! This has work excellently!

          EDIT AGAIN: Whoops, I've misentrerpated part of the thread! I don't know how to unmark threads resolved, if it's possible. So I'll ask the question here, if not I'll start a new thread.

          From what I can see, you guys are saying I should have my articles call artcile_1.htm etc etc, so that people can't put nasty stuff in to the query. Is their anyway I can do this with words? I intend on having a lot of articles and needing to know what is in each one without opening it up.

            Well, if you don't plan on using anything but letters and numbers, you can use [man]ctype_alnum/man to validate it.

              Does that excluder underscores?

                Yes. You'd have to use something like [man]preg_match[/man] for something a bit more customized:

                if(preg_match('/[^a-z0-9_-]/i', $_GET['aid'])) {
                	// invalid characters found
                } else {
                	// name is valid, continue displaying	
                }

                  Thanks man. You guys rock once again.

                    Write a Reply...