代碼如下:
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>0if (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="item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } else { $hint=$hint . "
item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } } } }}// Set output to "no suggestion" if no hint were found// or to the correct valuesif ($hint == "") { $response="no suggestion"; }else { $response=$hint; } //output the responseecho $response;?>
這段代碼中:
if ($hint=="") { $hint="item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } else { $hint=$hint . "
item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; }
問題1:之前不是已經定義$hint=0了,為啥還要if ($hint=="")?
問題2:如題$hint不=0,後面的代碼在執行什嗎?
回複討論(解決方案)
where $hint=0 ?
聲明$hini=“” 是為了儲存用的,第一次迴圈的時候$hini是沒有值的,但是第二次迴圈的時候就有了啊
where $hint=0 ?
$hint=""
是 ,不是0,但是為了方便我說成是0了
$hint="";是在for迴圈之外定義的;在$i = 0的時候$hint是空的字串會進入到if ($hint=="")判斷中 但迴圈加1時也就是$i = 1時此時的$hint已經不再是空的字串;這個判斷就是為了區別第一次迴圈
$hint="";是在for迴圈之外定義的;在$i = 0的時候$hint是空的字串會進入到if ($hint=="")判斷中 但迴圈加1時也就是$i = 1時此時的$hint已經不再是空的字串;這個判斷就是為了區別第一次迴圈
其實可以不用判斷的
這是用來判斷是不是第一行,不是第一行的話前面加
換行
其實列表換行這種事,交給CSS來做比較好,不用加
,也就省去這樣判斷了。