echo problem

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
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

echo problem

Post by Jim_Bo »

Hi,

cant figure out the syntax for the following, so it can be echoed to the screen. The problem is the select option within the html:

Code: Select all

<select name="choice" value="choice">
    <option>Select Choice</option>	   
    <option <?php echo $choice=='product_name' ? 'selected' : ''?> value="product_name">Product Name</option>
    <option <?php echo $choice=='description' ? 'selected' : ''?> value="description">Description</option>
  </select>
How should it be set out using:

Code: Select all

echo '<select name="choice" value="choice">
    <option>Select Choice</option>	   
    <option $choice=='product_name' ? 'selected' : '' value="product_name">Product Name</option>
    <option $choice=='description' ? 'selected' : '' value="description">Description</option>
  </select>';
Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Your first example should work fine. What is the output? Do you get error messages?
(#10850)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

It works fine as html, but I want to put the whole lot between php tags using echo, rather than straight html.


Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: echo problem

Post by Christopher »

The you need to separate the logical parts and concatenate the them together with the literal strings:

Code: Select all

echo '<select name="choice" value="choice">
    <option>Select Choice</option>	   
    <option ' . ($choice=='product_name' ? 'selected' : '') . 'value="product_name">Product Name</option>
    <option ' . ($choice=='description' ? 'selected' : '') . 'value="description">Description</option>
  </select>';
I added parens so most programmers would see what was going on.
(#10850)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Works like a charm, I get it now.

Thanks!
Post Reply