need help with loops, don't quite grasp it???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

need help with loops, don't quite grasp it???

Post by vincenzobar »

here is my code

Code: Select all

//insert SQL Statement
	$sql = "INSERT INTO CB_rateplan $rows VALUES " . implode ( ', ', $hold ).";";
	$num_rows_rp = mysql_num_rows($results_rp);
	
if(isset($results_rp) && isset($num_rows_rp)){

$rs= mysql_fetch_assoc($results_rp);
echo $rs['productid']."=".$temp[21]."<BR>"; // seeing what i grabs

if( $temp[21] != $rs['productid']){
		$add_rp = "INSERT INTO CB_rateplan $rows VALUES $values WHERE productid !='".$temp[21]."';";
		echo $add_rp;
	}else{
		for($i=0; $i< count($column); $i++){
			$update_rp = "UPDATE CB_rateplan SET ".$column[$i]."='".$temp[$i]."' WHERE productid='". $temp[21]."';";
			echo $update_rp;
		}//for loop 
		
	} //if 2
	echo "Worked";


}else{
  //SQL insert stuff that already works with no problems
	//echo $sql;
	//for calling strings for update and insert statements
	
	//print_r($temp);
	//mysql_query($sql);

	//Stick it in thur biotch!
/*if(mysql_query($sql)){
	print "<BR>info inserted successfully";
}else{
	print "<BR>did not insert";
}*/
}// If end
echo "<BR> VALUE".$temp[21];
What i need to do is match the database with the current xml parse and match arrays. so in the XML array key 21 is the same as productid.

now what i need to do is for every match run an update and for anything that doesn't match i need to insert it as a new row. And it has to filter through all records in XML and the table

example
table:row
productid = 12345, 56789, 11111, 22222
XML: 21
21 = 12345, 56789. 11111, 22222, 33333

so first 4 records woould udate and last record in XML would insert.

i can't quite figure out what to do here... how to set the loops and embedded loops. Anyone got any clues?
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

ok day 2... How can i parse through an array and pull the value i need for every time it occures and then compare it to the database array? I guess that is another way to ask the question above because i need to compare them in order to even start the IF statement.

Thanks
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

vincenzobar wrote:pull the value i need for every time it occures
what exactly does this mean :? I'm having a hard time understanding you...
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

I have an array where value 21 is the product ID. This array comes from a parsed XML sheet that is then inserted into a database.

Now say the next day i need to uplaod a new Database XML Dump. I need to check the database if the product ID already exists from the array of the new XML Parse and if so then update it and if the product ID doesn't exist then i need to add a new record.

So i need to check to see if array is eachle to database for each record in the array.

does that help... see bottom of first post
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

from what I understand, you have a list of ids in XML, which if exist are to be updated with the data in XML array, if not anew record has to be inserted. right :?:
now few clarifications :arrow:
> XML[21] = <list of ids> or <single id>
> what would be updated when a matching id is found.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

> XML[21] = <list of ids> or <single id>
List of all, for instance a XML Dump can have 1-500 records so i need to compare all from XML Dump to Database before proceeding

> what would be updated when a matching id is found.
for now the entire row except product ID and Just those records that have a matching Product ID

So far i am able to parse out the data from the table

can see my progress @ http://www.underwaterdesign.com/xml/output-rate.php.

Code: Select all

while($rs= mysql_fetch_assoc($results_rp)){
//print implode("' ", $rs). "<BR>";
foreach($rs as $p_id){
	echo "p_id: ". $p_id. "<BR>";
}
}
p_id: 50604
p_id: 50607
p_id: 50608
p_id: 50609
p_id: 50610
p_id: 50611
p_id: 50612
p_id: 50613
etc.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

vincenzobar wrote:> XML[21] = <list of ids> or <single id>
List of all, for instance a XML Dump can have 1-500 records so i need to compare all from XML Dump to Database before proceeding
meaning XML[21] = "50604, 50607, 50608, 50609, 50610, 50611, 50612, 50613 ";
is that the way it is?
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

right! after i pull that i then need to compare to Database 'productid'

see link above

p_id is from DB and a_id is from array
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

damn, that link is giving me 404 :evil:

well it might go something like this

Code: Select all

//store XML IDs in an array...
$XML_IDList = explode(',', $XML[21]);

//fetch all the DB IDs in an array to reduce tons of DB query... 
$idrs = mysql_query("SELECT productid FROM CB_rateplan") or die(mysql_error());
while($row = mysql_fetch_row($idrs))
 $DB_IDList[] = $row[0];

//match and update/add as needed... trifle now 
foreach($XML_IDList as $anID)
{
 if(in_array($anID, $DB_IDList))
  mysql_query("UPDATE old data");
 else
  mysql_query("INSERT new data");
}
how 'bout that 8)
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Parse error: parse error, unexpected '}' in /home/vincenzoba/domains/underwaterdesign.com/public_html/xml/output-rate.php on line 134
:|

well, did you try my solution :?:
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

yes

problem i am running into is when i update i need to update for every record in every column
si i go this but this is where i get back to my original question...

Code: Select all

$XML_IDList = explode(',', $temp[21]); 

//fetch all the DB IDs in an array to reduce tons of DB query...  
$idrs = mysql_query("SELECT productid FROM CB_rateplan") or die(mysql_error()); 
while($row = mysql_fetch_row($idrs)) 
$DB_IDList[] = $row[0]; 

//match and update/add as needed... trifle now  
foreach($XML_IDList as $p_id) 
{ 
	if(in_array($p_id, $DB_IDList)){
		$update_rp = "UPDATE CB_rateplan SET ".$column[$i]."='".$temp[$i]."' WHERE productid='". $rs['productid']."';";
		echo $update_rp; 
		//mysql_query($update_rp);
	}else {
 	$add_rp = "INSERT INTO CB_rateplan $rows VALUES $values WHERE productid!='".$rs['productid']."';"; 
  	echo $add_rp;
  	//mysql_query($add_rp);
  	} 
}
Since update can only insert one record at a time so i need to loop(and loop) to SET them all... i think.

$temp is actually the array
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

vincenzobar wrote:problem i am running into is when i update i need to update for every record in every column
what does that mean 8O every record OR every column...
why do you want to update all records or do you mean all the columns in that row... i m really confused now... :?
Last edited by n00b Saibot on Tue Nov 08, 2005 9:26 am, edited 1 time in total.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

and XML_IDList is only pulling one record
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

I want to update every column in every row that matches every Product ID.

so foreach product id in array that matches product id in table

for every column - set every column in row with matching product ids = to it's new values

is that better?
Post Reply