skip to Main Content

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..

2

Answers


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

    Login or Signup to reply.
  2. 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'>rn";
         $html .= "tt<div class='product-image-wrapper'>rn";
         $html .= "ttt<div class='single-products'>rn";
         $html .= "tttt<div class='productinfo text-center'>rn";
         $html .= "ttttt<img src='images/home/{$dep['product_image']}' alt='{$dep['product_name']}' />rn";
         $html .= "ttttt<h2>{$dep['product_price']}</h2>rn";
         $html .= "ttttt<p>{$dep['product_name']}</p>rn";
         $html .= "ttttt<a href='#' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>rn";
         $html .= "tttt</div>rn";
         $html .= "tttt<div class='product-overlay'>rn";
         $html .= "ttttt<div class='overlay-content'>rn";
         $html .= "tttttt<h2>{$dep['product_price']}</h2>rn";
         $html .= "tttttt<p>{$dep['product_name']}</p>rn";
         $html .= "tttttt<a href='#' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>rn";
         $html .= "ttttt</div>rn";
         $html .= "tttt</div>rn";
         $html .= "ttt</div>rn";
         $html .= "ttt<div class='choose'>rn";
         $html .= "tttt<ul class='nav nav-pills nav-justified'>rn";
         $html .= "ttttt<li><a href='#'><i class='fa fa-plus-square'></i>Add to wishlist</a></li>rn";
         $html .= "ttttt<li><a href='#'><i class='fa fa-plus-square'></i>Add to compare</a></li>rn";
         $html .= "tttt</ul>rn";
         $html .= "ttt</div>rn";
         $html .= "tt</div>rn";
         $html .= "t</div>rn";
    }
    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search