Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
inheritance of previous value
Old 03-28-2011, 02:18 PM inheritance of previous value
Extreme Talker

Posts: 173
Trades: 0
I am parsing a text file line by line. As I import the lines, I am storing the values is a class called Material.

The code seems to be parsing the string correctly however, when I go to save the value the new material that is created is inheriting the previous value and will no overwrite the value.

So this is wrong on two accounts, one is the values should be created with a NULL value by default and then not inherit the previous value.

Can anyone see the problem with my code?

The class is created at the end of the code and the call to the class is on line 49 our you could search for "new Material"



PHP Code:
<?php
//delete these rows*************************************
@include 'globalcfg.php';
@include 
'db_connect.php';
@include 
'function_library.php';
//******************************************************
set_time_limit(600);
ini_set('memory_limit''5120M');
$DEBUG FALSE;// could be false
if ($DEBUG) {
 
ini_set('display_errors''1');
 
error_reporting(E_ALL);
}
// accepts file name
//if (isset($_REQUEST['filename']))  $file_name=$_REQUEST['filename'];
//else         die('No filename provided...');
$file_name "3418499";
// Open file in read mode
$file = @fopen($file_name"r") or exit("Unable to open file!");
if (
$file) {
    
$sLBL=fgets($file); //Skip blank line
    
$sLBL=fgets($file);
 
$project_type=shatter($sLBL,0);
 
//Determines the starting point of each column
    
$pos[0]=strpos($sLBL"Description");
    
$pos[1]=strpos($sLBL"Rec./reqd qty");
    
$pos[2]=strpos($sLBL"Receipt Element");
    
$pos[3]=strpos($sLBL"Requirements Date");
    
$pos[4]=strpos($sLBL"Receipt Date");
    
$pos[5]=strpos($sLBL"Receipt Element No.");
    
$pos[6]=strpos($sLBL"Receipt Element Item");
    
$pos[7]=strpos($sLBL"Key");
    
$pos[8]=strpos($sLBL"Plant");
    
$sLBL=fgets($file); //Skip blank line
    
$sLBL=fgets($file);
 
$project=trim(ltrim($sLBL,"|-0 "));
    
$sLBL=fgets($file); //Skip blank line
    
$sLBL=fgets($file);
    
//Loops through the material status lines until end of file
    
$cnt=0;
 while (!
feof($file)) {
  
$empty_string=trim(trim($sLBL),"|- ");
  if(!empty(
$empty_string)){
   
$material[$cnt]=new Material;
   
$material[$cnt]->level=level($sLBL);
         
$material[$cnt]->part=parse_detail($sLBL,0,$pos[0]);
         
$material[$cnt]->part_descparse_detail($sLBL,$pos[0],$pos[1]);
         
$material[$cnt]->qty=trim(rtrim(substr($sLBL,$pos[1],$pos[2]-$pos[1]),"- "));    
         
$material[$cnt]->status=parse_detail($sLBL,$pos[2],$pos[3]);
         
$material[$cnt]->rdate=parse_detail($sLBL,$pos[3],$pos[4]);
         
$material[$cnt]->edate=parse_detail($sLBL,$pos[4],$pos[5]);
   if(
$material[$cnt]->status=="PO ScL"){
    
$material[$cnt]->po=parse_detail($sLBL,$pos[5],$pos[6]);
    
$material[$cnt]->line=parse_detail($sLBL,$pos[6],$pos[7]);
   }
   if(
$material[$cnt]->status=="PurRqs"){
       
$material[$cnt]->prod=parse_detail($sLBL,$pos[5],$pos[6]);    
   }
   if(
$material[$cnt]->status=="PrdOrd"){
          
$material[$cnt]->pr=parse_detail($sLBL,$pos[5],$pos[6]);
   }
         
$material[$cnt]->error=parse_detail($sLBL,$pos[7],$pos[8]);    
         
$material[$cnt++]->factory=parse_detail($sLBL,$pos[8],strlen($sLBL));
  }
     
$sLBL=fgets($file);
 }
 if (
$DEBUG) {echo "<pre>"var_dump($material); echo "</pre>\n";}
 
fix_dates($material);
 if (
$DEBUG) {echo "<pre>"var_dump($material); echo "</pre>\n";}
 
dump_data($project_type,$project,$material);
 
clean_up($project);
 
status_updates($project_type,$project);
}
//Removes old parts that have been removed from MD4C report after 15 days of not showing up on report
function clean_up($project){
 
$sql='DELETE FROM tbl_material WHERE tbl_material.module_id IN('.
   
'SELECT tbl_module.module_id FROM tbl_module WHERE tbl_module.project_id IN ('.
    
'SELECT tbl_project.project_id FROM tbl_project '.
    
'WHERE project="'.$project.'" OR network_number="'.$project.'")'.
   
') '.
  
'AND tbl_material.date_stamp<=DATE_SUB(NOW(),INTERVAL 3 MINUTE)';
 
query($sql);
}
//Updates status counts for project
function status_updates($project_type,$project)  {
 if(!
strcmp($project_type,"NetwkOrder")) $sql='SELECT project_id  FROM tbl_project WHERE network_number='.$project;
 else          
$sql='SELECT project_id FROM tbl_project WHERE project='.$project;
 
$row mysql_fetch_assoc(query($sql));
 
$project_id $row['project_id'];
 
$status_list=array("Plan.order","POSched.ln","QM","Plant");
 
$i=0;
 foreach(
$status_list as $s){
  
$sql='SELECT COUNT(*) AS cnt '
    
'FROM tbl_project INNER JOIN (tbl_module INNER JOIN tbl_material ON tbl_module.module_id = tbl_material.module_id) '.
    
'ON tbl_project.project_id = tbl_module.project_id '.
    
'WHERE tbl_project.project_id='.$project_id.' AND tbl_material.status=\''.$s.'\'';    
  
$row=mysql_fetch_assoc(query($sql));
  
$status_results[$i++]=$row['cnt'];
 }
 
$status_list=array("planned","po","qm","ps");
 
$i=0;
 
$sql_begin='INSERT INTO tbl_archive_status (project_id, ';
 
$sql_end'VALUES ('.$project_id.', ';
 foreach (
$status_list as $s) {
  if (!empty(
$status_results[$i])){
   
$sql_begin.=$s.', ';
   
$sql_end.=$status_results[$i].', ';
  }
  
$i++;
 }
 
$sql='SELECT COUNT( * ) AS pr FROM ( '.
   
'SELECT DISTINCT  pr  FROM tbl_project INNER JOIN '.
   
'(tbl_module INNER JOIN tbl_material ON tbl_module.module_id=tbl_material.module_id) '.
   
'ON tbl_project.project_id=tbl_module.project_id WHERE tbl_project.project_id ='.$project_id.') t2'
 
$row mysql_fetch_assoc(query($sql));
 if (
$row['pr']>1){
  
$sql_begin.='pr, ';
  
$sql_end.=($row['pr']-1).', ';
 }
 
$sql_begin=substr($sql_begin0, -2);  
 
$sql_end=substr($sql_end0, -2);
 
$result=query($sql_begin.') '.$sql_end.')');
}
//Dumps data to database
function dump_data($project_type,$project,&$my_material)  {
 if(!
strcmp($project_type,"NetwkOrder")){
  
//Determine if project already exists in database
  
$sql 'SELECT project_id  FROM tbl_project WHERE network_number='.$project;
  
//If project exists, return id
  
$result=query($sql);
  if (
mysql_num_rows($result)){
   
$row mysql_fetch_assoc($result);
   
$project_id $row['project_id'];
  }
  
//If project does not exist, add record and return id
  
else{       
   
$sql='INSERT INTO tbl_project(project, network_numbe) VALUES ('.$project.', '.$project.')';
   
$result=query($sql);
   
$project_id mysql_insert_id();
  }
 }
 else{
  
//Determine if project already exists in database
  
$sql 'SELECT project_id, project FROM tbl_project WHERE project='.$project;
  
//If project exists, return id
  
if (mysql_num_rows(query($sql))){
   
$row mysql_fetch_assoc(query($sql));
   
$project_id $row['project_id'];
  }
  
//If project does not exist, add record and return id
  
else{
   
$sql='INSERT INTO tbl_project(project) VALUES ('.$project.')';
   
$result=query($sql);
   
$project_id mysql_insert_id();
  }
 }
 
//Saves MD4C data to table either module or material tables
 
foreach ($my_material as &$value) {
  
$value->part_desc=mysql_real_escape_string($value->part_desc);
  
//Determine if part is a module or a part
  
if($value->level==1){
   
$sql 'SELECT * FROM tbl_module WHERE module="'.$value->part.'" AND project_id='.$project_id;
   
$result=query($sql);
   
//Confirms if module exist and returns id
   
if (mysql_num_rows($result)){ 
    
$row mysql_fetch_assoc($result);
    
$module_id $row['module_id'];
    
$sql_begin='UPDATE tbl_module SET ';
    
$flag=0;
    if(
strcmp($value->status,$row['status'])){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='status="'.sanitizeMySQL($value->status).'", flag_status=NOW()';
    }
    if(
strcmp($value->rdate,$row['rdate'])){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='rdate="'.sanitizeMySQL($value->rdate).'", flag_rdate=NOW()';
    }
    if(!(
$value->qty=$row['qty'])){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='qty='.sanitizeMySQL(str_replace(",","",$value->qty)).', flag_qty=NOW()';
    }
    if(
strcmp($value->prod,$row['prod'])){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='prod="'.sanitizeMySQL($value->prod).'", flag_prod=NOW()';
    }
    if(
strcmp($value->factory,$row['factory'])){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='factory="'.sanitizeMySQL($value->factory).'", flag_factory=NOW()';
    }
    if(
$flag){
     
$sql=$sql_begin.' WHERE module_id='.$module_id;
     
$result1=query($sql);
    }
   }
   
//else adds module to table and returns id
   
else{       
    
$sql_begin='INSERT INTO tbl_module (project_id, module, module_desc, status,rdate, qty, factory';
    
$sql_end='VALUES ( '.$project_id.', "'.
         
sanitizeMySQL($value->part).'", "'.
         
sanitizeMySQL($value->part_desc).'", "'.
         
sanitizeMySQL($value->status).'", "'.
         
sanitizeMySQL($value->rdate).'", '.
         
sanitizeMySQL($value->qty).', "'.
         
sanitizeMySQL($value->factory).'"'
    if(!empty(
$value->prod)){
     
$sql_begin.=', prod';
     
$sql_end.=', "'.sanitizeMySQL($value->prod).'"';
    }
    
$sql $sql_begin.') '.$sql_end.')';
    
query($sql);
    
$module_id mysql_insert_id();
   }
  }
  
//records part details to table
  
else{
   
$sql 'SELECT * FROM tbl_material '.
       
'WHERE module_id='.$module_id.' AND part="'.$value->part.'" AND factory="'.$value->factory.'" AND date_stamp<=DATE_SUB(NOW(),INTERVAL 3 MINUTE)';
   
$result=query($sql);
   
//Material exists in table, update details as required
   
if (mysql_num_rows($result)){
    
$row mysql_fetch_assoc($result);
    
$material_id $row['material_id'];
    
$sql_begin='UPDATE tbl_material SET date_stamp=NOW(), ';
    
$flag=0;
    if(
$value->status<>$row['status']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='status="'.sanitizeMySQL($value->status).'", flag_status=NOW()';
    }
    if(
$value->qty<>$row['qty']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='qty='.sanitizeMySQL(str_replace(",","",$value->qty)).', flag_qty=NOW()';
    }
    if(
$value->edate<>$row['edate'] && !empty($value->edate)){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='edate="'.sanitizeMySQL($value->edate).'", flag_edate=NOW()';
    }
    if(
$value->rdate<>$row['rdate']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='rdate="'.($value->rdate).'", flag_rdate=NOW()';
    }
    if(
$value->pr<>$row['pr']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='pr="'.($value->pr).'", flag_pr=NOW()';
    }
    if(
$value->po<>$row['po']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='po="'.sanitizeMySQL($value->po).'", flag_po=NOW()';
    }
    if(
$value->line<>$row['line']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='line="'.sanitizeMySQL($value->line).'", flag_line=NOW()';
    }
    if(
$value->rdate<>$row['factory']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='factory="'.sanitizeMySQL($value->factory).'", flag_factory=NOW()';
    }
    if(
$value->rdate<>$row['error']){
     if(
$flag$sql_begin.=', ';
     
$flag=1;
     
$sql_begin.='error="'.sanitizeMySQL($value->error).'", flag_error=NOW()';
    }
    if(
$flag){
     
$sql=$sql_begin.' WHERE material_id='.$material_id;
     
$result1=query($sql);
    }
   }
   
//Material does not exist, add recordset to table
   
else{
    
$sql_begin='INSERT INTO tbl_material (module_id, level, part, part_desc';
    
$sql_end'VALUES ('.$module_id.', "'.
          
sanitizeMySQL($value->level).'", "'.
          
sanitizeMySQL($value->part).'" ,"'.
          
sanitizeMySQL($value->part_desc).'" ';
    if(!empty(
$value->status)){
     
$sql_begin.=', status, flag_status';
     
$sql_end.=', "'.sanitizeMySQL($value->status).'" , Now()';
    }
    if(!empty(
$value->edate)){
     
$sql_begin.=', edate, flag_edate';
     
$sql_end.=', "'.sanitizeMySQL($value->edate).'" , Now()';
    }
    if(!empty(
$value->rdate)){
     
$sql_begin.=', rdate, flag_rdate';
     
$sql_end.=', "'.sanitizeMySQL($value->rdate).'" , Now()';
    }
    if(!empty(
$value->qty)){
     
$sql_begin.=', qty, flag_qty';
     
$sql_end.=', '.sanitizeMySQL(str_replace(",","",$value->qty)).' , Now()';
    }
    if(!empty(
$value->pr)){
     
$sql_begin.=', pr, flag_pr';
     
$sql_end.=', "'.sanitizeMySQL($value->pr).'" , Now()';
    }
    if(!empty(
$value->po)){
     
$sql_begin.=', po, flag_po';
     
$sql_end.=', "'.sanitizeMySQL($value->po).'" , Now()';
    }
    if(!empty(
$value->line)){
     
$sql_begin.=', line, flag_line';
     
$sql_end.=', "'.sanitizeMySQL($value->line).'" , Now()';
    }
    if(!empty(
$value->factory)){
     
$sql_begin.=', factory, flag_factory';
     
$sql_end.=', "'.sanitizeMySQL($value->factory).'" , Now()';
    }
    if(!empty(
$value->error)){
     
$sql_begin.=', error, flag_error';
     
$sql_end.=', "'.sanitizeMySQL($value->error).'" , Now()';
    }
    
$query $sql_begin.') '.$sql_end.')';
    
$result1=query($sql_begin.') '.$sql_end.')');
   } 
    }
  }
}
// array passed by reference so changes made here are reflected on the true array, more effeciant
function fix_dates(&$my_material) { 
 
// Check date format, could be yy/mm/dd or mm/dd/yy and update it to yy/mm/dd if it is mm/dd/yy
 // dates are stored in $my_material->edate and $my_material->rdate
 
$is_date_mm_dd_yy TRUE;
 
$is_date_yy_mm_dd TRUE;
 
 
// Determine if date is mm/dd/yy
 
foreach ( $my_material as &$value ) {
  if (!
is_null($value->edate))  {
   if(!
checkdatesubstr($value->edate02),substr($value->edate32),"20".substr($value->edate62))
   or 
abs("20".substr($value->edate62) - date("Y")) > 10 ){
    
$is_date_mm_dd_yy FALSE;
    break;
    }
   }
  if (!
is_null($value->rdate))  {
   if(!
checkdatesubstr($value->rdate02),substr($value->rdate32),"20".substr($value->rdate62))
   or 
abs("20".substr($value->rdate62) - date("Y")) > 10 ){
    
$is_date_mm_dd_yy FALSE;
    break;
    }
   }
  }
 
