kiran
kiran

Reputation: 641

inserting php code inside EOT

There are two tables, products and productcolor. Each product can have multiple color. I want to fetch color of products in dropdown. But not able to do that with 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 have searched for solution. Then I came to know that php code can't be written in EOT. php variables can be shown in EOT. But here i want to do looping to values of color from database. I have tried various combination but nothing worked. Above is my one of the attempt of doing that which is not working.

How do I get colorname of product in dropdown which is fetching from database as object in between EOT?

Can anyone please help me in this?

Upvotes: 4

Views: 6161

Answers (2)

John Slegers
John Slegers

Reputation: 47101

Make sure you :

  • do not have ANY other characters (including whitespace characters) on the same line as your EOT; statement.
  • do not have any PHP expressions in your string. Only variables are allowed and they MUST have 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 could put your expressions in a seperate function/method and include its output as a variable.

This code should also work:

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

Upvotes: 0

Spooky
Spooky

Reputation: 1316

When You use HEREDOC (it doesn't need to be 'EOT' it can be 'MYKEYWORD' as well), ending point of HEREDOC, closing HEREDOC, must be first in the line, with no whitespace before, followed by the semicolon and nothing else after in that line, otherwise it fails. That's 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 have noticed that You have foreach loop (pasted, just like that?) somewhere in between HEREDOC.

It won't work that way.

Write new method for html select > option -> value, outside that block of code, than call it, assign it to object property or variable, than use it within 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 semantic is also - bad. You probably don't want to wrap <select> <option> tags with <label>, also, Are You quite sure that You need <form> tag for each and every cart entry?!? No, no You don't.

You need only ONE form tag, I can bet..

My suggestion for You is to make Your self a cup of coffee, snap Your fingers, than re-write that whole block of code, again. Make it good. You can do it.

Upvotes: 3

Related Questions