www.webdeveloper.com
Results 1 to 3 of 3

Thread: search from an xml file

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Posts
    2

    search from an xml file

    hi, am new to php and xml. i want to know how to make a search using the xml

    this is the xml file:

    <?xml version="1.0"?>
    <students>
    <student>
    <id>001</id>
    <name>shaan</name>
    </student>
    <student><id>002</id><name>shaany</name></student><student><id>002</id><name>shaany</name></student><student><id>003</id><name>alisha</name></student><student><id>003</id><name>alisha</name></student><student><id>004</id><name>ayesha</name></student></students>

    this is the php code

    <?php

    $s_name= $_POST['name'];

    $xml = simplexml_load_file("mydata.xml");

    foreach($root->student->$s_name as $s_name)
    {
    echo "<p>";
    echo "<strong>Name:</strong> ".$s_student->name."<br/>";
    echo "<strong>Id:</strong> ".$s_student->id."<br/>";
    echo "</p>";
    }

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    did you test this before you posted it? i see 3 errors at a glance...
    PHP Code:
    <?php
    $student_name 
    $_POST['name'];

    // NOTE: should have some sort of validation for $_POST['name'] here

    $xml simplexml_load_file('mydata.xml');

    // loop thru each <student> in <students>
    foreach($xml->students as $student)
    {
        
    // case-insensitive comparison
        
    if(!strnatcasecmp($student->name$student_name))
        {
            echo 
    '<p><strong>Name:</strong> ' .$student->name'<br /><strong>Id:</strong> ' .$student->id'</p>';
        }
    }
    ?>

  3. #3
    Join Date
    Apr 2014
    Posts
    2
    yaa..it returns all the records. but i want to find only the record from the text box

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles