在ajax livesearch中显示图像缩略图

时间:2014-08-26 03:55:57

标签: php ajax xml image livesearch

所以我会直接进入它。我在编写显示我/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代码

感谢。

1 个答案:

答案 0 :(得分:0)

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("one.xml");

$x=$xmlDoc->getElementsByTagName('link');

//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('title');
    $z=$x->item($i)->getElementsByTagName('url');
    $w=$x->item($i)->getElementsByTagName('image');
    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="<img src='" . $w->item(0)->childNodes->item(0)->nodeValue . "' . width='5%'>" . "<a href='" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . "</a>";

        } else {

          $hint=$hint . "<br><img src='" . $w->item(0)->childNodes->item(0)->nodeValue . "'. width='5%'>" . "<a href='" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . "</a>";


        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?>

我不擅长PHP,但有些我是如何运作的。我也从w3schools获得了实时搜索代码。只需将(图像URL)添加到xml文件并编辑PHP代码就可以了。 我希望这可以帮助你 祝你好运=)