Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to convert my php code embedded in html to php code with embedded html code. My IDE is throwing syntax errors. can someone please help, thanks.

html/php (working):

    <select name="products">
                        <option value="select">Select</option>
                        <option value="product1" <?php if (!isset($updatebtn_clicked)){ echo @$product_list['product1']; }elseif ($_POST['products'] == $product_name[0]){ echo 'selected="selected"' ;} echo
                        '>'. $product_name[0]. '</option>' ?> </select>

The above is a snippet for a single item in my drop down list. They just keep repeating for all products and it works.

php/html (fails to work):

<?php
        $select = '<select name="products">
               <option value="select">Select</option>
                value="'.$product_name[0].'"'. 
                if (!isset($updatebtn_clicked))
                { echo @$product_list[$product_name[1]]; }elseif ($_POST['products'] == $product_name[1]){ echo 'selected="selected"' ;} echo'>'. $product_name[1]. '</option></select>';
?>

the above generates a syntax error at the first if statement. I'm just not seeing why : /

share|improve this question

closed as too localized by Quentin, feeela, vascowhite, j0k, Rob Hruska Jul 27 '12 at 12:26

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
What syntax error? Why don't you reformat it so it isn't all squished onto a single line? –  Quentin Jul 26 '12 at 10:30
    
well like i said the ide complains at the first if statement: if (!isset($updatebtn_clicked)) –  greenpool Jul 26 '12 at 10:31
1  
Test it with PHP, not your IDE. –  Quentin Jul 26 '12 at 10:32
add comment

2 Answers

up vote 2 down vote accepted

The second is trying to "append the if statement to the string". You can't write it that way.

Try this:

<?php
    $select = '<select name="products">
           <option value="select">Select</option>
            value="'.$product_name[0].'"';   // <- Finish assignment here

    if (!isset($updatebtn_clicked))
    { 
        $select = $select . @$product_list[$product_name[1]]; 
    }
    elseif ($_POST['products'] == $product_name[1])
    { 
        $select = $select . 'selected="selected"';
    }

    $select = $select . '>'. $product_name[1]. '</option></select>';

    echo $select;
?>

The difference between the first and the second thing you're trying to do is: The first thing outputs HTML with intermixed PHP. The PHP code is replaced with the output of the echo statements by the PHP interpreter.

The second way is that you're putting all the HTML into a variable. There, however, you can not mix PHP and HTML. You are in the scope of the PHP script all the time, so you have to write proper PHP. The statement

$select = 'Hello ' . if (condition) echo 'foo';

is not proper PHP code while

$select = 'Hello ';
if (condition) $select = $select . 'foo';

is correct PHP syntax.

EDIT
By the way: The second sample you posted (which I modified) does not output a valid second option! It should read:

    $select = '<select name="products">
           <option value="select">Select</option>
           <option value="'.$product_name[0].'"';   // <- Finish assignment here
share|improve this answer
    
FYI this: $select = $select . 'foo'; can be shortened too: $select .= 'foo'; –  James Jul 26 '12 at 11:55
1  
I know - the entire thing could be shortened using the ternary operator. However, as the OP asked such a basic syntax question, I thought it would be best to use the full syntax (which is more intuitive) than the shortened syntax. –  Thorsten Dittmar Jul 26 '12 at 12:09
    
Thanks for that! i'm a beginner to all this so that made a lotta sense. –  greenpool Jul 27 '12 at 2:49
add comment

Try this:

<?php
$select = "
    <select name='products'>
        <option value='select'>Select</option>
        <option value='product1' ".( (!isset($updatebtn_clicked)) ? @$product_list['product1']  : "selected='selected'").">".$product_name[0]."</option>
    </select> 
";
?>
share|improve this answer
    
I don´t urderstant the code, but the correct format in a string is this –  Sangar82 Jul 26 '12 at 10:40
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.