http://www.hogwartscastle
Berichten: 217
avatar
Offline Stuur privebericht
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
Code | Selecteer Alles
minimaliseren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?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'nulltrue);
// 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 "n";
    
printf('Visit us at <a href="%s">%s</a>' "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>n";
                    break;
                    
                case 
'link':
                    
printf('<a href="%s>%s</a>' "n"$pChild$pChild);
                    break;
                    
                default:
                    echo 
nl2br($pChild) . "n";
                    break;
            }
        }
        
        echo 
"</p>n";
    }
}

?>


Gegroet
10-12-2010 22:12
Dit topic is 210 keer bekeken door 40 verschillende leden
Reacties op: "PHP row"
1
Vreemd
Berichten: 1246
avatar
Offline Stuur privébericht
Door een UNIQUE CONSTRAINT op het veld merk_naam in de tabel merken?
10-12-2010 22:33
http://www.hogwartscastle
Berichten: 217
avatar
Offline Stuur privébericht
Dat lukt, maar nu als ik me xml import dan voegt hij maaar 3 producten toe en ook 3 merken.

En ik krijg ook dit te zien: Duplicate entry 'Aeg' for key 'CONSTRANT'
11-12-2010 10:06
Vreemd
Berichten: 1246
avatar
Offline Stuur privébericht
Ik kan op het moment twee dingen bedenken:
1. haal de or die weg
2. gebruik de INSERT INTO ... ON DUPLICATE KEY UPDATE syntax
11-12-2010 10:52
http://www.hogwartscastle
Berichten: 217
avatar
Offline Stuur privébericht
Jij bent de beste Tien maal dank
11-12-2010 13:25
http://www.hogwartscastle
Berichten: 217
avatar
Offline Stuur privébericht
Ja graag
11-12-2010 13:39
Eigenaar DiMoWeb
Berichten: 2399
avatar
Offline Stuur privébericht
En als je geen DB check wil doen, kan je gewoon de merken in een array steken en dan nadien met de in_array functie kijken of het merk al in de array zit of niet.
Of het bv opslagen in een SESSION.
11-12-2010 14:25
http://www.hogwartscastle
Berichten: 217
avatar
Offline Stuur privébericht
Ik heb nog een vraag, hij doet soms een beetje raar.
In me producten tabel dan vult hij de category en zo wel maar soms vult hij de eerste 4 producten niet, dus die geeft hij geen category.

Screen: http://i54.tinypic.com/104qonn.png

Code

Code | Selecteer Alles
minimaliseren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?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'nulltrue);
// 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)
{    
        
    
$merk $core->mysql->query("SELECT
                                        *
                                FROM
                                    merken
                                WHERE
                                    merk_naam = '"
.$core->mysql->escape(stripslashes($pChild->brand))."'");
    
$merkf $core->mysql->fetch($merk);
    
      
$rubriek $core->mysql->query("SELECT
                                            *
                                        FROM
                                            rubriek
                                        WHERE
                                            naam = '"
.$core->mysql->escape(stripslashes($pChild->category))."'");
    
$rubriekFetch $core->mysql->fetch($rubriek);
    
    
$subsubrubriek $core->mysql->query("SELECT
                                            *
                                        FROM
                                            subsubrubrieken
                                        WHERE
                                            subsub_omschrijving = '"
.$core->mysql->escape(stripslashes($pChild->sub_sub_category))."'");
    
$subsubrubriekFetch $core->mysql->fetch($subsubrubriek);
    
    
$subrubriek $core->mysql->query("SELECT
                                            *
                                        FROM
                                            subrubrieken
                                        WHERE
                                            sub_omschrijving = '"
.$core->mysql->escape(stripslashes($pChild->sub_category))."'") or die(mysql_error());
    
$subrubriekFetch $core->mysql->fetch($subrubriek);
    
    
// Merk
    
mysql_query("INSERT INTO
                       merken
                    (
                        merk_naam
                    ) VALUES (
                        '"
.$core->mysql->escape(stripslashes($pChild->brand))."'                                                                 
                    )
                        ON DUPLICATE KEY UPDATE merk_naam = '"
.$core->mysql->escape(stripslashes($pChild->brand))."'");
    
    
// Rubriek
    
mysql_query("INSERT INTO
                       rubriek
                    (
                        naam
                    ) VALUES (
                        '"
.$core->mysql->escape(stripslashes($pChild->category))."'                                                                 
                    )
                        ON DUPLICATE KEY UPDATE naam = '"
.$core->mysql->escape(stripslashes($pChild->category))."'");
    
     
mysql_query("INSERT INTO
                       subsubrubrieken
                    (
                        subid,
                        subsub_omschrijving
                    ) VALUES (
                        '"
.$subrubriekFetch['id']."',
                        '"
.$core->mysql->escape(stripslashes($pChild->sub_sub_category))."'                                                                 
                    )
                    ON DUPLICATE KEY UPDATE subsub_omschrijving = '"
.$core->mysql->escape(stripslashes($pChild->sub_sub_category))."'") or die(mysql_error());
    
mysql_query("INSERT INTO
                       subrubrieken
                    (
                        rubriek_id,
                        sub_omschrijving
                    ) VALUES (
                        '"
.$rubriekFetch['id']."',
                        '"
.$core->mysql->escape(stripslashes($pChild->sub_category))."'                                                                 
                    )
                    ON DUPLICATE KEY UPDATE sub_omschrijving = '"
.$core->mysql->escape(stripslashes($pChild->sub_category))."'") or die(mysql_error());

    
    if(
$core->mysql->num_rows($merk) >= 0)
    {    
                    
        
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."',   
                            '"
.$rubriekFetch['id']."',
                            '"
.$pChild->img_medium."', 
                            '"
.$pChild->daisycon_unique_id."',
                            '"
.$subrubriekFetch['id']."',
                            '"
.$subsubrubriekFetch['id']."',
                            '"
.$merkf['id']."'                                                                                 
                        )"
);
    }
 
                
    
// 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 "n";
    
printf('Visit us at <a href="%s">%s</a>' "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>n";
                    break;
                    
                case 
'link':
                    
printf('<a href="%s>%s</a>' "n"$pChild$pChild);
                    break;
                    
                default:
                    echo 
nl2br($pChild) . "n";
                    break;
            }
        }
        
        echo 
"</p>n";
    }
}

?>
11-12-2010 14:52
Reageer op: "PHP row"
1
Je kan niet reageren omdat je niet bent ingelogd. Inloggen of Aanmelden