从JSON文件中检索图像,该文件位于数据库中并以HTML img标记显示

时间:2017-06-17 07:37:56

标签: javascript php jquery html json

我真的不知道我到底哪里错了。可能很简单,但我无法弄清楚。任何帮助都会很棒,拜托并谢谢。

JSON代码(它存储在数据库中tblproducts表的'images'列中)

{"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代码:

<?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 } ?>

我不知道javascript以及如何从json获取每个图像并将其显示在不同的标记中。请帮忙...提前致谢

1 个答案:

答案 0 :(得分:0)

假设您的JSON在列import android.app.Fragment; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import in.webblue.nuclity.Activity.Logs.SaveLog; import in.webblue.nuclity.R; import static android.content.Context.MODE_PRIVATE; /** * Created by Akshay on 15-06-2017. */ public class TutorialFragment extends Fragment { private String Class_Name = "TutorialFragment"; private boolean ranBefore; View topLevelLayout1; View topLevelLayout2; View myView; String methodName = "onCreateView"; public static TutorialFragment newInstance() { TutorialFragment f = new TutorialFragment(); return f; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); myView = inflater.inflate(R.layout.tutorial_layout, container, false); topLevelLayout1=myView.findViewById(R.id.tutorial1); topLevelLayout2=myView.findViewById(R.id.tutorial2); if (!isFirstTime()) { topLevelLayout1.setVisibility(View.INVISIBLE); topLevelLayout2.setVisibility(View.INVISIBLE); } return myView; } private boolean isFirstTime() { try { SharedPreferences preferences = this.getActivity().getSharedPreferences("RanBefore", MODE_PRIVATE); boolean ranBefore = preferences.getBoolean("RanBefore", false); if (!ranBefore) { SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("RanBefore", true); editor.commit(); topLevelLayout1.setVisibility(View.VISIBLE); topLevelLayout2.setVisibility(View.INVISIBLE); topLevelLayout1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { topLevelLayout1.setVisibility(View.INVISIBLE); topLevelLayout2.setVisibility(View.VISIBLE); return false; } }); topLevelLayout2.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { topLevelLayout2.setVisibility(View.INVISIBLE); return false; } }); } } catch (Exception e){ Log.e(getClass().getName(),"Method Name :"+methodName+ " "+ e.getStackTrace().toString()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { SaveLog.saveLog(getContext(),Class_Name,methodName,e.toString()); } } return ranBefore; } } 中存储为字符串。你可以这样做:

images

我已经改变了图像附加逻辑,你所做的是错误的。您不能以这种方式附加图像,而是创建一个容器并将新创建的图像附加到该容器中。