I have a script that pulls out XML information then puts it into JSON format. I am trying to change the variable name as I put everything into a table for vewing after the json is decoded.
For instance, there are eleven scores that will be listed and rather than write out all eleven I wanted to dynamically change the name as the loops happens. The problem line I am having is in the studentInformation.php file, I put a comment next to it to make it easier to spot. Hopefully I have explained my issue well enough :P
Nodes in xml file (yeah it's only the first tag, but you get the idea)
<set1score>
<set2score>
<set3score>
<set4score>
<set5score>
<set6score>
<set7score>
<set8score>
<set9score>
<set10score>
<set11score>
xmlParse.php
<?php
class xmlParse
{
private $xmlFile;
public function __construct($xmlFile)
{
$this->xmlFile = $xmlFile;
}
private function xmlVerify($p)
{
return pathinfo($p,PATHINFO_EXTENSION)=='xml';
}
public function encodeJSON()
{
if($this->xmlVerify($this->xmlFile))
{
$xml = simplexml_load_file($this->xmlFile);
return json_encode($xml->children());
}
else
{
return ('<script>alert(\'error with xml file validation\');</script>');
}
}
}
?>
studentInformation.php
<?php
class studentInformation extends xmlParse
{
private $xmlFile;
public function __construct($xmlFile)
{
parent::__construct($xmlFile);
}
private function decodeJSON($i)
{
$j = json_decode($i);
return $j;
}
public function getStudentInformation()
{
$output = '';
$count = 1;
foreach($this->decodeJSON($this->encodeJSON($this->xmlFile))->student as $v)
{
$output .= "<tr>";
$output .= "<td>" . $v->lname . "</td>";
$output .= "<td>" . $v->fname . "</td>";
$output .= "<td>" . $v->set($count)score . "</td>"; //PROBLEM HERE!!!!!
$output .= "</tr>";
$count++;
}
return $output;
}
}
?>
if (condition) { return true; } else { return false; }Ugh...return condition? In your case,return pathinfo($p,PATHINFO_EXTENSION) == 'xml'would suffice.