// Determine if date is yy/mm/dd
 
foreach ($my_material as &$value) {
  if (!
is_null($value->edate))  {
   if(!
checkdatesubstr($value->edate32),substr($value->edate62),"20".substr($value->edate02))) {
    
$is_date_yy_mm_dd FALSE;
    break;
    }
   }
  if (!
is_null($value->rdate))  {
   if(!
checkdatesubstr($value->rdate32),substr($value->rdate62),"20".substr($value->rdate02))) {
    
$is_date_yy_mm_dd FALSE;
    break;
    }
   }
  }
 if ( 
$is_date_yy_mm_dd and $is_date_mm_dd_yy ) {
   echo 
'Date structure is indeterminate<br />';
  
//return $my_material;
 
} elseif ( !$is_date_yy_mm_dd and !$is_date_mm_dd_yy ) {
   echo 
'Date could be either<br />'
  
//return $my_material;
 
} elseif ( $is_date_mm_dd_yy ) {
   echo 
'Date structure mm-dd-yy, will be converted to yy-mm-dd<br />';
   foreach ( 
$my_material as &$value ) {
   
$value->edate substr($value->edate62) . $value->edate[5] . substr($value->edate05);
   
$value->rdate substr($value->rdate62) . $value->rdate[5] . substr($value->rdate05);
  }
 } elseif ( 
$is_date_yy_mm_dd ) {
   echo 
"Date structure yy-mm-dd, date does not need to be converted<br />";
 }
}
//Isolated project details from given string
function parse_detail($string,$pos_start,$pos_end){
 
$charlist="|-0 ";
 
$result=substr($string,$pos_start,$pos_end-$pos_start);
 
$result=ltrim($result,$charlist);
 
$result=trim($result);
 if(empty(
$result)) $result=NULL;
 return 
$result;
}
//Isolates part number from string
function component($string,$pos) { 
    
$assembly explode(" ",substr($string,($pos strlen($string))));
    return 
$assembly[0];
    }
 
