Loopy Loop - Quick Answer Needed!

Discussion in 'PHP' started by IGiveMoney, Oct 25, 2008.

  1. #1
    Ok so I am working on something that will parse out xml
    and then do an update to a database if a couple of things
    are found.

    BUT - Im trying to add a bit more dynamics to it. For example,
    if you just have say feeds.xml and within the feeds.xml is
    the fields "PARTNUM" and "QTAVAIL" - all works well with
    what I have below.

    But I am trying to make it so that just about any xml can be
    parsed and still fall within the routine of what I am doing.

    Example:
    My products.xml has fields PRODNUM and QUANTITY
    My feeds.xml has fields PARTNUM and QTYAVAIL

    Since there are going to be many xml feeds added to this
    I thought the best way would just to use an array and then
    just add the required fields as you add more xml.

    For instance:

    
    	$prd_array = array("PARTNUM");
    	$qty_array = array("QTYAVAIL");
    
    # After new feed added:
    
    	$prd_array = array("PARTNUM", "PRODNUM");
    	$qty_array = array("QTYAVAIL", "QUANTITY");
    
    PHP:
    Presently, my current code produces something
    like this:

    
    
    DATA -
    PARTNUM - 000011236
    STDPARTNUM - 000011236
    CATDESCRIPT - GPS DEVICES AND ACCESSORIES
    VENDORNAME - LOWRANCE CORPORATION
    PARTDESCRIPT - GPS,I-WAY 500C,IN-CAR GPS NAVIGATION
    QTYAVAIL - 0
    COST - 604.65
    UPC - 042194525389
    MSRP - 799.30
    WEIGHT - 10.5
    
    Code (markup):
    And then it would produce this:

    
    UPDATE products SET products_quantity='0' WHERE products_model = '00100681201'
    Code (markup):
    All of this is PERFECT!!

    The only problem I am having is this...

    When I add more then selection the array it only
    gives me the 'last' option...

    
    	$prd_array = array("PARTNUM", "PRODNUM");
    	$qty_array = array("QTYAVAIL", "QUANTITY");
    
    PHP:
    even after I tried a couple of different ways to loop
    it and still ended up with the same result. I get: PRODNUM.

    Im basically trying to get it so that if

    PRODNUM & QTYAVAIL are the two selections for the
    xml being parsed, then just finish up with the update.

    Or if it is PRODNUM & QUANTITY - move along with updates
    Or if it is PARTNUM & QUANTITY - move along with updates

    and so on...

    So with all that said - here's my present code.
    Im sure fresh eyes will see it right away. I just can't
    seem to get it right.

    NOTE: function myRecordHandler($record) is a loop of it's own
    as it passes through each field within the xml sheet to parse it out.


    
      function myRecordHandler($record)
      {
    
    	$prd_array = array("PARTNUM");
    	$qty_array = array(1 => "QTYAVAIL");
    
    
    	$count_total= count($prd_array);
    	for ($counter=0; $counter<$count_total; $counter++){
    	$prd_key = each ($prd_array);
    	#echo $prd_key[value] . "<br>";
    	}
    
    	$count_total2 = count($qty_array);
    	for ($counter2=0; $counter2<$count_total2; $counter2++){
    	$qty_key = each ($qty_array);
    	#echo $qty_key[value] . "<br>";
    	}
    
    	##############
    	#echo $prd_key[value] . ': ' . $qty_key[value] . '<br>';
    	#Turn the record value into something we can use
    	##############
    
              foreach($record as $key=>$value){
    
    		####################
    		#echo $key . ' - ' . $value . '<br>'; 
    		#echo $key . ' - ' . $qty_key[value] . '<br>';
    		####################
    
    		if($key === $qty_key[value]){
    			$_POST['product_qty'] = $value;
    		}
    
    		if($key === $prd_key[value]){
    			$_POST['product_id'] = $value;
    		}
    
    
    }
    
    	if(isset($_POST['product_qty']) && isset($_POST['product_id'])){
    
    		# Now loop through the $_POST values and make
    		# sure it's not the product_qty...
    		# When we find one that isn't the product_qty
    		# we simply do the update for the product 
    
    
    			if($key != $_POST['product_qty']){
    			//$sql_U = "UPDATE products SET products_quantity='{$temp_qty}' WHERE products_model = '{$_POST['product_id']}' AND products_quantity = '{$products_quantity}'";
    			echo 'If this were live - we would do the following:<br>';
    			echo "UPDATE products SET products_quantity='{$_POST['product_qty']}' WHERE products_model = '{$_POST['product_id']}'<br><br>";
    			//$res_U = mysql_query($sql_U); 
    
    
    			}	
    	}
      }
    
    
    
    
    PHP:
     
    IGiveMoney, Oct 25, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Wow, your giving so much info it's hard to tell where the question lies. . .

    So you want to just do an update if a certain xml node is a certain value, yea? Then, when you make the array multidimensional everything goofs up- only showing the PRODNUM array?
     
    ToddMicheau, Oct 25, 2008 IP