HTML within php file

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
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

HTML within php file

Post by Michael_C »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have become lost in the tags <?php and ?> - When I added php in amongst html code I get a parse error - "unexpected $" and the line number is the line after the last line of the php file.  I'm new to php, and am adding things in layers to ensure I get one layer working prior to moving on to the next.  Things worked fine before I added the test "if ($lbPrimaryVoted)" and "if ($lbSecondaryVoted)"- which required my adding the <?php and ?> tags.

I'm trying to control what HTML is ouputed based on values in a database table.  There are only three pieces of info I want to display:
1) a poll description
2) a submit button to either "display a poll", or "display results" (if the primary member has voted)
3) same as 2 (above), except for the secondary member.
At this point, the code snippet doesn't include the submit buttons.

Any help would be greatly appreciated.

Michael

Code: Select all

if (is_array($arPollIds))
{
  foreach ($arPollIds as $nId)
  {
    $lcQuestion = $php_poll->get_poll_question($nId) ;
    $lbPrimaryVoted = $php_poll->has_voted($nId, $lcUserName, $lcPrimaryName) ; 
    $lbSecondaryVoted = $lbSecondaryName and $php_poll->has_voted
                                    ($nId, $lcUserName, $lcSecondaryName) ; 
    ?>
    <div align="center">
    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
    <tr>
      <td width="50%"><b><font color="#6600FF"><?echo $lcQuestion; ?> 
                         </font></b><br></td>
      <?php
      if ($lbPrimaryVoted)
      {
         ?>
          <td width="25%"> <? echo $lcPrimaryName; ?><br></td>
      }
      else
      {
         ?>
         <td width="25%"> <? echo "View Status"; ?><br></td>
      }
      <?php
      if ($lbSecondaryVoted)
      {
        ?>
        <td width="25%"> <? echo $lcSecondaryName; ?><br></td>
      }
      else
      {
        ?>
        <td width="25%"> <? echo "View Status"; ?><br></td>
      }
    </tr>
    </table>
    </div>
    <?php
  }
}
else echo "Nothing to vote on" ;
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's either missing or misplaced <?php

It might be suggestable that using an editor with syntax highlighting may help lead you to how and where the tags should start and end.

Alternately, it could be suggested that instead of jumping in and out of php all over to use echos.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

feyd wrote:there's either missing or misplaced <?php

Alternately, it could be suggested that instead of jumping in and out of php all over to use echos.
Can you give me an example of what you're talking about?

Michael
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Code: Select all

<?echo $lcQuestion; ?>
Would that cause an error? Looks like it needs a space.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

feyd wrote:there's either missing or misplaced <?php
Alternately, it could be suggested that instead of jumping in and out of php all over to use echos.
Is this what you were talking about? Or, is there a more elegant approach?

Code: Select all

<td width="25%"> <? echo $lbPrimaryVoted ? $lcPrimaryName:"View Status"; ?><br></td>
Michael
Ocendo
Forum Newbie
Posts: 3
Joined: Tue Mar 07, 2006 7:55 pm

Post by Ocendo »

I'm new here(first post), but I've coded alot in PHP including forums but anyway idk if this is it but heres some problems:

Code: Select all

<?php 
      if ($lbPrimaryVoted) 
      { 
         ?> //// - Your if statement is gonna get messed up follow along now.
          <td width="25%"> <? echo $lcPrimaryName; ?><br></td> 
      }  ////- Between here
      else 
      { ////-And here are not in <? ?>, therefor there not getting run! your If is getting messed up..
         ?> //// -Never started with a <?
         <td width="25%"> <? echo "View Status"; ?><br></td> 
      } ////-No <? to start it! and no ?> to end it!
      <?php 
      if ($lbSecondaryVoted) 
      { 
        ?>  ////-You forgot the <? again!
        <td width="25%"> <? echo $lcSecondaryName; ?><br></td> 
      } 
      else 
      { ////You did it agian!
        ?> ////-No <? ?>
        <td width="25%"> <? echo "View Status"; ?><br></td> 
      }  ////-Useless, no <? ?> ////
    </tr> 
    </table> 
    </div>
Hope that helps.
You just misplaced your <? and ?> and your If never had a } to end it therefor your code can't be run properly.

Here how I would of done it and this is also what feyd was talking about:

Code: Select all

<div align="center"> 
    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1"> 
    <tr> 
      <td width="50%"><b><font color="#6600FF"><?echo $lcQuestion; ?>  
                         </font></b><br></td> 
      <?php 
      if ($lbPrimaryVoted) 
      { 
          echo('<td width="25%">' .$lcPrimaryName . '<br></td>');
      } 
      else 
      { 
         echo('<td width="25%">  "View Status" <br></td>'); 
      } 

      if ($lbSecondaryVoted) 
      { 
        echo('<td width="25%"> . $lcSecondaryName . '<br></td>');
      } 
      else 
      { 
        echo('<td width="25%"> "View Status" <br></td>');
      } 
?>
    </tr> 
    </table> 
    </div> 
    <?php 
  } 
} 
else echo "Nothing to vote on" ; 
?>
That's how I would of coded it but something is probably wrong because I've been coding C#/C++ for a while and havn't done php in a long time.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Thanks for the clarification of what feyd had said.

It looks like I could extend the approach you and feyd suggested to the whole snippet, and
do away with all the jumping in and out of php.

Is there any significant "hit" doing the echo() call? This script is so small, and it probably wouldn't matter - but, would it if dealing with larger output?

For example, does performance suffer doing:

Code: Select all

echo('<div align="center"> ') ;
     echo('<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1"> ') ;
     echo('<tr>') ;
       echo('<td width="50%"><b><font color="#6600FF">' .  $lcQuestion ) ; 
       echo('</font></b><br></td>') ;
Instead of doing:

Code: Select all

?> 
    <div align="center"> 
    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1"> 
    <tr> 
      <td width="50%"><b><font color="#6600FF"><?echo $lcQuestion; ?>  
                         </font></b><br></td> 
      <?php
Thanks again,
Michael
Ocendo
Forum Newbie
Posts: 3
Joined: Tue Mar 07, 2006 7:55 pm

Post by Ocendo »

There will be some hit because it has to pass through the processor but the hit should be very small and unnoticable, but to downsize the hit you could do this:

Code: Select all

<?php    echo('<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td width="50%"><b><font color="#6600FF">' .  $lcQuestion .  
'</font></b><br></td>') ; 
?>
Calling to many echo's would technically slow it down, but even a slow down like that would be basically nothing.
btw, the . will connect a string and a variable, and I use ' instead of " because your using " inside the echo statement. Anyway, This way would reduce some of the workload, but still echo's are very simple and don't take that long at all for php so it really dosn't matter.

Now if you want there is a function(I forgot it, hopefully someone else will remember) to check how long the script is taking to run. You could try it with the echo's and with the <? ?> to see how big the difference is. My guess is a tenth of a milisecond to a milisecond.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Ocendo wrote:Now if you want there is a function(I forgot it, hopefully someone else will remember) to check how long the script is taking to run.
Thanks for your input and example of grouping echo() output.

I would be interested in measuring script run time - there are some decisions I'm making on database layout which, if measured, might get redone. I am not sure if measuring delta time using something like microtime() would be meaningful.
I'd be interested if anyone has a better way.

I figured it wasn't an issue for the small script I am playing around with, but I'd just as soon develop a coding style that will run faster when it matters.
Post Reply