AJAX ライブ検索を使用して、ユーザー プロファイル固有のリンクを生成しています。それはうまく機能します。私は常に希望するプロファイルに行き着きますが、問題があります。
これをユーザー 1 (username = testuser; user_id = 1; blogname = testblog) に対して行います。「test」で検索すると、testuser のプロフィールへのリンクと testuser のブログへのリンクの両方が表示されます。奇妙なことに、リンクは次のように機能します。
profile.php?user=1&page=プロフィール
profile.php?user=1&page=ブログ
しかし、実際のリンクは次のようになります。
profile.php?user=%20+%201%20+%20&page=プロファイル
profile.php?user=%20+%201%20+%20&page=ブログ
私は目的のページにたどり着いたので、それは問題ではないと言うことができますが、私は $GET_['user'] 値が常に実数である必要があるため、そのようなものではありませんここで対処します。
これを修正する簡単な方法があることを願っています。nodeValue->string などのように。私が思うコードのこの部分で nodeValue を変更する必要があります: $z->item(0)->childNodes->item(0)->nodeValue
これは私が使用しているコードです:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//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('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
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='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
XMLfile内の構造は次のようになります。
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>