带有xml数据库的ajax搜索框

时间:2017-02-21 23:52:08

标签: php xml

我正在尝试在我的网站上创建一个实时搜索框。我有一个数据库的XML文件。我已经完成了JavaScript部分,但我是PHP的新手,并且我在w3schools上找到了这个代码的一些问题。我的问题是如何修改以下代码,以便我只获取“name”标签的值?

PHP代码:

    BufferedImage getOneFrame() {
        currFrame++;
        //At the 90th frame I capture that frame and save that frame
        if (currFrame == 120) {
            cap.read(mat2Img.mat);

            mat2Img.getImage(mat2Img.mat);

            Imgcodecs.imwrite("firstImage.png", mat2Img.mat);

        }
        cap.read(mat2Img.mat);

        return mat2Img.getImage(mat2Img.mat);
    }

    public void splitChannels() {

        IplImage firstShot = cvLoadImage("firstImage.png");
        //I split the channels so that I can determine the value of the pixel range
        IplImage hsv = IplImage.create( firstShot.width(), firstShot.height(), firstShot.depth(), firstShot.nChannels());
        IplImage hue = IplImage.create( firstShot.width(), firstShot.height(), firstShot.depth(), CV_8UC1 );
        IplImage sat = IplImage.create( firstShot.width(), firstShot.height(), firstShot.depth(), CV_8UC1 );
        IplImage val = IplImage.create( firstShot.width(), firstShot.height(), firstShot.depth(), CV_8UC1 );


        cvSplit( hsv, hue, sat, val, null );
        //How do I get a small range of pixels of my images to determine get their HSV?

    }

XML:

 <?php
 $xmlDoc = new DOMDocument();
 $xmlDoc->load("links.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');
 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="<a href='" . 
 $z->item(0)->childNodes->item(0)->nodeValue . 
 "' target='_blank'>" . 
 $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
 }
 else
 {
 $hint=$hint . "<br /><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;
 ?>

提前感谢您的回答!我真的很感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

以下是您将如何做的示例:

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("links.xml");
$books = $xmlDoc->getElementsByTagName("book");

//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 = "";

    foreach ($books as $book) {
        $name = $book->getElementsByTagName("name")->item(0)->nodeValue;
        $price = $book->getElementsByTagName("price")->item(0)->nodeValue;
        $quantity = $book->getElementsByTagName("quantity")->item(0)->nodeValue;
        echo "$name - $price - $quantity<br/>";
    }

}

这将从每个图书的xml文件中打印出$name$price$quantity