所以我会直接进入它。我很难编写一种方法来显示保存在我的/images/
文件夹中的图像的缩略图,这个困难源于我使用 ajax livesearch 来提供我的 data.xml 文件的内容。
当我将图像上传到我/images/
的文件夹时,会创建一个 xml 文件,将每个图像的路径保存为 xml 格式,然后 ajax livesearch 在 div 中提供结果,即图像路径和文件名。
我似乎无法编写一种方法来在 xml 文件生成的结果旁边显示缩略图。
这是我的名为 data.xml 的 xml 文件的外观:
<images>
<image>
<path>images/blue.jpg</path>
</image>
</images>
这是我的home.php
页面和我目前使用的所有代码:
<!-- php code for grabbing image data and placing it into data.xml -->
<?php
$path_to_image_dir = 'images'; // relative path to your image directory
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<images>
</images>
XML;
$xml_generator = new SimpleXMLElement($xml_string);
if ( $handle = opendir( $path_to_image_dir ) )
{
while (false !== ($file = readdir($handle)))
{
if ( is_file($path_to_image_dir.'/'.$file) )
{
list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
$image = $xml_generator->addChild('image');
$image->addChild('path', $path_to_image_dir.'/'.$file);
$image->addChild('height', $height);
$image->addChild('width', $width);
}
}
closedir($handle);
}
$file = fopen('data.xml','w');
fwrite($file, $xml_generator->asXML());
fclose($file);?>
<!-- php code for initiating instant search of xml data file -->
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("data.xml");
$x=$xmlDoc->getElementsByTagName('image'); //used to be link, now image, could be images
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('path');
$z=$x->item($i)->getElementsByTagName('height');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<div>
<strong>".$y->item(0)->childNodes->item(0)->nodeValue." ".$z->item(0)->childNodes->item(0)->nodeValue."</strong>
</div>";
}
else
{
$hint=$hint ."<div>
<strong>".$y->item(0)->childNodes->item(0)->nodeValue." ".$z->item(0)->childNodes->item(0)->nodeValue."</strong>
</div>";
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="") {
$response="";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
<!--code for fetching image thumbnail and displaying it-->
<html>
<head>
<script>
function showResult(str) {
if (str.length==0) {
document.getElementById("home").innerHTML="";
document.getElementById("home").style.border="0px";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("home").innerHTML=xmlhttp.responseText;
document.getElementById("home").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","home.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="home"></div>
</form>
</body>
</html>
我尝试了这种方法但无济于事:
<?php
$imageID = $_GET['path'];
$xml = simplexml_load_file("data.xml");
$searchedimage = $xml->xpath('[path="'.$imageID.'"]');
foreach($searchedimage as $imageinfo){
foreach ($imageinfo as $imagedetail){
if($imagedetail->getName($image) == 'path'){
echo '<img src="'.$image.'" height="250"; "width="250" ;>';
}
else{
echo $imagedetail->getName(). ": " ;
echo $imagedetail . "<br/>";
}
}
}
?>
我希望看到结果与正确的缩略图配对在一起。上面的代码失败了,如果有人知道原因并且可以提供一个编码示例,我将非常感激。我从http://www.w3schools.com/php/php_ajax_livesearch.asp获得了 ajax livesearch 代码
谢谢。