Is there a more efficient way to do this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    Is there a more efficient way to do this?

    Hi, I'm using PHP 4.3. I have 15 pages in which I need to take the
    content of the BODY and put it in a little table ...

    <table>
    <tr><td colspan="3"><im g src="header.gif "></td></tr>
    <tr>
    <td><img src="left_img.g if"></td>
    <td><!-- body content goes here --></td>
    <td><img src="right_img. gif"></td>
    </tr>
    <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
    </table>

    It is a pain to have to cut and paste all this HTML code (the above is
    just an abbreviated version) into all 15 pages. And plus, if the shell
    changes, I'll have to change it in 15 pages. Any advice on an
    efficient way to add this to everything?

    Thanks, - Dave

  • Chung Leong

    #2
    Re: Is there a more efficient way to do this?

    laredotornado@z ipmail.com wrote:
    Hi, I'm using PHP 4.3. I have 15 pages in which I need to take the
    content of the BODY and put it in a little table ...
    >
    <table>
    <tr><td colspan="3"><im g src="header.gif "></td></tr>
    <tr>
    <td><img src="left_img.g if"></td>
    <td><!-- body content goes here --></td>
    <td><img src="right_img. gif"></td>
    </tr>
    <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
    </table>
    >
    It is a pain to have to cut and paste all this HTML code (the above is
    just an abbreviated version) into all 15 pages. And plus, if the shell
    changes, I'll have to change it in 15 pages. Any advice on an
    efficient way to add this to everything?
    >
    Thanks, - Dave
    Get the file contents with file_get_conten ts(), then pluck out the body
    with a regular expression. Something like:

    $s = file_get_conten ts("bobo.html") ;
    preg_match("/<\s*body.*?>(.* ?)<\/\s*body.*?>/is", $s, $m);
    $body = $m[1];

    Comment

    • Rik

      #3
      Re: Is there a more efficient way to do this?

      laredotornado@z ipmail.com wrote:
      Hi, I'm using PHP 4.3. I have 15 pages in which I need to take the
      content of the BODY and put it in a little table ...
      >
      <table>
      <tr><td colspan="3"><im g src="header.gif "></td></tr>
      <tr>
      <td><img src="left_img.g if"></td>
      <td><!-- body content goes here --></td>
      <td><img src="right_img. gif"></td>
      </tr>
      <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
      </table>
      >
      It is a pain to have to cut and paste all this HTML code (the above is
      just an abbreviated version) into all 15 pages. And plus, if the
      shell changes, I'll have to change it in 15 pages. Any advice on an
      efficient way to add this to everything?

      well, first of all I really urge you to forget tables for layout and to use
      css. Tables are for tabular data only.

      with that out of the way:

      ---head.html----------
      <table>
      <tr><td colspan="3"><im g src="header.gif "></td></tr>
      <tr>
      <td><img src="left_img.g if"></td>
      <td>
      ----------------------
      ---foot.html----------
      </td>
      <td><img src="right_img. gif"></td>
      </tr>
      <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
      </table>
      ----------------------
      --page.php------------
      include('head.h tml');
      /* generate content */
      include('foot.h tml');
      ----------------------

      Alternatively, you could get ALL your static HTML in these two files, andf
      all pages which need it are in the same dir: echo only the content in the
      *.php pages, and create an .htaccess:

      --.htaccess------------
      php_flag auto_prepend_fi le head.html
      php_flag auto_append_fil e foot.html
      -----------------------
      ---page.php------------
      echo $content;
      -----------------------

      Grtz,
      --
      Rik Wasmus


      Comment

      • Jerry Stuckle

        #4
        Re: Is there a more efficient way to do this?

        laredotornado@z ipmail.com wrote:
        Hi, I'm using PHP 4.3. I have 15 pages in which I need to take the
        content of the BODY and put it in a little table ...
        >
        <table>
        <tr><td colspan="3"><im g src="header.gif "></td></tr>
        <tr>
        <td><img src="left_img.g if"></td>
        <td><!-- body content goes here --></td>
        <td><img src="right_img. gif"></td>
        </tr>
        <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
        </table>
        >
        It is a pain to have to cut and paste all this HTML code (the above is
        just an abbreviated version) into all 15 pages. And plus, if the shell
        changes, I'll have to change it in 15 pages. Any advice on an
        efficient way to add this to everything?
        >
        Thanks, - Dave
        >
        Or, you don't even need php - you can do a server-side include, if your
        server is set up to parse the file for SSI:

        <!--#include virtual="/file/contains/mystuff.html" -->

        Slightly more efficient than using PHP if you're already parsing the
        file for SSI (the server will handle it without calling PHP). But
        probably less efficient if you aren't already parsing it.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • laredotornado@zipmail.com

          #5
          Re: Is there a more efficient way to do this?

          --.htaccess------------
          php_flag auto_prepend_fi le head.html
          php_flag auto_append_fil e foot.html
          -----------------------
          ---page.php------------
          echo $content;
          -----------------------
          Regarding this idea, will the prepend happen at the very beginning of
          the page? I still want each of my 15 pages to have custom titles (e.g.
          the <TITLEelement is different for each) and I wouldn't want to screw
          up the HTML by prepending data before the "<HTML><HEAD><T ITLE>..."
          elements.

          Anyway, great ideas.

          Thanks, -


          Rik wrote:
          laredotornado@z ipmail.com wrote:
          Hi, I'm using PHP 4.3. I have 15 pages in which I need to take the
          content of the BODY and put it in a little table ...

          <table>
          <tr><td colspan="3"><im g src="header.gif "></td></tr>
          <tr>
          <td><img src="left_img.g if"></td>
          <td><!-- body content goes here --></td>
          <td><img src="right_img. gif"></td>
          </tr>
          <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
          </table>

          It is a pain to have to cut and paste all this HTML code (the above is
          just an abbreviated version) into all 15 pages. And plus, if the
          shell changes, I'll have to change it in 15 pages. Any advice on an
          efficient way to add this to everything?
          >
          >
          well, first of all I really urge you to forget tables for layout and to use
          css. Tables are for tabular data only.
          >
          with that out of the way:
          >
          ---head.html----------
          <table>
          <tr><td colspan="3"><im g src="header.gif "></td></tr>
          <tr>
          <td><img src="left_img.g if"></td>
          <td>
          ----------------------
          ---foot.html----------
          </td>
          <td><img src="right_img. gif"></td>
          </tr>
          <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
          </table>
          ----------------------
          --page.php------------
          include('head.h tml');
          /* generate content */
          include('foot.h tml');
          ----------------------
          >
          Alternatively, you could get ALL your static HTML in these two files, andf
          all pages which need it are in the same dir: echo only the content in the
          *.php pages, and create an .htaccess:
          >
          --.htaccess------------
          php_flag auto_prepend_fi le head.html
          php_flag auto_append_fil e foot.html
          -----------------------
          ---page.php------------
          echo $content;
          -----------------------
          >
          Grtz,
          --
          Rik Wasmus

          Comment

          • Gary Hasler

            #6
            Re: Is there a more efficient way to do this?

            Or, you can change all 15 pages to one php page, which includes the body
            contents from one of 15 html files.
            This would change a typical url from "yourdomain .com/page3.php" to
            "yourdomain .com/main.php?page3" , and the php script would have a line like
            <td><?php include ($root . "/" . $query . ".html"); ?></td>
            For the title, just echo a variable $title which gets looked up based on
            the query string.

            Note this has other repercussions like affecting search engine behaviour
            and caching, so it may not be the ideal solution.

            "laredotornado@ zipmail.com" wrote:
            Hi, I'm using PHP 4.3. I have 15 pages in which I need to take the
            content of the BODY and put it in a little table ...
            >
            <table>
            <tr><td colspan="3"><im g src="header.gif "></td></tr>
            <tr>
            <td><img src="left_img.g if"></td>
            <td><!-- body content goes here --></td>
            <td><img src="right_img. gif"></td>
            </tr>
            <tr><td colspan="3"><im g src="bottom.gif "></td></tr>
            </table>
            >
            It is a pain to have to cut and paste all this HTML code (the above is
            just an abbreviated version) into all 15 pages. And plus, if the shell
            changes, I'll have to change it in 15 pages. Any advice on an
            efficient way to add this to everything?
            >
            Thanks, - Dave

            Comment

            • Rik

              #7
              Re: Is there a more efficient way to do this?

              laredotornado@z ipmail.com wrote:
              >--.htaccess------------
              >php_flag auto_prepend_fi le head.html
              >php_flag auto_append_fil e foot.html
              >-----------------------
              >---page.php------------
              >echo $content;
              >-----------------------
              >
              Regarding this idea, will the prepend happen at the very beginning of
              the page?
              Yes, before line 0 so to speak.
              I still want each of my 15 pages to have custom titles
              (e.g.
              the <TITLEelement is different for each) and I wouldn't want to
              screw
              up the HTML by prepending data before the "<HTML><HEAD><T ITLE>..."
              elements.
              Either check & set the title in the head.php (instead of a static *.html),
              or go with Gary Hasler's option (which is the one I normally use). The
              drawbacks he mentions can easily be solved.

              I usually use some RewriteRule's in .htaccess to make it seems they're
              different static pages.

              Grtz,
              --
              Rik Wasmus


              Comment

              • Gary Hasler

                #8
                Re: Is there a more efficient way to do this?

                Rik wrote:
                I usually use some RewriteRule's in .htaccess to make it seems they're
                different static pages.
                That's the ultimate solution--then the clients never even need know there's
                any php involved. I'd love to have most of our site done like that but
                unfortunately rewrite's not available on our current hosted server.

                Comment

                • Chung Leong

                  #9
                  Re: Is there a more efficient way to do this?

                  Rik wrote:
                  --page.php------------
                  include('head.h tml');
                  /* generate content */
                  include('foot.h tml');
                  ----------------------
                  Agh! Drives me crazy that people keep recommending this hamfisted
                  method. Doing includes in lieu of calling function is a poor way to
                  program. A page header or footer is not different from other
                  functionalities in your application. To reuse it, wrap it in a
                  function. Example:

                  --interface.php---

                  <?php function printHeader() { ?>
                  <html><head>. ..
                  <?php } ?>

                  <?php function printFooter() { ?>
                  ....</body></html>
                  <?php } ?>
                  -----------------------

                  Note the immediate advantage here of having the HTML within the same
                  file. It's also obvious how you would implement any parameterized
                  behavior--by passing parameters. To handle different page title, for
                  example:

                  <?php function printHeader($ti tle = "Home Page") { ?>
                  <html><head>
                  <title><?php echo htmlspecialchar s($title); ?></title>
                  <?php } ?>

                  The individual pages then pass their own titles when they need to
                  override the default.

                  Comment

                  • Rik

                    #10
                    Re: Is there a more efficient way to do this?

                    Chung Leong wrote:
                    Rik wrote:
                    >--page.php------------
                    >include('head. html');
                    >/* generate content */
                    >include('foot. html');
                    >----------------------
                    >
                    Agh! Drives me crazy that people keep recommending this hamfisted
                    method. Doing includes in lieu of calling function is a poor way to
                    program. A page header or footer is not different from other
                    functionalities in your application. To reuse it,
                    Pardon, 'reuse'? How many heads/closing html tags do you want in a page? I
                    normally opt for just one, but hey, that's silly me.
                    wrap it in a
                    function. Example:
                    >
                    --interface.php---
                    >
                    <?php function printHeader() { ?>
                    <html><head>. ..
                    <?php } ?>
                    >
                    <?php function printFooter() { ?>
                    ...</body></html>
                    <?php } ?>
                    -----------------------
                    >
                    Note the immediate advantage here of having the HTML within the same
                    file. It's also obvious how you would implement any parameterized
                    behavior--by passing parameters. To handle different page title, for
                    example:
                    >
                    <?php function printHeader($ti tle = "Home Page") { ?>
                    <html><head>
                    <title><?php echo htmlspecialchar s($title); ?></title>
                    <?php } ?>
                    >
                    The individual pages then pass their own titles when they need to
                    override the default.
                    Well, that's indeed a pro to the include.
                    However, a con is this: If I want to change something in the head once the
                    project is running, I usually don't want to fiddle around in files anymore,
                    I want to be able to change it in an interface. While a *.php page can just
                    be opened and displayed in an textarea, I'm definitely more at ease working
                    on the plain HTML bits.

                    Then again, my bigger projects all have a dynamically built head section,
                    little to no actual static HTML content there.

                    Grtz,
                    --
                    Rik Wasmus


                    Comment

                    • Chung Leong

                      #11
                      Re: Is there a more efficient way to do this?

                      Rik wrote:
                      Pardon, 'reuse'? How many heads/closing html tags do you want in a page? I
                      normally opt for just one, but hey, that's silly me.
                      Code reuse typically means using the same code in more than one place,
                      as opposed to it being used multiple time in a single operation.
                      Well, that's indeed a pro to the include.
                      However, a con is this: If I want to change something in the head once the
                      project is running, I usually don't want to fiddle around in files anymore,
                      I want to be able to change it in an interface. While a *.php page can just
                      be opened and displayed in an textarea, I'm definitely more at ease working
                      on the plain HTML bits.
                      I would hardly call that a con. You can easily accommodate such a
                      requirement by changing the function. Since it's easier to edit the
                      template as a whole as opposed to it being splitted (since start tags
                      have to be reconciled with the end tag), you can load the whole frame
                      into the editor then use a keyword to determine where the split should
                      occur.
                      Then again, my bigger projects all have a dynamically built head section,
                      little to no actual static HTML content there.
                      One shouldn't confuse implementation with purpose. The purpose is the
                      same whether it's a block of static HTML or some super dynamic code--to
                      output a page header. When you have code that's properly encapsulated,
                      the caller wouldn't need to know how the process is implemented.

                      Comment

                      • Rik

                        #12
                        Re: Is there a more efficient way to do this?

                        Chung Leong wrote:
                        Rik wrote:
                        >Pardon, 'reuse'? How many heads/closing html tags do you want in a
                        >page? I normally opt for just one, but hey, that's silly me.
                        >
                        Code reuse typically means using the same code in more than one place,
                        as opposed to it being used multiple time in a single operation.
                        Well, the template file can also be reused as easily :-). But this is just a
                        sidetrack, I understand you want the same function to apply to different
                        pages, even projects, and keep them alike. If one only plans on doing one
                        project (someone only doing his own homepage, something like that), you CAN
                        make your code as reusable as hell, but it probably isn't worth the hassle.
                        >Well, that's indeed a pro to the include.
                        >However, a con is this: If I want to change something in the head
                        >once the project is running, I usually don't want to fiddle around
                        >in files anymore, I want to be able to change it in an interface.
                        >While a *.php page can just be opened and displayed in an textarea,
                        >I'm definitely more at ease working on the plain HTML bits.
                        >
                        I would hardly call that a con. You can easily accommodate such a
                        requirement by changing the function. Since it's easier to edit the
                        template as a whole as opposed to it being splitted (since start tags
                        have to be reconciled with the end tag), you can load the whole frame
                        into the editor then use a keyword to determine where the split should
                        occur.
                        Let's say I have an included file with all my functions. One blurts out a
                        header, another one a footer, and another a tablecell, an anchor, you name
                        it. I'd have a hell of a time to make an interface where people couldn't
                        change the functions, but can change the HTML, and only the HTML.

                        Keeping it in different files, or even in a database, will make live a lit
                        simpler for me and for the code of the UI.
                        >Then again, my bigger projects all have a dynamically built head
                        >section, little to no actual static HTML content there.
                        >
                        One shouldn't confuse implementation with purpose. The purpose is the
                        same whether it's a block of static HTML or some super dynamic
                        code--to output a page header. When you have code that's properly
                        encapsulated,
                        the caller wouldn't need to know how the process is implemented.

                        That was not the pont I was trying to make. What I actually ment to say was:
                        due to the fact I dynamically create all the content of the <headsection
                        (<link>'s, <meta>'s ), and want complete control over them by an interface,
                        I've never used the option to include() a static head. The option to include
                        a file with only a single '<html>' doesn't seem very worthwhile.

                        So to sum it up: you have a point from a developers standpoint. For someone
                        doing one page in his spare time it could well be overkill.

                        I'm not aware of any performance issue between the 2 different approaches,
                        but if there are, let me know.

                        Grtz,
                        --
                        Rik Wasmus


                        Comment

                        • Chung Leong

                          #13
                          Re: Is there a more efficient way to do this?

                          Rik wrote:
                          Well, the template file can also be reused as easily :-). But this is just a
                          sidetrack, I understand you want the same function to apply to different
                          pages, even projects, and keep them alike. If one only plans on doing one
                          project (someone only doing his own homepage, something like that), you CAN
                          make your code as reusable as hell, but it probably isn't worth the hassle.
                          I didn't say the method doesn't work. I said it's hamfisted.
                          Let's say I have an included file with all my functions. One blurts out a
                          header, another one a footer, and another a tablecell, an anchor, you name
                          it. I'd have a hell of a time to make an interface where people couldn't
                          change the functions, but can change the HTML, and only the HTML.
                          That's a non-issue. If that becomes a requirement, then you can change
                          the function. And if that's a requirement from the start, doing an
                          include on the file is a crappy method as it basically allows the
                          end-user to blow away the server.
                          So to sum it up: you have a point from a developers standpoint. For someone
                          doing one page in his spare time it could well be overkill.
                          Writing a function is not an overkill--it's basic programming.

                          Comment

                          • Rik

                            #14
                            Re: Is there a more efficient way to do this?

                            Chung Leong wrote:
                            Rik wrote:
                            >Well, the template file can also be reused as easily :-). But this
                            >is just a sidetrack, I understand you want the same function to
                            >apply to different pages, even projects, and keep them alike. If one
                            >only plans on doing one project (someone only doing his own
                            >homepage, something like that), you CAN make your code as reusable
                            >as hell, but it probably isn't worth the hassle.
                            >
                            I didn't say the method doesn't work. I said it's hamfisted.
                            >
                            >Let's say I have an included file with all my functions. One blurts
                            >out a header, another one a footer, and another a tablecell, an
                            >anchor, you name it. I'd have a hell of a time to make an interface
                            >where people couldn't change the functions, but can change the HTML,
                            >and only the HTML.
                            >
                            That's a non-issue. If that becomes a requirement, then you can change
                            the function. And if that's a requirement from the start, doing an
                            include on the file is a crappy method as it basically allows the
                            end-user to blow away the server.
                            >
                            >So to sum it up: you have a point from a developers standpoint. For
                            >someone doing one page in his spare time it could well be overkill.
                            >
                            Writing a function is not an overkill--it's basic programming.
                            Chung,
                            I can follow you to a certain point here, but according to your logic the
                            use of more then 1 file is always useless. just one big >200K index.php,
                            functions at the start, huge nested if statements further on. If you page
                            changes you can just change that file("If that becomes a requirement, then
                            you can change the function"). Basic programming.

                            Just have functions like:
                            page1(){
                            ?>
                            the actual HTML content of page 1
                            <?php
                            }

                            etc. etc.

                            Yep, that will be good coding, and easily maintainable.
                            --
                            Rik Wasmus


                            Comment

                            • Jerry Stuckle

                              #15
                              Re: Is there a more efficient way to do this?

                              Chung Leong wrote:
                              Rik wrote:
                              >
                              >>--page.php------------
                              >>include('head .html');
                              >>/* generate content */
                              >>include('foot .html');
                              >>----------------------
                              >
                              >
                              Agh! Drives me crazy that people keep recommending this hamfisted
                              method. Doing includes in lieu of calling function is a poor way to
                              program. A page header or footer is not different from other
                              functionalities in your application. To reuse it, wrap it in a
                              function. Example:
                              >
                              Sorry, Chung, I agree with Rik on this one. This is an excellent way to
                              reuse the code. I use various incarnations of this (either in PHP or
                              using SSI) in a lot of code.

                              It sure is a lot better than trying to keep everything in one page.


                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...