dongnanbi4942 2014-07-28 12:25
浏览 41
已采纳

将变量循环到Array中,然后将其编码为1 JSON

I had been searching for a while, and I can't seem to find a solution for my problem, so I created an account.

My situation is as below.

  1. I am doing a for loop upload image form.

  2. Every time it loops, it will have 1 $Listingid.photo

  3. I would like to add all the $Listingid.photo into an array, then package it into 1 JSON. and echo it.

And I am not very sure how to approach it. At the moment it is creating 1 JSON response per loop.

Only the last two lines of my code really matters to the problem.

My code:

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);

ob_start();
session_start();
include 'connect.php';

$Listingid =($_POST['listingid']);

if(isset($_FILES['file'])) {
    $_FILES["file"]["name"] = preg_replace('/\s+/', '', $_FILES["file"]["name"]);
    for($i=0;$i<count($_FILES["file"]["name"]);$i++)
    {

        $photo=$_FILES["file"]["name"][$i];
        $newname=$Listingid.$photo;
        move_uploaded_file($_FILES["file"]["tmp_name"][$i], "photo/$Listingid$photo");

        mysqli_query($con,"INSERT INTO listingpic (pic,listingid) VALUES ('$newname','$Listingid');") or die(mysqli_error($con));

        $arr = array('picname' => $Listingid.$photo);
        echo json_encode($arr);
    }
}
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何修改为正确的?求解决
      • ¥15 django访问管理员界面问题
      • ¥20 python homework完成
      • ¥20 求解 多变量系统的最小二乘辨识问题的推导以及matlab仿真
      • ¥15 arduino esp8266 Blinker编译报问题
      • ¥15 ubuntu18.04运行模型,直接死机
      • ¥30 (问卷调查)莫名其妙丢了u盘,你们会是什么心理状态
      • ¥100 Spark+android应⽤案例
      • ¥15 yolov8 目标检测 重叠 遮挡
      • ¥20 微信聊天记录如何部署到服务器上