0

Sperate PHP include files - opening and closing brackets/braces { } for if's and while loop nd closing curly brackets/braces in between files

Hi all , I am working with PHP includes i.e:

include_once("include1.php");

And a CSS/HTML designer that is very problematic. I am trying to keep all PHP away from the designer, as they become very panicked : \ and they create many problems, deleting code and things and messing up everything.

So I have been using the PHP includes to add the PHP where necessary between their HTML.

The PHP works fine and there are zero bugs.

It is the separation of the PHP into multiple include files that is causing my some problems.

Following is an example of what I am trying to separate.

The PHP to be separated:


<?php
$foo = 0;
$bar = 0;

if($foo == 0){

    $foo++;
    $bar++;
    // more php code

    while($foo < 20){

        $foo++;
        $bar++;
        // more php code

    // Now write the designers HTML here in this loop
    ?>  

        <BR /> Some HTML 
        <?php echo $foo; ?>
         more html 
        <?php echo $bar; ?>
        <BR /> More HTML <BR /> <BR />

    <?php
    // Done with the designers code -Now close my PHP here

   }// END while($foo < 5)

}// END if($foo == 0)
?>

I want to separate this into 2 includes - include1.php and include2.php

And I want to have the designers code between the while loop. So what I did was this:


include1.php

   if($foo == 0){

    $foo++;
    $bar++;
    // more php code

      while($foo < 20){

        $foo++;
        $bar++;
       // more php code
   ?>

DESIGNERS HTML and a little mixed PHP

    <BR /> Some HTML 
    <?php echo $foo; ?>
     more html 
    <?php echo $bar; ?>
    <BR /> More HTML <BR /> <BR />

include2.php

    <?php
   // Done with the designers code -Now close my PHP here

  }// END while($foo < 5)

}// END if($foo == 0)
?>

I get an error ("Parse error: syntax error, unexpected $end in)

I have multiple if's and while's and loops.

My question is: How do I separate these braces / brackets without an error?

Thank you

  • This is a bit off topic, but if you are having issues with your designer you should really look into a template engine. I have had great success using twig http://twig.sensiolabs.org/ – MrGlass Nov 26 '12 at 19:08

3 Answers3

2

It is not possible to span loops or if statements across multiple includes, that's what's causing the syntax error. The best thing to do is to adopt a templating system that truely separates out the logic and presentation.

A quick and dirty solution would be to throw the designer's HTML into a separate .html file and replace the PHP with placeholders. In your loop, replace the placeholders with the content.:

<BR /> Some HTML 
%%_Foo_%%
 more html 
%%_Bar_%%
<BR /> More HTML <BR /> <BR />
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • That's a great idea - except the designer gets lost trying to use includes or templates. They kinda need everything right there in front of them on one page - or they get lost. So I have been trying to figure a way to leave their html there and loop it - as it needs to be duplicated via the PHP - I'm thinking of using a LOT of javascript for this now –  Nov 26 '12 at 20:13
  • @KenDawsonWinnipeg I suggest hitting the designer with a stick until he stops getting lost. You shouldn't have to write horrid back-end code just to support a front-end designer with ADD. – Sammitch Nov 26 '12 at 20:38
  • Yeah... but that's just the way it goes - we have to work with people as well as code : ) –  Nov 26 '12 at 21:17
1

Combine your php files into 1 and include a single 'designers' template file from there and you won't get that syntax error.

<?php
$foo = 0;
$bar = 0;

if($foo == 0){

    $foo++;
    $bar++;
    // more php code

    while($foo < 20){

        $foo++;
        $bar++;
        // more php code

        include 'template.php'; // Include template here
        // No need to stick the remaining portion of this file into a seperate one

   }// END while($foo < 5)

}// END if($foo == 0)
?>
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • Well... Like I said above - I have to work with this designer - anyone have any other ideas - that would be great. I'm moving on to a javascript solution that copies the designers HTML then updates with the variables grabbed by PHP - it's going to be a lot of work - so I'll keep an eye out here for another solution. Thanks all : ) –  Nov 26 '12 at 21:20
0

You can't do it that way, when you are including PHP files they all have to be valid PHP. In include1.php for example you are not closing your while loop. Executing/Including that file will therefore produce an error. If i were you i would use a real Templating System like Smarty or Twig to avoid having to mess with HTML Fragments and PHP.

Stefan
  • 2,164
  • 1
  • 23
  • 40