Note that there are some explanatory texts on larger screens.

plurals
  1. POphp how to dynamically change variable name?
    primarykey
    data
    text
    <p>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.</p> <p>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</p> <p>Nodes in xml file (yeah it's only the first tag, but you get the idea)</p> <pre><code>&lt;set1score&gt; &lt;set2score&gt; &lt;set3score&gt; &lt;set4score&gt; &lt;set5score&gt; &lt;set6score&gt; &lt;set7score&gt; &lt;set8score&gt; &lt;set9score&gt; &lt;set10score&gt; &lt;set11score&gt; </code></pre> <p>xmlParse.php</p> <pre><code>&lt;?php class xmlParse { private $xmlFile; public function __construct($xmlFile) { $this-&gt;xmlFile = $xmlFile; } private function xmlVerify($p) { return pathinfo($p,PATHINFO_EXTENSION)=='xml'; } public function encodeJSON() { if($this-&gt;xmlVerify($this-&gt;xmlFile)) { $xml = simplexml_load_file($this-&gt;xmlFile); return json_encode($xml-&gt;children()); } else { return ('&lt;script&gt;alert(\'error with xml file validation\');&lt;/script&gt;'); } } } ?&gt; </code></pre> <p>studentInformation.php</p> <pre><code>&lt;?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-&gt;decodeJSON($this-&gt;encodeJSON($this-&gt;xmlFile))-&gt;student as $v) { $output .= "&lt;tr&gt;"; $output .= "&lt;td&gt;" . $v-&gt;lname . "&lt;/td&gt;"; $output .= "&lt;td&gt;" . $v-&gt;fname . "&lt;/td&gt;"; $output .= "&lt;td&gt;" . $v-&gt;set($count)score . "&lt;/td&gt;"; //PROBLEM HERE!!!!! $output .= "&lt;/tr&gt;"; $count++; } return $output; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload