0

Let's say I have a file which goes like this:

    <?php 
if(isset($_GET['cat_id'])){
    $cat_id = $_GET['cat_id'];
    $get_cat_pro = "select * from products where cat_id='$cat_id'";
    $run_cat_pro = mysqli_query($con,$get_cat_pro);
    while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
        $pro_id = $row_cat_pro['product_id'];
        $pro_title = $row_cat_pro['product_title'];
        $pro_cat = $row_cat_pro['cat_id'];
        $pro_brand = $row_cat_pro['brand_id'];
        $pro_desc = $row_cat_pro['product_desc'];
        $pro_price = $row_cat_pro['product_price'];
        $pro_image = $row_cat_pro['product_img1'];

        echo "
        <section>
            <div class='container'>
                <div class='row'>
                    "include_once 'php/includes/overall/widget.php'"

                    <div class='col-sm-9 padding-right'>
                        <div class='features_items'><!--features_items-->
                            <h2 class='title text-center'>Products</h2>

                            <ul class='pagination'>
                                <li class='active'><a href="">1</a></li>
                                <li><a href=''>2</a></li>
                                <li><a href=''>3</a></li>
                                <li><a href=''>&raquo;</a></li>
                            </ul>
                        </div><!--features_items-->
                    </div>
                </div>
            </div>
        </section>
        ";
    }
}
?>

And in this file I want to echo out an include_once command but I don't know the proper way to do this while echoing some html text.. So I get this error message as well:

Parse error: syntax error, unexpected 'include_once' (T_INCLUDE_ONCE), expecting ',' or ';' on line 19

And the line 19 is this:

"include_once 'php/includes/overall/widget.php'"

So how to correct this issue?

mrid
  • 5,782
  • 5
  • 28
  • 71

4 Answers4

0

Try the following...

Because you started the echo with " you can't have " inside it, you can only use " again when the echo is ending or when you're connecting it to another string.

As you can see I connected it by using periods like so:

...ss='row'>" . include_once ('php/includes/overall/widget.php'); ."<div clas...

    <?php 
if(isset($_GET['cat_id'])){
    $cat_id = $_GET['cat_id'];
    $get_cat_pro = "select * from products where cat_id='$cat_id'";
    $run_cat_pro = mysqli_query($con,$get_cat_pro);
    while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
        $pro_id = $row_cat_pro['product_id'];
        $pro_title = $row_cat_pro['product_title'];
        $pro_cat = $row_cat_pro['cat_id'];
        $pro_brand = $row_cat_pro['brand_id'];
        $pro_desc = $row_cat_pro['product_desc'];
        $pro_price = $row_cat_pro['product_price'];
        $pro_image = $row_cat_pro['product_img1'];

        echo "
        <section>
            <div class='container'>
                <div class='row'>".

                    include_once ('php/includes/overall/widget.php');


                    ."<div class='col-sm-9 padding-right'>
                        <div class='features_items'><!--features_items-->
                            <h2 class='title text-center'>Products</h2>

                            <ul class='pagination'>
                                <li class='active'><a href="">1</a></li>
                                <li><a href=''>2</a></li>
                                <li><a href=''>3</a></li>
                                <li><a href=''>&raquo;</a></li>
                            </ul>
                        </div><!--features_items-->
                    </div>
                </div>
            </div>
        </section>
        ";
    }
}
A Friend
  • 1,420
  • 17
  • 22
0
echo "
        <section>
            <div class='container'>
                <div class='row'>";

                 include_once 'php/includes/overall/widget.php';

                echo " <div class='col-sm-9 padding-right'>
                        <div class='features_items'><!--features_items-->
                            <h2 class='title text-center'>Products</h2>

                            <ul class='pagination'>
                                <li class='active'><a href="">1</a></li>
                                <li><a href=''>2</a></li>
                                <li><a href=''>3</a></li>
                                <li><a href=''>&raquo;</a></li>
                            </ul>
                        </div><!--features_items-->
                    </div>
                </div>
            </div>
        </section>
        ";
  • The include_once can be called many times. It will be executed only once. require_once would fail. And his include file might have code that needs to be located there to work. – Nic3500 Nov 05 '17 at 05:57
  • Yep, good edit, but Ryan Earnshaw beat you to it :-) And his answer is better, since he explains it, not just dump code. – Nic3500 Nov 05 '17 at 06:04
0

When you want to get into PHP context, you have to put <?php ?> tags in your HTML. Here you are using echo to output HTML from PHP, and requiring to do PHP again. If you want the HTML to switch to PHP to execute the include_once, you have to put the tags again. In your code, the include_once is viewed as HTML, which is not correct, hence the error.

But it gets a bit crazy with php that echos HTML, that requires PHP. So why not simply exit the PHP context to output your html, and get back in for the include_once?

Like so:

<?php 
if(isset($_GET['cat_id']))
{
    $cat_id = $_GET['cat_id'];
    $get_cat_pro = "select * from products where cat_id='$cat_id'";
    $run_cat_pro = mysqli_query($con,$get_cat_pro);
    while($row_cat_pro=mysqli_fetch_array($run_cat_pro))
    {
        $pro_id = $row_cat_pro['product_id'];
        $pro_title = $row_cat_pro['product_title'];
        $pro_cat = $row_cat_pro['cat_id'];
        $pro_brand = $row_cat_pro['brand_id'];
        $pro_desc = $row_cat_pro['product_desc'];
        $pro_price = $row_cat_pro['product_price'];
        $pro_image = $row_cat_pro['product_img1'];
        ?>

        <section>
            <div class='container'>
                <div class='row'>
                    <?php include_once('php/includes/overall/widget.php'); ?>

                    <div class='col-sm-9 padding-right'>
                        <div class='features_items'><!--features_items-->
                            <h2 class='title text-center'>Products</h2>

                            <ul class='pagination'>
                                <li class='active'><a href="">1</a></li>
                                <li><a href=''>2</a></li>
                                <li><a href=''>3</a></li>
                                <li><a href=''>&raquo;</a></li>
                            </ul>
                        </div><!--features_items-->
                    </div>
                </div>
            </div>
        </section>

        <?php
    }
}
?>
Nic3500
  • 8,144
  • 10
  • 29
  • 40
0

First off, since you want to echo a multiline string, I'd use Heredoc.

You could use fread(). Here's an example of what that would look like.

    $str = fread(fopen('php/includes/overall/widget.php', 'a+'), filesize('php/includes/overall/widget.php'));
    echo "
         ALL YOUR HTML
         {$str}
         SOME MORE HTML";
Mav
  • 1,087
  • 1
  • 15
  • 37
  • For mentionning Heredoc, +1. Your no.1 is not valid, the OP did not put a ; in his code in the question. -1. No.2 very weird way to do that, since all he has to do is return to PHP context. Not saying it is invalid, just convoluted compared to other ways PHP provides. So no +-. Final score, 0 ;-) – Nic3500 Nov 05 '17 at 06:20
  • @Mav If you mean this: http://www.paste.org/88025 , it returns the error `Parse error: syntax error, unexpected '">1 ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' on line 39` –  Nov 05 '17 at 06:23
  • @Nic3500 I probably saw another answer's semicolon, sorry for that. – Mav Nov 05 '17 at 07:41
  • @wraimiueahia that error exists because the anchor tag on line 29 has a double quote, and PHP interprets that as the end of the string you're printing. – Mav Nov 05 '17 at 07:43