Inserting php code inside EOT

There are two tables, products and product color. Each product can have several colors. I want to get the color of the products in the dropdown. But failed to do it with the following code.

 <?php
    $results = $mysqli->query("SELECT product_code, product_name, product_desc, product_img_name, price FROM products ORDER BY id ASC");
    if($results){ 
    $products_item = '<ul class="products">';
    //fetch results set as object and output HTML
    while($obj = $results->fetch_object())
    {  
        $id = $obj->id;
        $results1 = $mysqli->query("SELECT colornmae FROM productcolor where id=$id");
      if($results1){ 
        while($obj1 = $results1->fetch_object())
        {  
        $color[] = $obj1->colorname;
        }
      }
    $products_item .= <<<EOT
        <li class="product">
        <form method="post" action="cart_update.php">
        <div class="product-content"><h3>{$obj->product_name}</h3>
        <div class="product-thumb"><img src="images/{$obj->product_img_name}"></div>
        <div class="product-desc">{$obj->product_desc}</div>
        <div class="product-info">
        Price {$currency}{$obj->price} 

        <fieldset>

        <label>
            <span>Color</span>
            <select name="product_color">
            foreach ($color as $value) {
            <option value="{$value}">{$value}</option>
            }
            </select>
        </label>

        <label>
            <span>Quantity</span>
            <input type="text" size="2" maxlength="2" name="product_qty" value="1" />
        </label>

        </fieldset>
        <input type="hidden" name="product_code" value="{$obj->product_code}" />
        <input type="hidden" name="type" value="add" />
        <input type="hidden" name="return_url" value="{$current_url}" />
        <div align="center"><button type="submit" class="add_to_cart">Add</button></div>
        </div></div>
        </form>
        </li>
    EOT;
    }
    $products_item .= '</ul>';
    echo $products_item;
    }
    ?>    

      

I was looking for a solution. Then I found out that php code cannot be written to EOT

. php variables can be displayed in EOT. But here I want to do a loop for color values ​​from the database. I tried various combinations but nothing worked. Above is my attempt at doing what doesn't work.

How do I get the colorname

product in the dropdown that fetches from the database as an object between EOT

?

Can anyone help me with this?

+3


source to share


2 answers


Make sure that you:

  • do not have ANY other characters (including whitespace) on the same line as the operator EOT;

    .
  • You don't have PHP expressions in your string. Only variables are allowed and they MUST be in the format {$variable}

    .

To make your code more readable, you should also fix your indentation!


This code should work:



<?php
$results = $mysqli->query("SELECT product_code, product_name, product_desc, product_img_name, price FROM products ORDER BY id ASC");
if($results){ 
    $products_item = '<ul class="products">';
    //fetch results set as object and output HTML
    while($obj = $results->fetch_object()) {  
        $id = $obj->id;
        $results1 = $mysqli->query("SELECT colorname FROM productcolor where id=$id");
        $color = array();
        if($results1){ 
            while($colorvalue = $results1->fetch_object()) {  
                $color[] = $colorvalue->colorname;
            }
        }
        $products_item .= <<<EOT
    <li class="product">
        <form method="post" action="cart_update.php">
            <div class="product-content">
                <h3>{$obj->product_name}</h3>
                <div class="product-thumb">
                    <img src="images/{$obj->product_img_name}">
                </div>
                <div class="product-desc">
                    {$obj->product_desc}
                </div>
                <div class="product-info">
                    Price {$currency}{$obj->price} 

                    <fieldset> 
                        <label>
                            <span>Color</span>
                            <select name="product_color">
EOT;
        foreach ($color as $value) {
        $products_item .= <<<EOT
                                <option value="{$value}">{$value}</option>
EOT;
        }
        $products_item .= <<<EOT
                            </select>
                        </label>

                        <label>
                            <span>Quantity</span>
                            <input type="text" size="2" maxlength="2" name="product_qty" value="1" />
                        </label>

                    </fieldset>
                    <input type="hidden" name="product_code" value="{$obj->product_code}" />
                    <input type="hidden" name="type" value="add" />
                    <input type="hidden" name="return_url" value="{$current_url}" />
                    <div align="center">
                        <button type="submit" class="add_to_cart">Add</button>
                    </div>
                </div>
            </div>
        </form>
    </li>
EOT;
    }
    $products_item .= '</ul>';
    echo $products_item;
}
?>    

      


