php - Table formatting for xml returned via CURL from API -
i using company's api retrieve information database, being returned me in xml format. i'm trying accomplish 2 things - having couple issues.
1st format raw xml data table format, easier view via browser.
2nd data receiving ids/usernames/passwords/emails. able import data db each userid row inserted db (i can db work, can't figure out how process each user individually )
the api formatting <api> <message> <user> <id> </id> <login> </login> <password> </password> <message> </message> </api>
there hundreds of user's instead of one.
whenever print of $array, data big blob intended. however, when use updated code, (below) receive error user not valid index. receive looks start of table, without data in (only borders).
if me figure out why table not receiving data (or give me advice on better way it) appreciate it.
extra points can me figure out number two.
error notice: undefined index: user in /home/public_html/new/test.php on line 36
line 36 commented in code
here bottom part of code:
curl_setopt($ch, curlopt_returntransfer, 1); // method execution $result = curl_exec($ch); // close curl session $array = json_decode(json_encode((array)simplexml_load_string($result)),1); $array_user=$array['user']; //line 36 $tab='<table border="1" width="400">'; ($j=1; $j< count($array_user) ; $j++) { $tab.='<tr>'; $tab.='<td>'.$array_user[$j]['id'].'</td>'; $tab.='<td>'.$array_user[$j]['login'].'</td>'; $tab.='<td>'.$array_user[$j]['mail'].'</td>'; $tab.='<td>'.$array_user[$j]['date'].'</td>'; $tab.='</tr>'; } $tab.='</table>'; echo $tab; ?>
$sxe = simplexml_load_string('<api><message><user><id>10</id><login>a</login><password>abc</password></user><user><id>11</id><login>456</login><password>def</password></user></message></api>'); // or $sxe = simplexml_load_file($xml_url); function tabulate(simplexmlelement $sxe) { if (!count($sxe)) return ''; $table = "<table border='1' width='400'>\n"; $header = false; foreach($sxe $row) { if (!$header) { $table .= "<thead>\n<tr>\n\t"; foreach ($row $field) { $table .= "<th>"; $table .= htmlspecialchars($field->getname(), ent_noquotes, 'utf-8'); $table .= "</th>"; } $table .= "\n</tr>\n</thead>\n<tbody>\n"; $header = true; } $table .= "<tr>\n\t"; foreach ($row $field) { $table .= "<td>"; $table .= htmlspecialchars((string) $field, ent_noquotes, 'utf-8'); $table .= "</td>"; } $table .= "\n</tr>\n"; } $table .= "</tbody>\n</table>"; return $table; } function insert_users(pdo $db, simplexmlelement $users) { $ins = $db->prepare('insert users (id, login, password) values (?,?,?)'); foreach ($users $user) { $userdata = array((string) $user->id, (string) $user->login, (string) $user->password); $ins->execute($userdata); } } insert_users($db, $sxe->message->user); echo tabulate($sxe->message->user);
Comments
Post a Comment