2012-03-28 93 views
0

目前我有一個索引頁面,它使用simpleXML顯示來自XML頁面的值。而一個打印到另一個頁面的搜索功能,我想要做的是刷新首頁,以便在點擊按鈕時僅顯示搜索結果。使用XPath和php進行搜索

索引頁。

<?php 
    $action = "searchFunctionDescription.php"; 
?> 
    <form name="search" method="get" action=<?php echo "\"$action"?>"> 
    <input name="txtSearch" type="text" id="txtSearch" size="30"/> 
    <input type="submit" value="Search" /> 
    <?php 
     // load the xml file into a simplexml instance variable 
     $holiday = simplexml_load_file('holidays.xml'); 

     // draw a table and column headers 
     echo "<table border=\"1\">"; 

     // iterate through the item nodes displaying the contents 
     foreach ($holiday->channel->item as $holiday) { 
      echo "<tr><td><a href=\"{$holiday->link}\">{$holiday->title}</a>" . "<br />" . 
      "{$holiday->pubDate}" . "<br />" . 
      "{$holiday->description}</td>" . "<br />" . 
      "</tr>"; 
     } 
     echo "</table>"; 
    ?> 

然後我有我的searchProcessDescription.php頁

<?php 
    // create an instance 
    $holidayDoc = simplexml_load_file('holidays.xml');  

    // Passes txtSearch to current script from searchFormDescription.php 
    $txtSearch = $_GET['txtSearch']; 

    // Informs user of what they have searched 
    echo "Showing Results for <strong>$txtSearch</strong>"; 

    // set the query using the description 
    if (!is_null($txtSearch)) { 
     $qry = "//channel/item[description[contains(text(),\"$txtSearch\")]]"; 
    } 
    else { 
    // blank search entered so all holidays are shown. 
     $qry = "/channel/'ALL'"; 
    } 

    // execute the xpath query 
    $holidays = $holidayDoc->xpath($qry); 

    // now loop through all holidays and entered results into table 
    echo "<table border=\"0\">\n"; 
    foreach ($holidays as $holiday) 
    { 
     echo "<tr>\n"; 
     echo "<td><a href=\"{$holiday->link}\">{$holiday->title}</a></td>"; 
     echo "<td>{$holiday->description}</td>"; 
     echo "<td>{$holiday->pubDate}</td>"; 
     echo "<td><input type=\"checkbox\" name=\"saveCB\" value=\"3\"/></td>"; 
     echo "</tr>\n"; 
    } 
     echo "</table>\n"; 
?> 

有一個簡單的方法來此過程中添加到索引頁面,該頁面被點擊搜索按鈕時刷新?

感謝

回答

2

是的,你可以另一個變量添加到您的形式來檢查,你必須顯示的功能,這樣的事情:

<?php 
// create an instance 
$holidayDoc = simplexml_load_file('holidays.xml'); 

$resultTable = ""; 

switch (@$_POST['action']){ 
    case "Search": 

     $txtSearch = $_POST['txtSearch']; 
    $resultTable .= "Showing Results for <strong>$txtSearch</strong><br />"; 

     // set the query using the description 
     if (!is_null($txtSearch)) { 
      $qry = "//channel/item[description[contains(text(),\"$txtSearch\")]]"; 
     } 
     else { 
     // blank search entered so all holidays are shown. 
      $qry = "/channel/'ALL'"; 
     } 

     // execute the xpath query 
     $holidays = $holidayDoc->xpath($qry); 

     // now loop through all holidays and entered results into table 
     $resultTable .= "<table border=\"0\">\n"; 

     foreach ($holidays as $holiday) 
     { 
      $resultTable .= "<tr>\n"; 
      $resultTable .= "<td><a href=\"{$holiday->link}\">{$holiday->title}</a></td>"; 
      $resultTable .= "<td>{$holiday->description}</td>"; 
      $resultTable .= "<td>{$holiday->pubDate}</td>"; 
      $resultTable .= "<td><input type=\"checkbox\" name=\"saveCB\" value=\"3\"/></td>"; 
      $resultTable .= "</tr>\n"; 
     } 
     $resultTable .= "</table>\n"; 

     break; 

    default: // this means the home page, as is, without the query into XML file 
     $resultTable .= "<table border=\"1\">";// draw a table and column headers 

     // iterate through the item nodes displaying the contents 
     foreach ($holidayDoc->channel->item as $holiday) { 
      $resultTable .= "<tr><td><a href=\"{$holiday->link}\">{$holiday->title}</a>" . "<br />" . 
      "{$holiday->pubDate}" . "<br />" . 
      "{$holiday->description}</td>" . "<br />" . 
      "</tr>"; 
     } 
     $resultTable .= "</table>"; 
    break; 
    } 

?> 

<form name="search" method="POST" action=#"> 
    <input name="txtSearch" type="text" id="txtSearch" size="30"/> 
    <input type="submit" value="Search" name="action" /> 
</form> 

<?=$resultTable ?> <!-- finally show the result table --> 

我希望這有用!

+0

有一些錯誤,我正在會見了: 注意:未定義的變量:節日第65行 注意:試圖讓行非對象的屬性65 注意:試圖讓非財產上線65 -object 警告:未定義變量::行動作77 – 2012-03-28 11:12:10

+0

對不起,則應在此處修改:爲第65行 通知的foreach()提供參數無效 <形式名稱=「搜索」方法=「POST」 action =#「> 其他錯誤是奇怪的,所有變量似乎inizialized,行號不正確:我所有的腳本有59行! – Gian 2012-03-28 13:49:27

+0

我找到了...請檢查變量$ holidayDoc和$ holiday,我修改了附加的代碼,對不起 – Gian 2012-03-28 13:53:58