Thanks for your help,
I thought I had it it sorted but my little script managed to created over 500,000 records in the table !!
Rather than just echoing the data, what I am trying to do is
insert the data into a table, well two tables.
One table contains the product data, the other table contains which
categories the product is in. The product maybe in 3 or 4 categories or sub-categories.
I could not make it work with the site_nodes array, so I was trying to use
the more basic approach:
This is my code:
$xml = simplexml_load_file($file);
$cnt = 0;
foreach ($xml->xpath('/Catalog/Category') as $top_cat) {
foreach ($top_cat->xpath('Site') as $top_site) {
$sql_ck = "SELECT cb_id FROM cb_update WHERE id = '$top_site->Id' AND day_no = '$this_day'";
$result_ck = mysql_query($sql_ck)
or die("could not FIND ID in cb_update.". mysql_error());
$num = mysql_num_rows($result_ck);
if($num == 0) { // - so the product is not yet recorded.
$title = mysql_real_escape_string($top_site->Title);
$descrip = mysql_real_escape_string($top_site->Description);
$sql_ins = "INSERT INTO cb_update ( cb_date, day_no, id, title, descrip, comm )
VALUES
( '$today', '$this_day','$top_site->Id','$title', '$descrip', '$top_site->Commission' )";
$result_ins = mysql_query($sql_ins) or die("could not execute INSERT to clicky.". mysql_error());
// Also insert the category
$sql_ins = "INSERT INTO cb_cat_update (id_cat, cat, sub_cat, pop, day_no_cat)
VALUES ('$top_site->Id', '$top_cat->Name', ' ', '$top_site->PopularityRank', '$this_day')";
$result_ins = mysql_query($sql_ins) or die("could not execute INSERT to cb_cat_update.". mysql_error());
}
else { // So the product IS recorded, this must be another occurance in a different category - so we just record the extra category.
$sql_ins = "INSERT INTO cb_cat_update (id_cat, cat, sub_cat, pop, day_no_cat)
VALUES ('$top_site->Id', '$top_cat->Name', ' ', '$top_site->PopularityRank', '$this_day')";
$result_ins = mysql_query($sql_ins) or die("could not execute INSERT to cl_cat.". mysql_error());
}
}
// Now lets do the usb-categories
foreach ($top_cat->xpath('Category') as $sub_cat) {
foreach ($sub_cat->xpath('Site') as $sub_site) {
$sql_ck = "SELECT cb_id FROM cb_update WHERE id = '$sub_site->Id' AND day_no = '$this_day'";
$result_ck = mysql_query($sql_ck)
or die("could not FIND ID in cb_update.". mysql_error());
$num = mysql_num_rows($result_ck);
if($num == 0) { // - so the product is not yet recorded.
$title = mysql_real_escape_string($sub_site->Title);
$descrip = mysql_real_escape_string($sub_site->Description);
$sql_ins = "INSERT INTO cb_update ( cb_date, day_no, id, title, descrip, comm )
VALUES
( '$today', '$this_day','$sub_site->Id','$title', '$descrip', '$sub_site->Commission' )";
$result_ins = mysql_query($sql_ins) or die("could not execute INSERT to clicky.". mysql_error());
// Also insert the sub-category
$sql_ins = "INSERT INTO cb_cat_update (id_cat, cat, sub_cat, pop, day_no_cat)
VALUES ('$sub_site->Id', '$top_cat->Name', '$sub_cat->Name', '$sub_site->PopularityRank', '$this_day')";
$result_ins = mysql_query($sql_ins) or die("could not execute INSERT to cb_cat_update.". mysql_error());
}
else { // So the product IS recorded, this must be another occurance in a different sub-category - so we just record the extra category and sub-category.
$sql_ins = "INSERT INTO cb_cat_update (id_cat, cat, sub_cat, pop, day_no_cat)
VALUES ('$sub_site->Id', '$top_cat->Name', '$sub_cat->Name', '$sub_site->PopularityRank', '$this_day')";
$result_ins = mysql_query($sql_ins) or die("could not execute INSERT to cl_cat.". mysql_error());
}
$cnt++;
}
}
}
Well, maybe I have got the foreach loops mixed up, it seems logical to me,
but I can not get it working :o
Can anyone see what I have done wrong ?
Thanks