As an alternative to splitting your string into bits, you can put your expressions in a separate function / method and include its output as a variable.

This code should work as well:

<?php
function print_colors($color) {
    $returnstring = "";
    foreach ($color as $value) {
        $returnstring .= "<option value=\"{$value}\">{$value}</option>";
    }
    return $returnstring;
}

$results = $mysqli->query("SELECT product_code, product_name, product_desc, product_img_name, price FROM products ORDER BY id ASC");
if($results){ 
    $products_item = '<ul class="products">';
    //fetch results set as object and output HTML
    while($obj = $results->fetch_object()) {  
        $id = $obj->id;
        $results1 = $mysqli->query("SELECT colorname FROM productcolor where id=$id");
        $color = array();
        if($results1){ 
            while($colorvalue = $results1->fetch_object()) {  
                $color[] = $colorvalue->colorname;
            }
        }
        $products_item .= <<<EOT
    <li class="product">
        <form method="post" action="cart_update.php">
            <div class="product-content">
                <h3>{$obj->product_name}</h3>
                <div class="product-thumb">
                    <img src="images/{$obj->product_img_name}">
                </div>
                <div class="product-desc">
                    {$obj->product_desc}
                </div>
                <div class="product-info">
                    Price {$currency}{$obj->price} 

                    <fieldset> 
                        <label>
                            <span>Color</span>
                            <select name="product_color">
                                {print_colors($color)}
                            </select>
                        </label>

                        <label>
                            <span>Quantity</span>
                            <input type="text" size="2" maxlength="2" name="product_qty" value="1" />
                        </label>

                    </fieldset>
                    <input type="hidden" name="product_code" value="{$obj->product_code}" />
                    <input type="hidden" name="type" value="add" />
                    <input type="hidden" name="return_url" value="{$current_url}" />
                    <div align="center">
                        <button type="submit" class="add_to_cart">Add</button>
                    </div>
                </div>
            </div>
        </form>
    </li>
EOT;
    }
    $products_item .= '</ul>';
    echo $products_item;
}
?>

      

0


source


When you use HEREDOC (it doesn't need to be "EOT", it could also be "MYKEYWORD"), the HEREDOC endpoint that closes the HEREDOC must be the first on the line, no spaces before it, followed by a semicolon and nothing else. string , otherwise it fails. This is why your code won't work.

$heredoc = <<<KEYWORD

  {$variable} - this will print value of $variable 
  $variable - this will print $variable, not $variable value

KEYWORD;

      

EDIT:

I noticed that you have a foreach loop (inserted, exactly the same?) Somewhere between the HEREDOC.

It won't work.

Write a new method for html select> option β†’ value, outside this block of code, than call it, assign a property or object variable to it, than use it inside HEREDOC .. concatenate, whatever.



// New small method
public function smallLoop($color) {
    $x=null;
    foreach($color as $value)
    $x.='<option value="'.$value.'">'.$value.'</option>';
    return $x;
}

// Inside loop
<select name="product_color">
    {$obj->smallLoop($color)}
</select>

      

Note

Your html semantics are bad too. You probably don't want to wrap the tags <select> <option>

with <label>

, also, are you sure you need a tag <form>

for every cart entry?!? No no.

You only need one form tag, I bet ..

My suggestion for you is to make yourself a cup of coffee, snap your fingers, and not write the whole block of code again. Do it well. You can do that.

+3


source







All Articles