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
Jim_Bo
Forum Contributor
Posts: 390 Joined: Sat Oct 02, 2004 3:04 pm
Post
by Jim_Bo » Tue Feb 06, 2007 1:06 am
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
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Feb 06, 2007 1:20 am
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 » Tue Feb 06, 2007 1:35 am
Hi,
It works fine as html, but I want to put the whole lot between php tags using echo, rather than straight html.
Thanks
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Feb 06, 2007 1:43 am
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 » Tue Feb 06, 2007 2:08 am
Hi,
Works like a charm, I get it now.
Thanks!