/    Sign up×
Community /Pin to ProfileBookmark

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>”;
}

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsApr 14.2014 — did you test this before you posted it? i see 3 errors at a glance...[code=php]<?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>';
}
}
?>[/code]
Copy linkTweet thisAlerts:
@shaaninoauthorApr 14.2014 — yaa..it returns all the records. but i want to find only the record from the text box
×

Success!

Help @shaanino spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...