2015-01-09 24 views
-3

帮助需要的朋友,我想将从数据库检索到的图像信息转换为json格式。 下面是从数据库将图像信息从服务器转换为json

// Query for a list of all existing files 
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`'; 
$result = $dbLink->query($sql); 

// Check if it was successfull 
if($result) { 
// Make sure there are some files in there 
if($result->num_rows == 0) { 
    echo '<p>There are no files in the database</p>'; 
} 
else { 
    // Print the top of a table 
    echo '<table width="100%"> 
      <tr> 
       <td><b>Name</b></td> 
       <td><b>Mime</b></td> 
       <td><b>Size (bytes)</b></td> 
       <td><b>Created</b></td> 
       <td><b>&nbsp;</b></td> 
      </tr>'; 

    // Print each file 
    while($row = $result->fetch_assoc()) { 
     echo " 
      <tr> 
       <td>{$row['name']}</td> 
       <td>{$row['mime']}</td> 
       <td>{$row['size']}</td> 
       <td>{$row['created']}</td> 
       <td><a href='get_file.php?id={$row['id']}'>Download</a></td> 
      </tr>"; 
    } 

    // Close table 
    echo '</table>'; 
} 

// Free the result 
$result->free(); 
} 
else 
{ 
echo 'Error! SQL query failed:'; 
echo "<pre>{$dbLink->error}</pre>"; 
} 

这里获取图像信息的代码是什么想实现

{ 
"success": 1, 
"message": "images Available", 
"images": [ 
    { 
     "Name": "richmahnn", 
     "Mime": "ajh544k", 
     "Size": "222", 
     "Created": "232014", 
     "url": "http://localhost/imageUpload/ajh544k.jpg" 
    }, 
    { 
     "Name": "john", 
     "Mime": "ajh5644k", 
     "Size": "15", 
     "Created": "232014", 
     "url": "http://localhost/imageUpload/ajh5644k.jpg" 
    }, 

到目前为止,这是我所得到的 enter image description here

+0

你有没有尝试过任何的JSON转换?如果是这样,请显示你的尝试和具体出错。 PHP的['json_encode()'](http://php.net/manual/en/function.json-encode.php)可能会有帮助。 – showdev 2015-01-09 18:46:46

+0

你试过json_encode() 查http://php.net/manual/en/function.json-encode.php – 2015-01-09 18:48:11

回答

1
  1. 使用JSON中想要的属性布局创建一个php数组。
  2. json_encode($ yourarraydata);
  3. ?????
  4. #Win
1

从你说你要需要的JSON数据,那么这似乎是最简单的解决方案。

基本上使用stdClass()创建一个新的类,它满足您的要求,即JSON数据是一个对象。 然后更改while循环以将结果行作为对象而不是assoc数组返回,然后可以将它放入新类的images数组属性中,每个返回的行都有一个。

现在,您拥有一个PHP类中的所有数据,只需使用json_encode()将其转换为JSON字符串即可。其结果可以作为返回的数据回显出来。

我假设你用来封装数据库访问代码的类有一个方法,它将以对象形式以及assoc数组的形式返回行。

// Query for a list of all existing files 
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`'; 
$result = $dbLink->query($sql); 

$toJson = new stdClass(); 

// Check if it was successfull 
if($result) { 
    // Make sure there are some files in there 
    if($result->num_rows == 0) { 
     echo '<p>There are no files in the database</p>'; 
     toJson->success = 0; 
     toJson->message = 'There are no files in the database'; 

    } else { 
     // Print the top of a table 
     echo '<table width="100%"> 
       <tr> 
       <td><b>Name</b></td> 
       <td><b>Mime</b></td> 
       <td><b>Size (bytes)</b></td> 
       <td><b>Created</b></td> 
       <td><b>&nbsp;</b></td> 
       </tr>'; 

     // Print each file 
     while($row = $result->fetch_object()) { 
      echo "<tr> 
        <td>{$row->name}</td> 
        <td>{$row->mime}</td> 
        <td>{$row->size}</td> 
        <td>{$row->created}</td> 
        <td><a href='get_file.php?id={$row->id}'>Download</a></td> 
        </tr>"; 

      // amend and existing properties 
      $row->name = 'http://' 
         . $_SERVER['HTTP_HOST'] 
         . '/imageUpload/' 
         . $row->name; 

      // or alternatively add a new property 
      $row->url = 'http://' 
         . $_SERVER['HTTP_HOST'] 
         . '/imageUpload/' 
         . $row->name; 

      $toJson->images[] = $row; 
     } 

     // Close table 
     echo '</table>'; 
     $toJson->message = 'images Available'; 
     $toJson->imageCount = $result->num_rows; 
    } 

    // Free the result 
    $result->free(); 
} else { 
    toJson->success = 0; 
    toJson->message ='Error! SQL query failed:'; 
    echo 'Error! SQL query failed:'; 
    echo "<pre>{$dbLink->error}</pre>"; 
} 

如果您正在使用您的网页AJAX调用调用这个,然后返回数据只是做

echo json_encode($toJson); 
exit; 

如果你正在做别的事情与得到的JSON字符串,然后就去做这

$json_string = json_encode($toJson); 
+0

谢谢里格斯,让我试试吧 – Richmahnn 2015-01-09 21:17:22

+0

json现在正在显示,但是如何让它显示以“name”形式显示图像的完整url:“http://localhost/imageUpload/3fYUZH2OfZY.jpg”而不是“name”:“3fYUZH2OfZY.jpg” – Richmahnn 2015-01-10 08:05:22

+0

查看我的回答了解其他信息。基本上,你可以修改'name'属性或添加一个新的属性,即'url',然后将其添加到图像数组中。 – RiggsFolly 2015-01-10 15:05:09

相关问题