Avatar of teksavers
teksavers

asked on 

Looping through data and loop seems to be ignoring the condition

I have a problem with my code. Here is what is happening.
I am getting back XML data and I am looking for the condition $cond.
It can be USED - NEW - or REF

I have if statements that are in the loop of getting back the XML data  but the if statement ignores the condition it seems.
example:
No record Found for WIC1-DSU-T1, with a Condition of USED. But we found one with the condition of NEW Now we need to insert a new set of records.
It inserts it. But I have the new include files commented out.  the REF code seems to be running.

and it comes to another record where the $cond = NOB and the REF code seems to run again:
No record Found for WIC1-DSU-T1, with a Condition of USED. But we found one with the condition of NOB Now we need to insert a new set of records.

Duplicate entry 'SKU318878-WIC1-DSU-T1-master' for key 2

I know that I am creating SKU's on the fly and they need to be in the loop to get a new number but a NOB should be ignored.
What am I doing wrong here??
<?php
//test Produ1ct
$Prod = "WIC1-DSU-T1";
 
$sUsername = "root";
$sPassword = "";
$sDatabase = "teksavers_test";
 
// Connect to database and select database
$conn = mysql_connect('localhost', $sUsername, $sPassword) or die(mysql_error());
mysql_select_db ($sDatabase) or die("Could not select database" . mysql_error());
 
global $conn;
 
// sku number
$sku = rand(1, 1000000);
 
//
function commasplit ($name)
    {
        $v1 = explode (",", $name) ;
        $name = "" ;
        foreach ($v1 as $t)
        {
            $name .= $t ;
        }
        return $name ;
    }
 
 
 
  //get user agent for Brokerbin
$uagent = $_SERVER['HTTP_USER_AGENT'];
// Retrieve XML
//$url = "http://2.0.ittrader.com/index.php?keycode=jakexml&keyword=$Prod";
$url = "http://2.0.ittrader.com/?key=teks382&part=$Prod&agent=$uagent";
$sxe = new SimpleXMLElement($url, NULL, TRUE);
for ($x=0;$x<count($sxe->item);$x++) {
 
      $part_id = mysql_real_escape_string($sxe->item[$x]->part_id);
      $partkey = mysql_real_escape_string($sxe->item[$x]->partkey);
      $qty = mysql_real_escape_string($sxe->item[$x]->qty);
      $cond = mysql_real_escape_string($sxe->item[$x]->cond);
      $partsno = mysql_real_escape_string($sxe->item[$x]->partsno);
      $description = mysql_real_escape_string($sxe->item[$x]->description);
      $mfg = mysql_real_escape_string($sxe->item[$x]->mfg);
      $country = mysql_real_escape_string($sxe->item[$x]->country);
      $price = mysql_real_escape_string($sxe->item[$x]->price);
 
           $newprice = substr($price, 1); // returns price with no $
          //echo $newprice;
 
        $newprice =commasplit($newprice);
 
        // If Statement to only update the exact product in the system.
 // Looking for a USED Product
  $strSQLExists = "SELECT * FROM `xcart_products` WHERE `product` LIKE '%$Prod%' AND `cond` LIKE '%USED%'";
 //$strSQLExists = "SELECT * FROM `xcart_products` ";
  $rsExists = mysql_query($strSQLExists,$conn);
  $data = mysql_fetch_array($rsExists);
  if($data)
  {
  // Record found update the record using the product.php code.
        echo "There is a record  for <b>$Prod</b>, with Condidtion of <b>USED</b><br />So now lets update it!";
 
  }
        else
          {
          //record NOT found. Create a new record in Xcart
          echo "Hmm.. No record Found for <b>$Prod</b>, with a  Condition of <b>USED</b>. But we found one with the condition of $cond Now we need to insert a new set of records.<br /> <br />";
  //START The if includes
  if ($cond =="NEW") {
        // Do this: check for product of NEW if exists SQL query
        // if the record exists update the record.
//      include("updateNEW.php");
        }
else {
        // Add new records for condition for NEW.
        // include the NEW files to run in the statement
//       include("createNEW.php");
}
 
if ($cond =="REF") {
        // check for product of REF if exists SQL query
        // Update the record.
        include("updateREF.php");
        }
else {
        // Add new records for condition for REF.
        // include the REF files to run in the statement
         include("createREF.php");
}
if ($cond =="NOB") {
        // Do Nothing
 
        }
if ($cond =="CALL") {
        // Do Nothing
 
        }
          }
 
//////////////////////////////////////////////////////////////////////////////////////////////////
 
 
} // END bROKERBIN fOR lOOP
echo "<BR /> End of the File";
?>
 
 
////////// updateNEW.php /////////////////////
<?
// Check for NEW product condition if condition exisits then update the NEW version record of the product
// we need to get the record ID:
 
$strSQLExists = "SELECT * FROM `xcart_products` WHERE `product` LIKE '%$Prod%' AND `cond` LIKE '%NEW%'";
  //$strSQLExists = "SELECT * FROM `xcart_products` ";
  $rsExists = mysql_query($strSQLExists,$conn);
  $data = mysql_fetch_array($rsExists);
  $productid = $data['productid'];
  if($data)
  {
// DO the Product.php type of update
 
}// end the if $data exists
 
