2012-03-28 2 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 on line 경고 : 65 행의 foreach()에 잘못된 인수가 제공되었습니다. 알림 : 정의되지 않은 변수 : 77 행의 작업 –

+0

죄송합니다. 여기서 수정해야합니다 :

다른 오류는 이상합니다. 모든 변수가 초기화되지 않았으며 줄 번호가 올바르지 않습니다. 모든 스크립트에 59 줄이 있습니다! – Gian

+0

찾았습니다 ... 변수 $ holidayDoc 및 $ holiday를 확인하십시오. 첨부 된 코드를 수정합니다. 죄송합니다. – Gian