1

I have been trying to loop the entire html div part so that it creates 12 columns(twitter-bootstrap) through div tags. In this case it shows that variable 'dep' is undefined..

 <? php
$q1="select * from product limit 12";
$ret=mysqli_query($mysqli,$q1);
while($dep=mysqli_fetch_assoc($ret)){
    ?>
                    <div class="col-sm-4">
                        <div class="product-image-wrapper">
                            <div class="single-products">
                                <div class="productinfo text-center">
                                <?php   echo "<img     src=\"images/home/".$dep['product_image']."\" alt='".$dep['product_name']."' />";
                                    echo "<h2>".$dep['product_price']."</h2>";
                                    echo "<p>".$dep['product_name']."</p>";
                                    ?>
                                    <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>";
                                </div>
                                <div class="product-overlay">
                                    <div class="overlay-content">
                                    <?php   echo "<h2>".$dep['product_price']."</h2>";
                                        echo "<p>".$dep['product_name']."</p>"; 
                                        ?>
                                        <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
                                    </div>
                                </div>
                            </div>
                            <div class="choose">
                                <ul class="nav nav-pills nav-justified">
                                    <li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
                                    <li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
 }
                    ?>

I also tried this kind of format too,but it only displayed the echo keywords.In this case it displays the echo tags as if they were a part of html itself

  <? php
$q1="select * from product";
$ret=mysqli_query($mysqli,$q1);
while($dep=mysqli_fetch_assoc($ret)){

                    echo '<div class="col-sm-4">';
                        echo '<div class="product-image-wrapper">';
                            echo '<div class="single-products">';
                                echo '<div class="productinfo text-center">';
                                    echo "<img src=\"images/home/".$dep['product_image']."\" alt='".$dep['product_name']."' />";
                                    echo "<h2>".$dep['product_price']."</h2>";
                                    echo "<p>".$dep['product_name']."</p>";
                                    echo "<a href=\"#\" class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>Add to cart</a>";
                                echo "</div>";
                                echo "<div class=\"product-overlay\">";
                                    echo "<div class=\"overlay-content\">";
                                        echo "<h2>".$dep['product_price']."</h2>";
                                        echo "<p>".$dep['product_name']."</p>";
                                        echo "<a href=\"#\" class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>Add to cart</a>";
                                    echo "</div>";
                                echo "</div>";
                            echo "</div>";
                            echo "<div class=\"choose\">";
                                echo "<ul class=\"nav nav-pills nav-justified\">";
                                    echo "<li><a href=\"\"><i class=\"fa fa-plus-square\"></i>Add to wishlist</a></li>";
                                    echo"<li><a href=\"\"><i class=\"fa fa-plus-square\"></i>Add to compare</a></li>";
                                echo "</ul>";
                            echo "</div>";
                        echo "</div>";
                    echo "</div>";
}
                    ?>

I cannot understand where am i going wrong.Please help..

4
  • Welcome to Stack Overflow. There are a lot of ways to do this. What errors are you getting or what is "going wrong"? Commented Jun 25, 2015 at 22:26
  • maybe to change 'mysqli_fetch_assoc' to 'mysqli_fetch_array'? Commented Jun 25, 2015 at 22:28
  • Since you say it's displaying the echo tags as if they were part of html, i think all that's wrong in the second example is <? php instead of <?php (the extra space). <? is a php short tag, but it could be disabled on your machine. The other error "'dep' is undefined.." might mean that there were 0 results from the query. Commented Jun 26, 2015 at 2:41
  • @billirichards- yeah it was the space between <? and php that produced the error. Thanx. :) Commented Jun 26, 2015 at 9:27

2 Answers 2

1

Try:

<?php
$q1 = "select * from product";
$ret = mysqli_query($mysqli,$q1);
$html = "";
while($dep=mysqli_fetch_assoc($ret)){
     $html .= "\t<div class='col-sm-4'>\r\n";
     $html .= "\t\t<div class='product-image-wrapper'>\r\n";
     $html .= "\t\t\t<div class='single-products'>\r\n";
     $html .= "\t\t\t\t<div class='productinfo text-center'>\r\n";
     $html .= "\t\t\t\t\t<img src='images/home/{$dep['product_image']}' alt='{$dep['product_name']}' />\r\n";
     $html .= "\t\t\t\t\t<h2>{$dep['product_price']}</h2>\r\n";
     $html .= "\t\t\t\t\t<p>{$dep['product_name']}</p>\r\n";
     $html .= "\t\t\t\t\t<a href='#' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>\r\n";
     $html .= "\t\t\t\t</div>\r\n";
     $html .= "\t\t\t\t<div class='product-overlay'>\r\n";
     $html .= "\t\t\t\t\t<div class='overlay-content'>\r\n";
     $html .= "\t\t\t\t\t\t<h2>{$dep['product_price']}</h2>\r\n";
     $html .= "\t\t\t\t\t\t<p>{$dep['product_name']}</p>\r\n";
     $html .= "\t\t\t\t\t\t<a href='#' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>\r\n";
     $html .= "\t\t\t\t\t</div>\r\n";
     $html .= "\t\t\t\t</div>\r\n";
     $html .= "\t\t\t</div>\r\n";
     $html .= "\t\t\t<div class='choose'>\r\n";
     $html .= "\t\t\t\t<ul class='nav nav-pills nav-justified'>\r\n";
     $html .= "\t\t\t\t\t<li><a href='#'><i class='fa fa-plus-square'></i>Add to wishlist</a></li>\r\n";
     $html .= "\t\t\t\t\t<li><a href='#'><i class='fa fa-plus-square'></i>Add to compare</a></li>\r\n";
     $html .= "\t\t\t\t</ul>\r\n";
     $html .= "\t\t\t</div>\r\n";
     $html .= "\t\t</div>\r\n";
     $html .= "\t</div>\r\n";
}
echo $html;
?>

Found 2 syntax errors, the <? php and one echo"...". The echo syntax may get overlooked by PHP in the end, but I noticed it.

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

Comments

0

Your second code has a space between the <? and php otherwise they are essentially the same thing. Or perhaps it was a formatting issue?

Comments

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.