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?

3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@ 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?
His first opening <?php tag is not in the same indentation as the rest of the code.. that's why I wrote what I wrote.
@ Spooky : I am getting error in your New small method code. is there anything missing in that code?
@ 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.
@ kiran .. and if You read that NOTE, know that label requires for attribute, something like this <label for="uniqueID"> Label text </label> <select id="uniqueID" name="uniqueID"><!-- option value, option value , option value .. --> </select> ... That would be the proper HTML syntax/semantics.
|
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;
}
?>

7 Comments

@ John Slegers: the values of color is not displaying. is my code of fetching color values is right?
@kiran : What IS showing? Do you get ANY output at all for your colors?
@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!
in above code $color = []; is this declaration right? getting syntax error here.
@kiran : What version of PHP are you using? If you're using PHP 5.3 or below, you should use $color = array(); instead.
|

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.