Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: PHP common code for pages  (Read 6384 times)

0 Members and 1 Guest are viewing this topic.

Dread

    Topic Starter


    Beginner

    Thanked: 1
    PHP common code for pages
    « on: November 26, 2009, 11:52:51 PM »
    I'm working locally with the latest WAMP Server on Windows. I understand the include concept so we don't have to rewrite the code. But then we have to include that line in all the pages to have that effect for all the pages. For instance including the 3 php files header, left-nav and footer. Can I have pages with code ONLY for those page body and have the header, left-nav, footer included automatically for all the pages in a folder ? I'm not sure if this is possible locally but at least is that possible for a domain hosted in a web-server (like in yahoo) & how ?
    My apologies if this doesn't make any sense, let me know ....

    kpac

    • Web moderator
    • Moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: PHP common code for pages
    « Reply #1 on: November 27, 2009, 12:32:58 PM »
    Quote
    Can I have pages with code ONLY for those page body and have the header, left-nav, footer included automatically for all the pages in a folder ?
    Sorry, but here you lost me. ;D

    If you can explain it a bit more I'll be happy to help if I can.

    Dread

      Topic Starter


      Beginner

      Thanked: 1
      Re: PHP common code for pages
      « Reply #2 on: November 28, 2009, 09:15:08 PM »
      I have the files header.php, left-nav.php, footer.php. I would ideally include these files in the other pages, page1.php & so on to page100.php. I kind of hate the idea of writing code to include files in all these 100 pages. Is there a better way to automatically include files to all the page1...100.php ? Does that make any sense ?
      I appreciate your help.

      kpac

      • Web moderator
      • Moderator


      • Hacker

      • kpac®
      • Thanked: 184
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: PHP common code for pages
      « Reply #3 on: November 29, 2009, 04:53:45 AM »
      Yeah, it makes sense now.

      Unfortunately there is no other way to include the files in all the pages. The easiest thing to do now would be to copy and paste the line so you don't have to type.

      Dread

        Topic Starter


        Beginner

        Thanked: 1
        Re: PHP common code for pages
        « Reply #4 on: November 29, 2009, 11:21:22 PM »
        So that means everyone in the web development is writing the include code for the header/left-nav/footer in every single page of the 100s or 1000 page site ?? Well, that's something not cool. I guess that's how it is now. Thanks for your response.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: PHP common code for pages
        « Reply #5 on: November 30, 2009, 02:24:54 AM »
        Normally when you have 100's or 1000\s of pages you can eliminate the different content and move it into a database of some sort. Then use page parameters to select which record to show.

        Either that, or isolate separate content into each of the 100 or 1000 pages and use the same PHP file to selectively include them based on page parameters.
        I was trying to dereference Null Pointers before it was cool.

        Dread

          Topic Starter


          Beginner

          Thanked: 1
          Re: PHP common code for pages
          « Reply #6 on: November 30, 2009, 09:25:12 PM »
          ... move it into a database of some sort. Then use page parameters to select which record to show.

          Well, do have the MySql DB .. and all the pages would have the same header,left-nav and footer. Wouldn't I still have to include the file with the dynamic code in all the pages ??

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: PHP common code for pages
          « Reply #7 on: December 01, 2009, 02:54:58 AM »
          No. BEcause you wouldn't have separate pages.

          Say- for example, the pages were all articles. What would be done is the articles would be stored in database; and, using page parameters, accessed based on a id or key of some sort. The "articleText" or similar Database field would be inserted in a specific location of the page- that is, the php file would incude() all the appropriate left, top, right, etc and what have you includes, the HTML for formatting the article text (table? span tag? who knows) the pages would all be the same except for the article content; and while there would sort of be multiple "pages" there would only be ONE file.

          I was trying to dereference Null Pointers before it was cool.

          kpac

          • Web moderator
          • Moderator


          • Hacker

          • kpac®
          • Thanked: 184
            • Yes
            • Yes
            • Yes
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 7
          Re: PHP common code for pages
          « Reply #8 on: December 01, 2009, 09:36:14 AM »
          Here's an example for an articles section.

          Code: [Select]
          <?php

          // Retrieve the article id from the location, e.g. articles.php?article_id=XXX
          $articleid $_GET['article_id']

          // Connect to database
          $con mysql_connect($db_server$db_user$db_pass);
          if (!
          $con)
          {
          die('Could not connect: ' mysql_error());
          }

          // Select DB
          $db_selected mysql_select_db($db_name$con);
          if (!
          $db_selected
          {
          die('Can\'t use DB: ' mysql_error());
          }

          // Generate MySQL query
          $result mysql_query("SELECT * FROM articles WHERE article_id='$articleid'");
          if(!
          $result)
          {
          die("Can't execute query: " mysql_error());
          }

          // Include header, leftnav whatever
          include(dirname(__FILE__) . "/header.php"); // ETC.

          // As long as the article id matches the DB, do this.
          while($row mysql_fetch_array($result))
          {
                  echo 
          $row['article_content'];
          }

          // Include more
          include(dirname(__FILE__) . "/footer.php");

          ?>

          « Last Edit: December 02, 2009, 09:00:52 AM by kpac »

          Dread

            Topic Starter


            Beginner

            Thanked: 1
            Re: PHP common code for pages
            « Reply #9 on: December 01, 2009, 04:24:17 PM »
            Thanks guys, i'm going to try it out & let you know how it goes .. you guys are awesome!