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!
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
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?
<?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:
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?
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:
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.
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.