Page 1 of 2
need help with loops, don't quite grasp it???
Posted: Mon Nov 07, 2005 3:43 pm
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?
Posted: Tue Nov 08, 2005 7:42 am
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
Posted: Tue Nov 08, 2005 7:48 am
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...
Posted: Tue Nov 08, 2005 7:59 am
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
Posted: Tue Nov 08, 2005 8:11 am
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
> XML[21] = <list of ids> or <single id>
> what would be updated when a matching id is found.
Posted: Tue Nov 08, 2005 8:19 am
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.
Posted: Tue Nov 08, 2005 8:33 am
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?
Posted: Tue Nov 08, 2005 8:37 am
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
Posted: Tue Nov 08, 2005 8:50 am
by n00b Saibot
damn, that link is giving me
404
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

Posted: Tue Nov 08, 2005 8:54 am
by vincenzobar
Posted: Tue Nov 08, 2005 9:07 am
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

Posted: Tue Nov 08, 2005 9:19 am
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
Posted: Tue Nov 08, 2005 9:25 am
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

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...

Posted: Tue Nov 08, 2005 9:25 am
by vincenzobar
and XML_IDList is only pulling one record
Posted: Tue Nov 08, 2005 9:29 am
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?