2012-03-28 20 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のオブジェクト 警告:行65のforeach()に無効な引数が指定されています 通知:未定義の変数:行77のアクション –

+0

申し訳ありません、ここで修正する必要があります:

他のエラーは奇妙で、すべての変数は初期化されていないようですが、行番号は正しくありません。 – Gian

+0

私は変数を確認してください$ holidayDocと$ holiday、私は、申し訳ありませんが添付コードを変更します – Gian