4

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?

kiran
  • 641
  • 2
  • 12
  • 34

2 Answers2

3

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.

Spooky
  • 1,235
  • 15
  • 17
  • @ Spooky: I am not getting where i am making mistake. what should i change? Can you please explain with my code? How it should be? – kiran Jul 21 '15 at 12:30
  • His first opening ` – Spooky Jul 21 '15 at 12:38
  • @ Spooky : I am getting error in your `New small method` code. is there anything missing in that code? – kiran Jul 21 '15 at 13:10
  • @ kiran Sorry, I made a mistake with singlequote concat, I have edited that. It should be good now, however, take a look at NOTE part I aded. – Spooky Jul 21 '15 at 13:14
  • @ kiran .. and if You read that NOTE, know that `label` requires `for` attribute, something like this ` ` ... That would be the proper HTML syntax/semantics. – Spooky Jul 21 '15 at 13:21
  • @Spooky: Actually, a label is allowed to wrap a form element. And in that case, you do not nead to include a "for" property. This syntax is not commonly known, but it is valid. See also http://stackoverflow.com/questions/774054/should-i-put-input-tag-inside-label-tag/774065#774065. – John Slegers Jul 21 '15 at 13:28
  • @ John Slegers .. Yes, it appears so it can be done, but I doubt that kiran will have benefits out of third valid example You pointed out. – Spooky Jul 21 '15 at 14:05
0

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;
}
?>
John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • @ John Slegers: the values of color is not displaying. is my code of fetching color values is right? – kiran Jul 21 '15 at 13:08
  • @kiran : What IS showing? Do you get ANY output at all for your colors? – John Slegers Jul 21 '15 at 13:10
  • @kiran : You misspelled `colorname` in your SQL query. You spelled it as `colornmae` instead. I corrected the spelling in my code examples. Let me know if you still have a problem after fixing the spelling! – John Slegers Jul 21 '15 at 13:23
  • in above code `$color = [];` is this declaration right? getting syntax error here. – kiran Oct 29 '15 at 10:37
  • @kiran : What version of PHP are you using? If you're using PHP 5.3 or below, you should use `$color = array();` instead. – John Slegers Oct 29 '15 at 11:04
  • @kiran : I just updated my answer and replaced `$color = [];` with `$color = array();`, so it also works in PHP 5.3 and below. – John Slegers Oct 29 '15 at 11:07
  • @ John Slegers : Thanks a lot for looking this again. I am using PHP 5.3.8 version. Still that was giving error. I replaced it with `$color = array();`. Its working now. :) – kiran Oct 29 '15 at 11:45