0

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>;

1 Answer 1

6

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.