Retrieve image from JSON file which is in the database and display in HTML img tag

Ayanti Goswami

I really dont know where the hell i am wrong. May be it is simple but I can't figure it out. Any help would be great, please and thank you.

JSON code (it is stored in 'images' column in tblproducts table in the database)

{"200x200":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-200x200-imaezt6hypjzhdug.jpeg","400x400":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-400x400-imaezt6hypjzhdug.jpeg","800x800":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-800x800-imaezt6hypjzhdug.jpeg","unknown":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-original-imaezt6hypjzhdug.jpeg"}

HTML & PHP Code:

<?php 
   $category_id = $_GET['category_id'];
   $result = mysql_query("select * from tblproducts where category_id = '$category_id");
   while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
?>
<script>
  var data = 
      data.forEach( function(obj) {
          var img = new Image();
          img.src = obj.Img1;
          img.setAttribute("class", "banner-img");
          img.setAttribute("alt", "effy");
          document.getElementById("img-container").appendChild(img);
      });
</script>
    <img id="img-container" alt=" " class="img-responsive" /> 
    <h5>
        <a target="_blank" href="<?php echo $row['product_url']; ?>">
           <?php echo $row['product_title']; ?>
        </a>
    </h5>
   <?php echo $row['maximum_price']; ?>
<?php } ?>

I have no idea about the javascript and how to fetch each of the image from the json and display it in different tag. Please help... Thanks in advance

abhishekkannojia

Assuming your JSON is stored as string in column images. You can do something like this:

// ...
<script>
  var data = <?php echo json_decode($row['images']); ?>
      Object.keys(data).forEach(function(key) {
          var img = new Image();
          img.src = data[key];
          img.setAttribute("class", "banner-img");
          img.setAttribute("alt", "effy");
          img.classList.add('img-responsive');
          document.getElementById("img-container").appendChild(img);
      });
</script>
    <div id="img-container"></div>
    <h5>
        <a target="_blank" href="<?php echo $row['product_url']; ?>">
           <?php echo $row['product_title']; ?>
        </a>
    </h5>
// ...

I've changed the image append logic, what you were doing was wrong. You cannot append images that way, instead create a container and append the newly created images in that container.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get image from JSON file using JavaScript and display in HTML img tag

From Dev

Get image from JSON file using JavaScript and display in HTML img tag

From Dev

How to retrieve pdf and word file from MySQL database and display in an html tag

From Dev

Passing an image from an img tag to the database or server folder(not choose file)

From Dev

Retrieve html file from database once image is clicked in ajax

From Dev

how to display image in a <img> tag whose name is stored in database

From Dev

html tag <img> and <image>

From Dev

Laravel display image from path which is in database

From Dev

display a image that starts with a # in <img> tag

From Dev

Get image file name from HTML img tag by using RegEx in Notepad++

From Dev

How to display an image in android ImageView from the <img> tag

From Dev

Umbraco - How to display an image from a media picker in the src of an IMG tag?

From Dev

How to retrieve image file paths from mysql database and display the images using php

From Dev

How to retrieve image file paths from mysql database and display the images using php

From Dev

display image file from json

From Dev

How to assign JSON image value to HTML img tag

From Dev

How to retrieve BLOB image from the database and in which data type?

From Dev

HTML img tag not showing the image

From Dev

img src to azure blob storage image not working from html tag

From Dev

Load images using html <img> tag from Android expansion file

From Dev

php code to retrieve data from mysql database and display in html table

From Dev

how to retrieve data from Parse database and display in html

From Dev

Display image grid using img tag

From Dev

Can not display the image which its path comes from database

From Dev

Unable to display an image from HTML file

From Dev

Display image from json data in html?

From Dev

How to retrieve and display an image which is stored in Mongodb

From Dev

HTML - how to display local data in img tag?

From Dev

What is the difference between using img tag src attribute in HTML to display image and styling it in CSS?

Related Related

  1. 1

    Get image from JSON file using JavaScript and display in HTML img tag

  2. 2

    Get image from JSON file using JavaScript and display in HTML img tag

  3. 3

    How to retrieve pdf and word file from MySQL database and display in an html tag

  4. 4

    Passing an image from an img tag to the database or server folder(not choose file)

  5. 5

    Retrieve html file from database once image is clicked in ajax

  6. 6

    how to display image in a <img> tag whose name is stored in database

  7. 7

    html tag <img> and <image>

  8. 8

    Laravel display image from path which is in database

  9. 9

    display a image that starts with a # in <img> tag

  10. 10

    Get image file name from HTML img tag by using RegEx in Notepad++

  11. 11

    How to display an image in android ImageView from the <img> tag

  12. 12

    Umbraco - How to display an image from a media picker in the src of an IMG tag?

  13. 13

    How to retrieve image file paths from mysql database and display the images using php

  14. 14

    How to retrieve image file paths from mysql database and display the images using php

  15. 15

    display image file from json

  16. 16

    How to assign JSON image value to HTML img tag

  17. 17

    How to retrieve BLOB image from the database and in which data type?

  18. 18

    HTML img tag not showing the image

  19. 19

    img src to azure blob storage image not working from html tag

  20. 20

    Load images using html <img> tag from Android expansion file

  21. 21

    php code to retrieve data from mysql database and display in html table

  22. 22

    how to retrieve data from Parse database and display in html

  23. 23

    Display image grid using img tag

  24. 24

    Can not display the image which its path comes from database

  25. 25

    Unable to display an image from HTML file

  26. 26

    Display image from json data in html?

  27. 27

    How to retrieve and display an image which is stored in Mongodb

  28. 28

    HTML - how to display local data in img tag?

  29. 29

    What is the difference between using img tag src attribute in HTML to display image and styling it in CSS?

HotTag

Archive