I’m pretty new to coding and wanted to try and build a twitter app for my website. I got the following code to work, but I know there is a better way of doing it… I just don’t know how (possibly printing out the data into an array and then into a table with a foreach statement?). I’d really appreciate if someone can point me in the right direction. Basically, what I’m looking to do is parse the XML and create variables that I can call/print out automatically instead of having to do it so manually (the way I’m doing it now).
Thanks!
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
<title> Some Title </title>
</head>
<body>
<div class="container">
<div class="row-fluid">
<div class="span2">
<!--Sidebar content-->
<button>Carmelo Anthony</button>
</div>
<div class="span10">
<!--Body content-->
<?php
$xmldata = 'https://twitter.com/statuses/user_timeline/carmeloanthony.xml';
$open = fopen($xmldata, 'r');
$content = stream_get_contents($open);
fclose($open);
$xml = new SimpleXMLElement($content);
?>
<table class="table table-striped">
<tr>
<td> <img src=" <? echo $xml->status[0]->user->profile_image_url; ?>"/> </td>
<td><strong> <? echo $xml->status[0]->user->name; ?></strong> <i>@<? echo $xml->status[0]->user->screen_name; ?></i> <br /> <? echo $xml->status[0]->text; ?></td>
<td><? echo date("M j",strtotime($xml->status[0]->created_at)); ?></td>
</tr>
<tr>
<td> <img src=" <? echo $xml->status[1]->user->profile_image_url; ?>"/> </td>
<td><strong> <? echo $xml->status[1]->user->name; ?></strong> <i>@<? echo $xml->status[1]->user->screen_name; ?></i> <br /> <? echo $xml->status[1]->text; ?></td>
<td><? echo date("M j",strtotime($xml->status[1]->created_at)); ?></td>
</tr>
<tr>
<td> <img src=" <? echo $xml->status[2]->user->profile_image_url; ?>"/> </td>
<td><strong> <? echo $xml->status[2]->user->name; ?></strong> <i>@<? echo $xml->status[2]->user->screen_name; ?></i> <br /> <? echo $xml->status[2]->text; ?></td>
<td><? echo date("M j",strtotime($xml->status[2]->created_at)); ?></td>
</tr>
<tr>
<td> <img src=" <? echo $xml->status[3]->user->profile_image_url; ?>"/> </td>
<td><strong> <? echo $xml->status[3]->user->name; ?></strong> <i>@<? echo $xml->status[3]->user->screen_name; ?></i> <br /> <? echo $xml->status[3]->text; ?></td>
<td><? echo date("M j",strtotime($xml->status[3]->created_at)); ?></td>
</tr>
<tr>
<td> <img src=" <? echo $xml->status[4]->user->profile_image_url; ?>"/> </td>
<td><strong> <? echo $xml->status[4]->user->name; ?></strong> <i>@<? echo $xml->status[4]->user->screen_name; ?></i> <br /> <? echo $xml->status[4]->text; ?></td>
<td><? echo date("M j",strtotime($xml->status[4]->created_at)); ?></td>
</tr>
</table>
</div>
</div>
</body>
</html>
You can use a foreach loop for this:
For reference: http://php.net/manual/en/control-structures.foreach.php
EDIT
For the weird character problem, try decoding the string using : html_entity_decode