Page 1 of 1

echo problem

Posted: Tue Feb 06, 2007 1:06 am
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

Posted: Tue Feb 06, 2007 1:20 am
by Christopher
Your first example should work fine. What is the output? Do you get error messages?

Posted: Tue Feb 06, 2007 1:35 am
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

Re: echo problem

Posted: Tue Feb 06, 2007 1:43 am
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.

Posted: Tue Feb 06, 2007 2:08 am
by Jim_Bo
Hi,

Works like a charm, I get it now.

Thanks!