greenpool
greenpool

Reputation: 561

php/html code inside php script - how to display in html

I am trying to put the following into a php block so i can reuse it in other webpages but i'm getting way too many errors.

can somebody please tell me how i can achieve this?

block of code inside php file:

<select name="products">
                        <option value="select">Select</option>
                        <option value="Box" <?php echo @$product_list['Box'] ?>>Box</option>
                        <option value="TV"  <?php echo @$product_list['TV'] ?>>TV</option>
                        <option value="Setup"  <?php echo @$product_list['Setup'] ?>>Setup</option>
                    </select>

I need the above code echoed inside the html page.

Thanks!

I just tried the heredoc syntax which doesn't appear to work which must mean i'm doing something wrong sigh

EDIT:

Any idea why i would get the following error for the code below: syntax error, unexpected T_IF

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

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

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

Upvotes: 0

Views: 559

Answers (1)

nickb
nickb

Reputation: 59699

Use single quotes:

$select = '<select name="products">
               <option value="select">Select</option>
               <option value="Box" ' . $product_list['Box'] . '>Box</option>
               <option value="TV"  ' . $product_list['TV'] . '>TV</option>
               <option value="Setup" ' . $product_list['Setup'] . '>Setup</option>
           </select>';
echo $select;

Or, close and open the PHP block:

<?php

?>
<select name="products">
    <option value="select">Select</option>
    <option value="Box" <?php echo @$product_list['Box'] ?>>Box</option>
    <option value="TV"  <?php echo @$product_list['TV'] ?>>TV</option>
    <option value="Setup"  <?php echo @$product_list['Setup'] ?>>Setup</option>
</select>
<?php

Or, use output buffering:

<?php
ob_start();
?>
<select name="products">
    <option value="select">Select</option>
    <option value="Box" <?php echo @$product_list['Box'] ?>>Box</option>
    <option value="TV"  <?php echo @$product_list['TV'] ?>>TV</option>
    <option value="Setup"  <?php echo @$product_list['Setup'] ?>>Setup</option>
</select>
<?php
$select = ob_get_clean();
echo $select;

Upvotes: 6

Related Questions