if ($newprice=="CALL"){
        $iSQL = "UPDATE xcart_pricing SET price = '1.00', quantity = 1 WHERE productid = $productid";
        $iSQL2 = "UPDATE xcart_products SET list_price = '1.00', avail = $qty, descr = '$Prod -- $description' WHERE productid = $productid";
        $iSQL3 = "UPDATE xcart_products_lng SET descr  = '$Prod -- $description' WHERE productid = $productid";
        $iSQL4 = "UPDATE xcart_extra_field_values SET fieldid ='1', value = '$cond' WHERE productid = $productid";
        // echo "<br>$iSQL<br>";
        $oRs = mysql_query($iSQL) or die(mysql_error());
        $oRs2 = mysql_query($iSQL2) or die(mysql_error());
        $oRs3 = mysql_query($iSQL3) or die(mysql_error());
        $oRs4 = mysql_query($iSQL4) or die(mysql_error());
       // echo "<br>Product is set to USED: <br>$iSQL<br>";
        }
        else {
                $iSQL = "UPDATE xcart_pricing SET price = '$newprice', quantity = 1 WHERE productid = $productid";
        $iSQL2 = "UPDATE xcart_products SET list_price = '$newprice', avail = $qty, descr = '$Prod -- $description' WHERE productid = $productid";
        $iSQL3 = "UPDATE xcart_products_lng SET descr  = '$Prod -- $description' WHERE productid = $productid";
        $iSQL4 = "UPDATE xcart_extra_field_values SET fieldid ='1', value = '$cond' WHERE productid = $productid";
       // echo "<br>$iSQL<br>";
        $oRs = mysql_query($iSQL) or die(mysql_error());
        $oRs2 = mysql_query($iSQL2) or die(mysql_error());
        $oRs3 = mysql_query($iSQL3) or die(mysql_error());
        $oRs4 = mysql_query($iSQL4) or die(mysql_error());
        //echo "<br>Product is set to USED: <br>$iSQL<br>";
        }
 
 
?>
 
 
//////////////////// createNEW.php //////////////////////////////////
<?
$iSQL = "INSERT INTO `teksavers_test`.`xcart_products` (`product`, `productcode`, `list_price`, `descr`, `manufacturerid`, `avail`,`cond`) VALUES ('$Prod', 'SKU$sku-$Prod', '1.00','NEW - $description','1', '$qty','$cond');";
          $oRs = mysql_query($iSQL) or die(mysql_error());
         // echo "SKU-$sku created <br>";
 
          //Step 2: query table for productid based on Created SKU in this loop.
          $query = "SELECT productid FROM `teksavers_test`.`xcart_products` WHERE productcode ='SKU$sku-$Prod'; ";
          $result = mysql_query($query) or die(mysql_error());
      while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $prodid = $row['productid'];
      }
 
      // Step 3: Insert Price into xcart_pricing -- this is where the price is actually kept.
                $iSQL2 = "INSERT INTO `teksavers_test`.`xcart_pricing` (`productid`, `quantity`, `price`) VALUES ('$prodid','1','1.00');";
            $oRs2 = mysql_query($iSQL2) or die(mysql_error());
 
                         // Step 4: Insert into xcart_product_categories / The Categorie is the main one all products. Make sure that is created in Xcart.
                         $iSQL3 = "INSERT INTO `teksavers_test`.`xcart_products_categories` (`categoryid`,`productid`,`main`) VALUES ('1','$prodid','Y');";
                 $oRs3 = mysql_query($iSQL3) or die(mysql_error());
 
                                 // Step 5: Query for pricing ID from xcart_pricing to use in xcart_quick_prices
                                 $query2 = "SELECT * FROM `xcart_pricing` WHERE `productid` ='$prodid';";
                                 $result2 = mysql_query($query2) or die(mysql_error());
                         while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){
                         $PriceId = $row2['priceid'];
                         }
                                 //echo "PriceId= $PriceId <br>";
                                          // Step 6: Insert into xcart_quick_prices
                                          $iSQL4 = "INSERT INTO `teksavers_test`.`xcart_quick_prices` (`productid`,`priceid`) VALUES ('$prodid', '$PriceId');";
  $oRs4 = mysql_query($iSQL4) or die(mysql_error());
 
                                                // Step 7: Insert into xcart_products_lng // this is for the Lang setting to US
                                                $iSQL5 = "INSERT INTO `teksavers_test`.`xcart_products_lng` (`code`,`productid`,`product`,`descr`) VALUES ('US', '$prodid', '$Prod','$cond - $description');";
                                            //echo $iSQL5;
                                    $oRs5 = mysql_query($iSQL5) or die(mysql_error());
 
                                                        // Step 8: Insert into xcart_extra_field_values // this updates the condition field for the product.
                                                $iSQL6 = "INSERT INTO `teksavers_test`.`xcart_extra_field_values` (`productid`,`fieldid`,`value`) VALUES ('$prodid', '1','$cond');";
 
                                    $oRs6 = mysql_query($iSQL6) or die(mysql_error());
                                                                        $mypart = $$Prod;
                                                                        $myqty = $qty;
                                                                        $mycond = $cond;
 echo "<p class=\"smallquery\">Creating...<br> SKU: SKU-$sku <br>$mypart <br>Price: 1.00 <br>Qty: $myqty <br />Condition: $mycond <br /> $iSQL <br> $iSQL2 <br> $iSQL3 <br> $iSQL4 <br> $iSQL5 <br> $iSQL6 </p><hr>";
?>

Open in new window

PHPWeb Languages and Standards

Avatar of undefined
Last Comment
teksavers

8/22/2022 - Mon