Taha Kirmani
Taha Kirmani

Reputation: 1274

Why my Loop is breaking when i add HTML in my Php?

I have written the following code. The whole thing was working fine until i added the following HTML in my PHP code.

  echo  "
        <div class='fpd-product' title='$name' 
        data-thumbnail='$base_image_loc'>
     ";

If i remove this HTML(div), it fetches all the record from database and echo them, but if i add this HTML it only prints the last output from the loop.

Kindly tell me where i am making mistake.

      <?php

      $query  ='select * from products '
             . 'inner join product_description '
             . 'on products.product_id= product_description.product_id';


      $query_run= mysql_query($query);


       while($query_fetch= mysql_fetch_array($query_run))
       {
        $product_id=    $query_fetch['product_id'];
        $price=         $query_fetch['retail_price'];
        $name=          $query_fetch['name'];               

        echo $name;


        //Image Query
         $image_query= "select * from product_images where product_id=$product_id";
         $image_query_run= mysql_query($image_query);

         $base_image= mysql_fetch_array($image_query_run);
         $base_image_loc= $base_image['images'];



    //If i remove this echo it fetches all the records from database and echo them,
    //otherwise it will only print the last record.


         echo  "
              <div class='fpd-product' title='$name' 
              data-thumbnail='$base_image_loc'>
                                                     ";


          while($image_query_fetch=   mysql_fetch_array($image_query_run))
          {
            $image_location=    $image_query_fetch['images'];
            $image_name=        $image_query_fetch['name']; 

           }                                     

      }                     


     ?> 
<img src="images/sweater/basic.png" title="Base" data-parameters='{"x": 332, "y": 311, "colors": "#D5D5D5,#990000,#cccccc", "price": 20}' />

<img src="images/sweater/highlights.png" title="Hightlights" data-parameters='{"x": 332, "y": 311}' />
<img src="images/sweater/shadow.png" title="Shadow" data-parameters='{"x": 332, "y": 309}' />

</div>

Thanks Taha

Upvotes: 0

Views: 72

Answers (4)

Alexander
Alexander

Reputation: 20244

For debugging such problems, you have to check the HTML source code (rightclick in browser -> view source). If you see all records there, it is a problem purely with HTML; that means you should first use an HTML validator before anything else.

Upvotes: 1

JEYASHRI R
JEYASHRI R

Reputation: 452

Change your echo statement

    echo  "<div class='fpd-product' title=".$name." data-thumbnail=".$base_image_loc.">";

and close the div, end of the code

 echo "</div>";

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143966

You need a </div> here:

  while($image_query_fetch=   mysql_fetch_array($image_query_run))
  {
    $image_location=    $image_query_fetch['images'];
    $image_name=        $image_query_fetch['name']; 

  }             

  echo "</div>";

Upvotes: 2

Agha Umair Ahmed
Agha Umair Ahmed

Reputation: 1020

your echo statement should look like this

echo  "<div class='fpd-product' title='".$name."' data-thumbnail='".$base_image_loc."'>";

and where is div close? or you can do with html like this

<div class='fpd-product' title='<?php echo $name; ?>' data-thumbnail='<?php echo $base_image_loc; ?>'>

and close the div after while loop

Upvotes: 1

Related Questions