Newbie: How to universalize a mysql=>xml script

From: Date: Thu, 25 Aug 2005 02:07:24 +0000
Subject: Newbie: How to universalize a mysql=>xml script
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-221307@lists.php.net to get a copy of this message
Hi I am try to create a generic function that takes any mysql found set and creates and custom tiered xml list The below works but seems a bit kludgy :( Code synopsis: If we are are on a new row and the first value in that row , <artist>, is different...then do something Is there a cleaner and more universal way to do this ? I am attempting to take my php skills to the next level by making my code more generic and flexible. Ultimately, I would like to feed it a couple of variables and generate an xml list with any number of levels ... FYI, I know the xml is not formed properly....is just for dummy purposes :) I am a bit new to all of this so any help is appreciated :) many thanks g The below works and generates the dummy output: <artist>Akwid</artist> <trackName>Jamas Imagine</trackName> <id>38</id> <trackName>No Hay Manera</trackName> <id>42</id> <artist>Azteka</artist> <trackName>Mexicano Por Fortuna</trackName> <id>41</id> <trackName>Sueno Americano</trackName> <id>33</id> ... // PHP code $result = mysql_query($sql); $numRows = mysql_num_rows($result); $numFields = mysql_numfields($result); $i = 0; //use to determing the first row //get all the keys in the found set for ($x=0; $x<$numFields; $x++) { $keyNames[]= mysql_field_name($result, $x); } while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { foreach($line as $key => $val) { //are we on the first key in the found set ? if($key == $keyNames[0]){ // Are we on row 0 or is $val, <artist>, a new value if($i = 0 OR $val !== $firstVal) { echo "<$key>$val</$key>"."\n"; $firstVal = $val; } // We are not on the first key...so add a tab }else{ echo "\t"."<$key>$val</$key>"."\n"; }
                $i++; //used for checking for first row
                }
                }
               


« previous php.general (#221307) next »