我使用Wookmark插件,并希望能够使用我的数据库来输出内容。当前的格式是PHP数组,如下所示:
$data = array(
array(
'id' => "1",
'title' => "First image",
'url' => "http://www.example.org/1",
'width' => "560",
'height' => "560",
'image' => "",
'preview' => ""
),
array(
'id' => "2",
'title' => "Second image",
'url' => "http://www.example.org/1",
'width' => "560",
'height' => "560",
'image' => "",
'preview' => ""
)
);我试过用一些代码测试,但在这个领域没有太多的经验。我试着在Stackoverflow上搜索,但没有成功。无论如何,这就是我尝试使用MySQLI的原因(然而,它可能是完全错误的)。
$sth = mysqli_query($con, "SELECT * from entries");
$column = array();
while($row = mysqli_fetch_array($sth)){
$column[] = $row[$key];
}有什么解决方案吗?
发布于 2014-09-20 13:17:54
您可以将图像数据保存在MySQL数据库中,并使用Wookmark-jQuery插件以Wookmark样式在HTML页面上显示它们。可以通过使用Wookmark JSON API下载JSON数据来构建示例数据库。
导入到数据库
CREATE TABLE images (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
referer VARCHAR(255),
url VARCHAR(255) NOT NULL,
width SMALLINT UNSIGNED NOT NULL,
height SMALLINT UNSIGNED NOT NULL,
image VARCHAR(255),
preview VARCHAR(255)
);用于从Wookmark下载和导入图像数据的快速PHP脚本
<?PHP
//### Import Wookmark's popular images to database ###//
$imgs = json_decode(file_get_contents('http://www.wookmark.com/api/json/popular'), true);
$SQL = "INSERT INTO images VALUES(:id, :title, :referer, :url, :width, :height, :image, :preview)";
$db = new PDO("mysql:host=localhost;dbname=imgbase", "username", "password");
$stmnt = $db->prepare($SQL);
foreach($imgs as $img){
$stmnt->execute($img);
}
exit("Done!");
?>从数据库返回JSON数据
这个简单的PHP脚本可用于从数据库表返回JSON数据
popular.php
<?PHP
$per_page = 10;
$page = 1;
if(isset($_GET['per_page'])){
$per_page = (int) $_GET['per_page'];
if($per_page < 10) $per_page = 10;
if($per_page > 50) $per_page = 50;
}
if(isset($_GET['page'])){
$page = (int) $_GET['page'];
if($page < 1) $page = 1;
}
$SQL = "SELECT * FROM images LIMIT ".(($page-1)*$per_page).",".$per_page;
$db = new PDO("mysql:host=localhost;dbname=imgbase", "username", "password");
$imgdata = "(".json_encode($db->query($SQL)->fetchAll(PDO::FETCH_ASSOC)).")";
header("Content-Type: application/json");
echo isset($_GET['callback'])?$_GET['callback'].$imgdata:$imgdata;
?>以Wookmark样式显示图像
使用Wookmark-jQuery plugin。在本例中,除了jQuery之外,只需要以下文件:
编辑example-api/index.html文件,将apiURL从http://www.wookmark.com/api/json/popular更改为您的popular.php页面的example-api/index.html。
然后导航到index.html,您将看到以与wookmark.com相同的样式排列的图像
https://stackoverflow.com/questions/25941911
复制相似问题