You are here:  » Increment var


Increment var

Submitted by antitrust56 on Tue, 2008-12-09 16:58 in

Hello,

I have a problem. I parse a XML feed with success but i have small problem to add 1 on a field of my table.
My var $dbprodid begin at 5 in my database and I want 1,2,3,4 in field on each loop.

<?php
$nom_marchand = $_POST['nom'];
$texte = $_POST['texte'];
$devise = $_POST['devise'];
$rien = $_POST['key'];
$id_marchand = $_POST['key1'];
$cat_prod = $_POST['key2'];
$nom_prod = $_POST['key3'];
$marque_prod = $_POST['key4'];
$desc_prod = $_POST['key5'];
$lien_prod = $_POST['key6'];
$img_prod = $_POST['key7'];
$small_img = $_POST['key8'];
$extra_field1 = $_POST['key9'];
$extra_field2 = $_POST['key10'];
$extra_field3 = $_POST['key11'];
$prix_prod = $_POST['key12'];
$dbprodid = 1;
require("MagicParser.php");
function myRecordHandler($record) {
global $nom_marchand;
global $id_marchand;
global $cat_prod;
global $nom_prod;
global $marque_prod;
global $desc_prod;
global $texte;
global $lien_prod;
global $img_prod;
global $prix_prod;
global $devise;
global $extra_field1;
global $extra_field2;
global $extra_field3;
global $small_img;
global $dbprodid;
$dbprodid = $dbprodid + 1;
$sql = "INSERT INTO affiliSt_product (
merchant,
merchantProdID,
prodCategory,
prodName,
prodBrand,
prodDescription,
prodPromoText,
prodLink,
prodImageURL,
prodPrice,
prodCurrency,
extraFieldA,
extraFieldB,
extraFieldC,
prodImageSmall,
dbProdID
) VALUES (
'$nom_marchand',
'$record[$id_marchand]',
'$record[$cat_prod]',
'$record[$nom_prod]',
'$record[$marque_prod]',
'$record[$desc_prod]',
'$texte',
'$record[$lien_prod]',
'$record[$img_prod]',
'$record[$prix_prod]',
'$devise',
'$record[$extra_field1]',
'$record[$extra_field2]',
'$record[$extra_field3]',
'$record[$small_img]',
'$dbprodid'
)";
mysql_query($sql);
}
MagicParser_parse("http://www.florajet.com/export/catalogue_xml_perez_468.txt","myRecordHandler","xml|PRODUCTFEED/PRODUCT/");
echo "L'insertion du flux à bien été effectué";
?>

Thanks,

José

Submitted by support on Tue, 2008-12-09 17:25

Hello José,

The first problem I can see, is it will only ever start from at least 2.

This is because you set $dbprodid = 1; at the beginning, and then you
are adding 1 BEFORE the database query, so the first one is always 2.

But then you say it starts at 5. This could be because the first 4
records do not import - the SQL might be failing.

To add 1 only if the SQL works; and also so it starts from 1, first of
all DELETE this line from your code:

 $dbprodid = $dbprodid + 1;

...and then change:

 mysql_query($sql);

...to:

 if (mysql_query($sql)) $dbprodid = $dbprodid + 1;

Hope this helps!
Cheers,
David.

Submitted by antitrust56 on Wed, 2008-12-10 15:23

I works perfectly.

You are a good developper.