//Function returns nth array value from a string that has been exploded and trimmed
function shatter($string,$pos) {    
 
$string_a=(explode(" ",$string));
    
$type=trim($string_a[$pos]);
 return 
$type;
}
//Determines the level of the part
function level($string){
 if(
strpos($string,"---")) $lvl=(strpos($string,"-")  )/4-1;
 else      
$lvl=(strpos($string,"-")+1)/4-1;
 return 
$lvl;
}
class 
Material  {
    public 
$level NULL;       //Part level
    
public $part NULL;            //Part number
    
public $part_desc NULL;       //Part description
    
public $status NULL;    //Status of part
    
public $rdate NULL;           //Date the parts are required by
    
public $edate NULL;           //Date the parts are expected
    
public $qty NULL;             //Required quantity for project
    
public $pr NULL;              //Purchase requistion number to buy part 
    
public $po NULL;              //PO numbere that is buying the part
    
public $line NULL;            //Line on PO that is purchasing the part
    
public $factory NULL;         //Manufacturing facility for part
    
public $error NULL;           //SAP error code
    
public $prod NULL;            //Production order number
}
?>
dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-29-2011, 12:27 AM Re: inheritance of previous value
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
On a very quick eyeball I can't see $cnt being incremented.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 03-29-2011, 09:56 AM Re: inheritance of previous value
Extreme Talker

Posts: 173
Trades: 0
That was something I thought as well, but if does create 187 parts all with the same details.

Here is the line with the counter.
PHP Code:
$material[$cnt++]->factory=parse_detail($sLBL,$pos[8],strlen($sLBL)); 
When I step through the code, should it jump down to the class and step through the class when it initalizes it?
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-01-2011, 09:28 PM Re: inheritance of previous value
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
Put the increment on $material[$cnt]=new Material;

and that line should be where the initialisation code is invoked


__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to inheritance of previous value
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.69208 seconds with 12 queries