Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mixing PHP and HTML output in an ECHO 1

Status
Not open for further replies.

cutley

Programmer
Jan 8, 2003
17
US
I cannot believe I haven't run into this until now. Take a look at what I'm trying to do:

echo &quot;<option value=$my_office[office_id] <? if ($office == $my_office[office_id]) echo \&quot;selected\&quot;; ?>>$my_office[office_name]&quot;;

The embedded PHP shows up in the HTML source, instead of executing. So you will see the following when you view source:
<? if ($office == $my_office[office_id]) echo \&quot;selected\&quot;; ?>

I have tried escaping various characters with no success. Is this even possible without breaking it up into multiple ECHO's?

Thanks in advance.
 
Your statement is a PHP one, so technically your trying PHP within PHP!

You could break up your statement into separate echos or you could test with your If statement first and echo he whole line with or(else) without the &quot;selected&quot;.
BDC.
 
Hi,

You are re-opening a new php section within a php section.

You should do something like this:

echo &quot;<option value=\&quot;&quot; . $my_office[office_id] . &quot;\&quot;&quot;;
if ($office == $my_office[office_id])
{
echo &quot;SELECTED&quot;;
}
echo &quot;NAME=\&quot;&quot; . $my_office[office_name] . &quot;\&quot;\n&quot;;

That should work. You can also do it this way too:

<OPTION VALUE=&quot;<? echo $my_office[office_id]; ?>&quot; <? if ($office == $my_office[office_id]) { echo &quot;SELECTED&quot;; }?> NAME=&quot;<? echo $my_office[office_name];?>&quot;>

Hope this helps!
relax.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top