html in php question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
halfhoot
Forum Newbie
Posts: 2
Joined: Tue May 30, 2006 9:56 am

html in php question

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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
}
}
?>
(#10850)
halfhoot
Forum Newbie
Posts: 2
Joined: Tue May 30, 2006 9:56 am

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: html in php question

Post 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
  }
}
?>
Post Reply