Page 1 of 1

html in php question

Posted: Tue May 30, 2006 10:24 am
by halfhoot
Hello all,

I've been trying to figure out if it's possible at all to put html in the middle of php without breaking the php or using echo or print. Hear me out... this is the situation:

I'm just making an admin section for a web site. I used to work in cold fusion, but we want to move over to php for server reasons. Here is pseudocode of what I used to do in cold fusion.

Code: Select all

if url.mode = section
{
  if url.action = add
  {
      if formwassubmitted
      {
          handle form here
      }
      else
      {
          show form here
      }
  }
  else if url.action = edit
  {
     etc etc etc
  }
}
So pretty much it's several if statements deep depending on what mode they've chosen, what action they've chosen, and whether or not they submitted a form or whatnot. Often times in the deepest levels of the nest there is a lot of html code... and it's just plain ugly to try to put all that in echos or prints. Cold Fusion worked as a tag language where even the ifs and else ifs were tags that didn't require continuity... didn't have to be together in one <?php ... ?> tag.

I'm looking for any possible way other than having a different include file for every single section to have html look nice in large amounts of code... I'm guessing there's nothing I can do and I have to live with horrible looking huge chunks of html in string quotes that don't color code in dreamweaver or millions of different include files. If I'm wrong though, I would be pleasantly suprised and appreciative.

Posted: Tue May 30, 2006 10:39 am
by Christopher
Remember to add PHP tags around your code.

It would be something like:

Code: Select all

<?php
if url.mode = section
{
if url.action = add
{
if formwassubmitted
{
?>
handle form here
<?php
}
else
{
?>
show form here
<?php
}
}
else if url.action = edit
{
etc etc etc
}
}
?>

Posted: Tue May 30, 2006 12:28 pm
by halfhoot
:oops: I had no idea you could split out the middle of {} areas between different <?php?> tags... Thank you... I feel silly. But happy!

Re: html in php question

Posted: Tue May 30, 2006 1:29 pm
by RobertGonzalez
EDIT | I just read your post. Sorry, the initial response is probably better than mine. :)
halfhoot wrote:Here is pseudocode of what I used to do in cold fusion.

Code: Select all

if url.mode = section
{
  if url.action = add
  {
      if formwassubmitted
      {
          handle form here
      }
      else
      {
          show form here
      }
  }
  else if url.action = edit
  {
     etc etc etc
  }
}
So pretty much it's several if statements deep depending on what mode they've chosen, what action they've chosen, and whether or not they submitted a form or whatnot. Often times in the deepest levels of the nest there is a lot of html code... and it's just plain ugly to try to put all that in echos or prints.
OK, I am guessing this is how the PHP would look, with a little stuff added in to help you achieve what you want.

Code: Select all

<?php
if (isset($_GET['mode']) && $_GET['mode'] == 'section')
{
  if (isset($_GET['action']) && $_GET['action'] == 'add')
  {
      if ($_POST['formwassubmitted'] == true)
      {
          // Insert processing code here
          // You could feasibly add all output
          // to a single concatenated var and echo at the end
          $display .= 'All processed data for output';
      }
      else
      {
          $display = 'Enter form HTML here or call it from another stored var';
      }
  }
  elseif ($_GET['action'] == 'edit')
  {
     // Process editing
  }
}
?>