开发者

Problem with building a JSON structure

First of all, I'm uploading multiple images that goes through a for loop by the input file with multiple selections (HTML5), however when attempting to output the links in JSON, it's only outputting one image filename, even when I've selected 2+ images.

This is how I'm creating the 开发者_开发技巧JSON (the important parts, not the full script):

$count = 0;
$image_count = count($_FILES['file']);

for($i = 0; $i < $image_count; $i++)
{
    $filename = random(13) . fileExt($_FILES['file']['name'][$i]);

    move_uploaded_file($_FILES['file']['tmp_name'][$i], 'images/' . $filename);

    $output = array();

    $output['url'] = 'http://www.mywebsite.com/images/' . $filename;
    $output['name'] = $filename;

    $count++;
}

if($count == $image_count)
{
   echo json_encode($output);
}

The final output is like this:

{
    "url": "http://www.mywebsite.com/images/0Z9O3a9q1k2A8.jpg",
    "name": "0Z9O3a9q1k2A8.jpg"
}

When it should have multiple images listed like this:

{
    "url": "http:\/\/www.mywebsite.com\/images\/0Z9O3a9q1k2A8.jpg",
    "name": "0Z9O3a9q1k2A8.jpg"
},
    {
    "url": "http:\/\/www.mywebsite.com\/images\/3jn5jk5n4kj5n.jpg",
    "name": "3jn5jk5n4kj5n.jpg"
},
    {
    "url": "http:\/\/www.mywebsite.com\/images\/nj23knk5k5455.jpg",
    "name": "nj23knk5k5455.jpg"
}

If I do this:

array_push($output, 'http://www.mywebsite.com/images/' . $filename);
array_push($output, $filename);

I get an output like this:

{
    "0": "http:\/\/www.mywebsite.com\/images\/0Z9O3a9q1k2A8.jpg",
    "1": "0Z9O3a9q1k2A8.jpg",
    "2": "http:\/\/www.mywebsite.com\/images\/3jn5jk5n4kj5n.jpg",
    "3": "3jn5jk5n4kj5n.jpg",
    "4": "http:\/\/www.mywebsite.com\/images\/nj23knk5k5455.jpg",
    "5": "nj23knk5k5455.jpg",
}

I need it to be to similar to what I need it to be so I can parse it with jQuery.


Try this:

$output = array();
for($i = 0; $i < $image_count; $i++)
{
    $filename = random(13) . fileExt($_FILES['file']['name'][$i]);

    move_uploaded_file($_FILES['file']['tmp_name'][$i], 'images/' . $filename);

    $output[] = array(
        'url' => 'http://www.mywebsite.com/images/' . $filename,
        'name' => $filename
    );

    $count++;
}

With $output[] = x you can add an entry to the array. The output is then like this:

[
  {
    "url": "http:\/\/www.mywebsite.com\/images\/0Z9O3a9q1k2A8.jpg",
    "name": "0Z9O3a9q1k2A8.jpg"
  },
  {
    "url": "http:\/\/www.mywebsite.com\/images\/3jn5jk5n4kj5n.jpg",
    "name": "3jn5jk5n4kj5n.jpg"
  }
]


You need to have an array that you are using. So instead of this:

$output = array();

$output['url'] = 'http://www.mywebsite.com/images/' . $filename;
$output['name'] = $filename;

You would want something like:

$output = array();

$output[]['url'] = 'http://www.mywebsite.com/images/' . $filename;
$output[]['name'] = $filename;

That will give you output that you can then refer to in your jquery as:

success: function(response){
    response[0].name;
    response[0].url;
}

Does that make sense? The jquery code is very abbreviated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