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 : /
The second is trying to “append the
ifstatement to the string”. You can’t write it that way.Try this:
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
echostatements 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
is not proper PHP code while
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: