1

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;  
}
}
?>
6
  • 1
    if (condition) { return true; } else { return false; } Ugh... Commented Jul 27, 2011 at 18:55
  • 1
    Why not just return condition? In your case, return pathinfo($p,PATHINFO_EXTENSION) == 'xml' would suffice. Commented Jul 27, 2011 at 18:58
  • Ahh, very nice. I like that much better! Thanks for the heads up ;) Commented Jul 27, 2011 at 18:59
  • How does everything else look? Commented Jul 27, 2011 at 18:59
  • from your code I understand that: from student 1 you want the tag set1score, from student2, the tag set2score etc. Is that correct? Commented Jul 27, 2011 at 19:01

3 Answers 3

3

Horrible horrible design. why not...

<score num="1">
<score num="2">
etc...

Such a reference would only be necessary if you need to access a specific score anyways. if you're simply using XML as a temporary storage medium for transport, then even a simple <score> would do, since you'd just iterate/extract/destroy anyways.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Marc, very true after seeing what you have I am an idiot! HAHA! That would make my life much easier :)
Mike, listen to this advice. The hard part while coding is learning how to make things easy! Harol tried to explain this for ages! ;)
Man I wish that would sink in! I seem to make things waaaaay too hard for me, but I am learning :)
@Marc - After working with the XML file the reason I have each scored numbered is for when it get's imported into Micrsoft Excel. It will display the node as a header for easy input.
0

Try this: $output .= "<td>" . $v->'set'.$count.'score' . "</td>";

2 Comments

I think it's supposed to be double quotes. Sorry Mike
It's okay Jag, I tried it with double quotes and had no luck. Thanks for the advice :)
0

Create a dynamic variable:

$setCount = 'set' .$count. 'score';
$$setCount = $score;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.