Hey,
Ik ben bezig met een webshop affiliatie site maar nu probeer ik de merken van de producten in te laden via een XML bestand. Alleen is het in de xml dat er merken dubbel staan, dus elk product heeft een merk en die laadt hij dan ook in.
Weet iemand hoe ik gewoon een merk maar 1 keer in de database kan zetten?
Dus niet dat hij steeds dubbel komt te staan.
Code
PHP
<?php
include '../includes/config.php';
// Load the XML data from the specified file name.
// The second argument (NULL) allows us to specify additional libxml parameters,
// we don't need this so we'll leave it as NULL. The third argument however is
// important as it informs simplexml to handle the first parameter as a file name
// rather than a XML formatted string.
$pFile = new SimpleXMLElement('http://hardsoftware.nl/xml/elektrokoopjes.xml', null, true);
// Now that we've loaded our XML file we can begin to parse it.
// We know that a channel element should be available within the,
// document so we begin by looping through each channel
foreach ($pFile->item as $pChild)
{
mysql_query("INSERT INTO
merken
(
merk_naam
) VALUES (
'".$core->mysql->escape(stripslashes($pChild->brand))."'
)") or die(mysql_error());
$merk = $core->mysql->query("SELECT
*
FROM
merken
WHERE
merk_naam = '".$core->mysql->escape(stripslashes($pChild->brand))."'");
$merkf = $core->mysql->fetch($merk);
mysql_query("INSERT INTO
producten
(
naam,
link,
beschrijving,
prijs,
cat,
afbeelding_groot,
daisycon_id,
subcat,
subsubcat,
merk_product
) VALUES (
'".$core->mysql->escape(stripslashes($pChild->title))."',
'".$core->mysql->escape(stripslashes($pChild->link))."',
'".$core->mysql->escape(stripslashes($pChild->description))."',
'".$pChild->minimum_price."',
'".$core->mysql->escape(stripslashes($pChild->category))."',
'".$pChild->img_medium."',
'".$pChild->daisycon_unique_id."',
'".$core->mysql->escape(stripslashes($pChild->sub_sub_category))."',
'".$core->mysql->escape(stripslashes($pChild->sub_category))."',
'".$merkf['id']."'
)") or die(mysql_error());
// Print our channel specific information, this should be
// easy to understand, basically we're grabbing the
// title, descripting and link nodes and outputting their values
echo "<h1>" . $pChild->title . "</h1>\n";
echo "IMG: ".$pChild->category." en sub ".$pChild->sub_category."<p>\n";
echo $pChild->description . "<br />\n";
printf('Visit us at <a href="%s">%s</a><br />' . "\n", $pChild->link, $pChild->link);
echo "</p>\n";
// Now we want to loop through the items inside this channel
foreach ($pFile->general->item as $pItem)
{
echo "<p>\n";
// If this item has child nodes as it should,
// loop through them and print out the data
foreach ($pItem->general() as $pChild)
{
// We can check the name of this node using the getName() method.
// We can then use this information, to, for example, embolden
// the title or format a link
switch ($pChild->getName())
{
case 'title':
echo "<b>$pChild</b><br />\n";
break;
case 'link':
printf('<a href="%s>%s</a><br />' . "\n", $pChild, $pChild);
break;
default:
echo nl2br($pChild) . "<br />\n";
break;
}
}
echo "</p>\n";
}
}
?>
Toon Meer
Gegroet