I have a quite large xml file, in the file I have multiple img urls, and I need to fetch each url separat from each other.
the xml looks about this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<transferData version="1.0">
<item id="1266427" itemType="kivihomepagetransfer">
<property name="country_id">
<value>4402</value>
</property>
...
...
...
<property name="image">
<property>
<property name="image_realtyimagetype_id">
<value>paakuva</value>
</property>
<property name="image_itemimagetype_name">
<value>kivirealty-www</value>
</property>
<property name="image_iv_order">
<value>1</value>
</property>
<property name="image_url">
<value>http://kivi.etuovi.com/itemimages/realty/www/209.12286/img22128.jpg</value>
</property>
<property name="image_status">
<value>1</value>
</property>
<property name="image_iv_select_flag">
<value>true</value>
</property>
<property name="image_transfer_id">
<value>1</value>
</property>
</property>
...
...
...
</item>
</transferData>
<img src=".$fields["image_url"]." width='150px'></td></img>
for each picture instead of the IMAGE text><?php
header("Content-Type: text/html; charset=utf-8");
require("MagicParser.php");
$count = 0;
function myRecordHandler($record)
{
$fields = array();
$currentName = "";
foreach($record as $key => $value)
{
if (strpos($key,"NAME"))
{
$currentName = $value;
}
if (strpos($key,"VALUE"))
{
$fields[$currentName] .= $value." ";
}
}
// now we can just print the fields we want
print "<b>".$fields["town"].", ".$fields["quarteroftown"].", ".$fields["realtytype_id"]."</b><br>";
print "".$fields["presentation"]."";
print "<table width='600px' border='0'>";
print "<tr>";
print "<td valign='top' align='right' width='200px'><strong></strong></td>";
print "<td valign='top'><b>".$fields["price"]." euro</b></td></tr>";
print "<td valign='top' align='right' width='200px'><strong>Plats / Sijainti:</strong></td>";
print "<td valign='top'>".$fields["quarteroftown"]."".$fields["town"]."<br>".$fields["street_name"].", ".$fields["postcode"]."".$fields["postarea"]."</td></tr>";
print "<td valign='top' align='right' width='200px'><strong>Typ / Tyyppi:</strong></td>";
print "<td valign='top'>".$fields["extra_realtytype_id"]."</td></tr>";
print "<td valign='top' align='right' width='200px'><strong>Typ / Tyyppi:</strong></td>";
print "<td valign='top'>IMAGE</td></tr>";
print "<td valign='top' align='right' width='200px'><strong>Typ / Tyyppi:</strong></td>";
print "<td valign='top'>IMAGE</td></tr>";
print "</table><br>";
}
$filename = "{link saved}";
if (!MagicParser_parse($filename,"myRecordHandler","xml|TRANSFERDATA/ITEM/"))
{
print MagicParser_getErrorMessage();
}
?>
Anyone got any ideas?
Hi,
The first change I would make, seeing as there are more than 1 image_url properties (I assume you want all images), is to load them into a separate array called $images. Your display code can then loop through $images to display the appropriate image HTML. To do this, firstly where you have this section of code:
$fields = array();
$currentName = "";
foreach($record as $key => $value)
{
if (strpos($key,"NAME"))
{
$currentName = $value;
}
if (strpos($key,"VALUE"))
{
$fields[$currentName] .= $value." ";
}
}
REPLACE that with:
$fields = array();
$images = array();
$currentName = "";
foreach($record as $key => $value)
{
if (strpos($key,"NAME"))
{
$currentName = $value;
}
if (strpos($key,"VALUE"))
{
if ($currentName=="image_url")
{
$images[] = $value;
}
else
{
$fields[$currentName] .= $value." ";
}
}
}
Next, in your display code, where you have this:
print "<td valign='top' align='right' width='200px'><strong>Typ / Tyyppi:</strong></td>";
print "<td valign='top'>IMAGE</td></tr>";
...REPLACE that with:
foreach($images as $image)
{
print "<td valign='top' align='right' width='200px'><strong>Typ / Tyyppi:</strong></td>";
print "<td valign='top'><img src='".$image."' /></td>";
}
Hope this helps!
All the best,
David.