2012-03-28 21 views
0

Derzeit habe ich eine Indexseite, die Werte von einer XML-Seite mit SimpleXML zeigt. Und eine Suchfunktion, die zu einer anderen Seite druckt, was ich tun möchte, ist, die Titelseite zu aktualisieren, um nur die Suchergebnisse anzuzeigen, wenn die Schaltfläche geklickt wird.Suche mit XPath und PHP

Indexseite.

<?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>"; 
    ?> 

Ich habe dann meine searchProcessDescription.php Seite

<?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"; 
?> 

Gibt es eine einfache Möglichkeit, diesen Prozess zu der Index-Seite und für die Seite hinzufügen zu aktualisieren, wenn die Suchtaste geklickt wird?

Dank

Antwort

2

Ja, es ist, Sie eine andere Variable zu Ihrem Formular hinzufügen, könnte die Funktion, die Sie haben zu zeigen, so etwas zu überprüfen:

<?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 --> 

Ich hoffe, dass diese nützlich!

+0

Es gibt eine Reihe von Fehlern i mit erfüllt bin: Hinweis: Undefined variable: Urlaub auf der Linie 65 Hinweis: on line Der Versuch, erhalten Eigenschaft Nicht-Objekt 65 Hinweis: Der Versuch, Eigentum nicht zu bekommen -object on line 65 Warning: Invalid argument für foreach geliefert() in Zeile 65 Hinweis: Undefined variable: action on line 77 –

+0

Sorry, Sie hier ändern sollen:

Die anderen Fehler sind seltsam, alle Variablen scheinen inizialisiert zu sein, und die Zeilennummern sind nicht korrekt: mein ganzes Skript hat 59 Zeilen! – Gian

+0

Ich habe gefunden ... bitte überprüfen Sie die Variable $ holidayDoc und $ holiday, ich modifiziere den angehängten Code, sorry – Gian