Solved

PHP Loop that Did Not Work

Posted on 2011-09-06
27
345 Views
Last Modified: 2012-05-12
I am trying to figure out why the PHP loop in my related question did not work.  I am trying to replace the Date field data with a data from the date_submitted field for each record.  What happens is that, when displayed, it places the date_submitted data as requested but in the NEXT RECORD!  So, the first record gets no date and the date for the first record ends up in the date column of the second record.  

All of the other fields do not have this issue.

Any ideas as to why only this field does this and where the fault lies?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Web Order Review and Insert</title>
	
	<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
	
</head>

<body>
	
	<div id="wrapper">

<?php

	include('conn.php');
		
	//******** Detect line endings on Macintosh **********//		
	ini_set('auto_detect_line_endings',TRUE);
	
	
	//******** Initialize the CSV file row to "0" **********//
	$row = 0;
	
	$cid_array = array('');
	
	$month_array = array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	
	
	//******** Open the CSV file (place filename here) **********//
	$handle = fopen("orders.csv", "r");
	
		
	echo '<form action="create_file.php" method="post">';
		
	while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
		
		//*********** Assign the CSV headers to array $tbl_headers **********//
		if($row == 0){
			
			$tbl_headers = $data;	
			
			$row++;
			
		}else{
		
		    $num = count($data);
		    $row++;
			
			echo '<h1>Order #' . ($row - 1) . '</h1>';
			echo '<fieldset><ul>';
			
			//*********** Get CustomerID, Date from CSV file **********//
			for ($c=0; $c < $num; $c++) {
			
				if($tbl_headers[$c] == 'CustomerID'){
					
					$cid = $data[$c];
					
				}
				if($tbl_headers[$c] == 'Date'){
										
					$data[$c] = $dp;
					//$order_date = $data[$c];								
					
				}
				if($tbl_headers[$c] == 'date_submitted'){
					
					$dp = substr($data[$c],0,10);
					
				}

				
				if($tbl_headers[$c] == 'LoanMaturedate'){
					$LoanMatureDate2 = $data[$c];
					
					
					if($data[$c] == ''){
						
						$data[$c] = '0000-00-00';
						
					}
					
				}
				
				if($tbl_headers[$c] == 'LoanMatureDateV2'){
					$LoanMatureDate_V2 = $data[$c];
					
					if(preg_match('/\d\d\-[A-Za-z][A-Za-z][A-Za-z]\-\d\d/', $LoanMatureDate_V2)){
						
						$day = substr($LoanMatureDate_V2, 0, 2);

						$month = substr($LoanMatureDate_V2, 3, 3);
						
						foreach($month_array as $key2 => $value){
							
							if($month == $value){
								
								$month = $key2;
								
								if(strlen($month) == 1){
									
									$month = '0' . $month;
									
								}
								
							}
							
						}

						$year = '20' . substr($LoanMatureDate_V2, -2);

						$sql_date2 = $year . '-' . $month . '-' . $day;
												
						$data[$c] = $sql_date2;
						
					}else if(preg_match('/\d\d\/\d\d\/\d\d\d\d/', $LoanMatureDate_V2)){
						
						$month = substr($LoanMatureDate_V2, 0, 2);

						$day = substr($LoanMatureDate_V2, 3, 2);

						$year = substr($LoanMatureDate_V2, -4);
						
						$sql_date2 = $year . '-' . $month . '-' . $day;
						
						$data[$c] = $sql_date2;
						
											
					}
					if($data[$c] == ''){
						
						$data[$c] = '0000-00-00';
						
					}
					
				}

			
			}			
			
			
			//*********** Retrieve, calculate, and display next GCE Number **********//
			$sql = "SELECT Filenumid FROM customerwebid WHERE `Customer ID` = '$cid'";
			$results = mysql_query($sql) or die(mysql_error());
			$row_filenumid = mysql_fetch_array($results);
			$fid = $row_filenumid[0];

			$sqlz = "SELECT MAX(`GCE File`) FROM filesthisyear WHERE `GCE File` LIKE '$fid%'";				
			$results = mysql_query($sqlz) or die(mysql_error());
			$row2 = mysql_fetch_array($results);
			
			$k = 100;
			
			//*********** Determine last used GCE Number **********//
			if(in_array($fid, $cid_array)){				
				
				foreach($cid_array as $key => $value){					
					
					if($value == $fid){
						
						$k += 100;
						
					}
					
				}
				
				$cid_array[] = $fid;
				
			}else{
				
				$cid_array[] = $fid;
				
			}
			
			if($row2[0] != NULL){
				$sub = substr($row2[0], -6) + $k;		
			}else{
				$sub = '0' . date('y') + $k;
			}
			
			
			
			//*********** Make sure order always contains six numeric digits **********//
			if(strlen($sub) == 3){
				$sub = '000' . $sub;
			}elseif (strlen($sub) == 4) {
				$sub = '00' . $sub;
			}elseif (strlen($sub) == 5) {
				$sub = '0' . $sub;
			}
			
			//make_six($sub);
			
			//$gce_number = $fid . $sub;
			
			
			
			
			echo '<li><label><strong>GCE File Number</strong></label><input type="text" name="' . $row . '_gce" value="' . $fid . $sub . '" /></li>';				
					
			
			
			//*********** Create list items of each CSV field **********//
		    for ($c=0; $c < $num; $c++) {
			
				echo '<li><label>' . $tbl_headers[$c] . '</label>';
		
			    echo '<input type="text" value="' . htmlentities($data[$c]) . '" name="' . $row . '_' . $tbl_headers[$c] . '"/></li>';			
	
		    }
			
			echo '</ul></fieldset>';
		
		}
		
	}
		
	echo '<input type="submit" id="submit" value="Import Into Jobs"/>';
	
	
	//*********** Pass CSV header values to next script **********//
	echo '<input type="hidden" name="tbl_headers" value="' . implode(',', $tbl_headers) . '" />';
	
	echo '</form>';
		
	fclose($handle);
	
	function make_six($num){
		if(strlen($num) == 3){
			$num = '000' . $num;
		}elseif (strlen($num) == 4) {
			$num = '00' . $num;
		}elseif (strlen($num) == 5) {
			$num = '0' . $num;
		}
		
		return $num;
	}
	
	function make_date(){
		
	}
		

?>

</div>

</body>
</html>

Open in new window

0
Comment
Question by:hydrazi
  • 12
  • 8
  • 3
  • +2
27 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 36489763
First, put in some echoes at the points where the field values should be correct, and see which variables are not what they should be.

echo $variablename;

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 36489797
Can you strip out parts that don't affect the actual working.

But I think I can..

Follow this.

You open file, extract headers and then get the next row.

You are now at line 51.

You start examining the columns.

Lines 65-70 deal with the column heading 'Date'.
Lines 71-75 deal with the column heading 'date_submitted'.

Now. In the Date handler you use the variable $dp, but is assigned a value in date_submitted.

Assuming that the column order is Date, date_submitted, then $dp won't get a value in THIS row until it you process the date_submitted column.

When you've assigned a value to $dp, it won't get used until the Date handler is called and that happens in the next row.

I'll come back with a little tidy up for you to try.
0
 

Author Comment

by:hydrazi
ID: 36489815
I have tried it in a different order, though I wonder if it is because in the CSV, the DATE column comes before date_submitted.
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 100 total points
ID: 36489819
<?php
$handle = fopen('orders.csv', 'r');
if (False === ($tbl_headers = fgetcsv($handle, 1000, '|')))
	{
	die('No content');
	}

while(False !== ($a_Row = fgetcsv($handle, 1000, '|')))
	{
	$data = array_combine($tbl_headers, $a_Row);
	
	// Now you can use $data['CustomerID'], $data['Date'], $data['date_submitted'], etc.
	}

Open in new window


By extracting the column headings and then combining them with data, you can create an associative array, just like you would get from a database, allowing you to access the data in a more sensible way.

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 36489843
@hydrazi it is.

And I've given you a possible solution by using the array_combine() function to produce an associative array, allowing you to access the elements directly and in any order.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36490073
Please post the input test data you are using.  I may be able to help you get it sorted out.

While we are waiting to see the test data, please read this article about how to handle DATETIME fields in PHP.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 82

Expert Comment

by:hielo
ID: 36492706
save the attached code as test.php and try it:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Web Order Review and Insert</title>
	
	<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
	
</head>

<body>
	
	<div id="wrapper">

<?php

	include('conn.php');
		
	//******** Detect line endings on Macintosh **********//		
	ini_set('auto_detect_line_endings',TRUE);
	
	
	//******** Initialize the CSV file row to "0" **********//
	$row = -1;
	
	$cid_array = array('');
	
	$month_array = array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	
	
	//******** Open the CSV file (place filename here) **********//
	$handle = fopen("orders.csv", "r");
	
		
	echo '<form action="create_file.php" method="post">';
		
	while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
		++$row;
		//*********** Assign the CSV headers to array $tbl_headers **********//
		if($row == 0){
			$tbl_headers =  $data;	
			$temp=array_combine( $data, array_keys($data))
			extract(  $temp, EXTR_PREFIX_ALL, 'tblHeader_');
		}else{

			echo '<h1>Order #',$row,'</h1>';
			echo '<fieldset><ul>';
			
			//*********** Get CustomerID, Date from CSV file **********//
					$cid = $data[$tblHeader_CustomerID];
					$dp = substr($data[$tblHeader_date_submitted],0,10);
					$data[$tblHeader_Date] = $dp;

					$LoanMatureDate2 = $data[$tblHeader_LoanMaturedate];
					if($data[$tblHeader_LoanMaturedate] == ''){
						$data[$tblHeader_LoanMaturedate] = '0000-00-00';
					}

					$LoanMatureDate_V2 = $data[$tblHeader_LoanMatureDateV2];
					if(preg_match('/\d\d\-[A-Za-z][A-Za-z][A-Za-z]\-\d\d/', $LoanMatureDate_V2))
					{	
						$day = substr($LoanMatureDate_V2, 0, 2);

						$month = substr($LoanMatureDate_V2, 3, 3);
						
						foreach($month_array as $key2 => $value){
							
							if($month == $value){
								
								$month = $key2;
								
								if(strlen($month) == 1){
									
									$month = '0' . $month;
									
								}
								
							}
							
						}

						$year = '20' . substr($LoanMatureDate_V2, -2);

						$sql_date2 = $year . '-' . $month . '-' . $day;
												
						$data[$tblHeader_LoanMatureDateV2] = $sql_date2;
						
					}
					else if(preg_match('/\d\d\/\d\d\/\d\d\d\d/', $LoanMatureDate_V2))
					{
						$month = substr($LoanMatureDate_V2, 0, 2);
						$day = substr($LoanMatureDate_V2, 3, 2);
						$year = substr($LoanMatureDate_V2, -4);
						$sql_date2 = $year . '-' . $month . '-' . $day;
						$data[$tblHeader_LoanMatureDateV2] = $sql_date2;
					}

					if($data[$tblHeader_LoanMatureDateV2] == '')
					{
						$data[$tblHeader_LoanMatureDateV2] = '0000-00-00';
					}
			
			
			//*********** Retrieve, calculate, and display next GCE Number **********//
			$sql = "SELECT Filenumid FROM customerwebid WHERE `Customer ID` = '$cid'";
			$results = mysql_query($sql) or die(mysql_error());
			$row_filenumid = mysql_fetch_array($results);
			$fid = $row_filenumid[0];

			$sqlz = "SELECT MAX(`GCE File`) FROM filesthisyear WHERE `GCE File` LIKE '$fid%'";				
			$results = mysql_query($sqlz) or die(mysql_error());
			$row2 = mysql_fetch_array($results);
			
			$k = 100;
			
			//*********** Determine last used GCE Number **********//
			if(in_array($fid, $cid_array)){				
				
				foreach($cid_array as $key => $value){					
					
					if($value == $fid){
						
						$k += 100;
						
					}
					
				}
				
				$cid_array[] = $fid;
				
			}else{
				
				$cid_array[] = $fid;
				
			}
			
			if($row2[0] != NULL){
				$sub = substr($row2[0], -6) + $k;		
			}else{
				$sub = '0' . date('y') + $k;
			}
			
			
			
			//*********** Make sure order always contains six numeric digits **********//
			if(strlen($sub) == 3){
				$sub = '000' . $sub;
			}elseif (strlen($sub) == 4) {
				$sub = '00' . $sub;
			}elseif (strlen($sub) == 5) {
				$sub = '0' . $sub;
			}
			
			//make_six($sub);
			
			//$gce_number = $fid . $sub;
			
			
			
			
			echo '<li><label><strong>GCE File Number</strong></label><input type="text" name="' . $row . '_gce" value="' . $fid . $sub . '" /></li>';				
					
			
			
			//*********** Create list items of each CSV field **********//
		    foreach($tbl_headers as $c=>$label) {
			
				echo '<li><label>' . $label . '</label>';
		
			    echo '<input type="text" value="' . htmlentities($data[$c]) . '" name="' . $row . '_' . $label . '"/></li>';			
	
		    }
			
			echo '</ul></fieldset>';
		
		}
		
	}
		
	echo '<input type="submit" id="submit" value="Import Into Jobs"/>';
	
	
	//*********** Pass CSV header values to next script **********//
	echo '<input type="hidden" name="tbl_headers" value="' . implode(',', $tbl_headers) . '" />';
	
	echo '</form>';
		
	fclose($handle);
	
	function make_six($num){
		if(strlen($num) == 3){
			$num = '000' . $num;
		}elseif (strlen($num) == 4) {
			$num = '00' . $num;
		}elseif (strlen($num) == 5) {
			$num = '0' . $num;
		}
		
		return $num;
	}
	
	function make_date(){
		
	}
		

?>

</div>

</body>
</html>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 36492892
Just a thought to help you simplify the code... You can replace this:
function make_six($num){
		if(strlen($num) == 3){
			$num = '000' . $num;
		}elseif (strlen($num) == 4) {
			$num = '00' . $num;
		}elseif (strlen($num) == 5) {
			$num = '0' . $num;
		}
		
		return $num;
	}

Open in new window

with this:
$padded_num = str_pad($num, 6, '0', STR_PAD_LEFT);

Open in new window

See http://php.net/manual/en/function.str-pad.php

All of the PHP functions are documented in the online man pages of http://php.net.  It's worth reading, since most of the commonly used functions have interesting user-contributed notes that can be very eye-opening.

Best regards, ~Ray
0
 

Author Comment

by:hydrazi
ID: 36494917
So many awesome answers.... I will try these this afternoon!  Will report back.
0
 
LVL 82

Expert Comment

by:hielo
ID: 36495302
Line 45 of post ID:36492706 is missing a semicolon at the end.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36496534
This question looks somewhat similar to the earlier example I posted for you, reproduced below.  It does the "heavy lifting" of assigning named keys to associative array elements in line 43.  This data construct eliminates the need for the for() loop and all the comparison tests shown in lines 57-144 of the code snippet in the original post of this question.  You simply address the fields directly by name, instead of testing each of the names to see if it equals a constant.  We did that in line 58 of the snippet below when we replaced a target column with information from a source column.  It is really much simpler when you do it that way.

I'd like to take a moment to consider the programming surrounding the field named LoanMatureDateV2.  In the other example, where we had some test data to work with, LoanMatureDateV2 was empty, but we had ISO-8601 DATETIME strings in the fields called Date and date_submitted.  We used the value in date_submitted to replace the value in Date.  In the current code snippet, a lot of processing goes on between line 90 and line 141 that seems to be related to reformatting the date string in LoanMatureDateV2, perhaps from something like 07-Sep-11 or 09/07/2011 into the standard ISO-8601 date format.  (I am not exactly sure about this because there are no comments in the code to explain the 50+ lines of programming.)  That reformatting is usually accomplished with something like this:

if ($x = strtotime(LoanMatureDateV2))
{
    $sql_date2 = date('Y-m-d', $x);
}
else
{
    $sql_date2 = '0000-00-00';
}

You can test strings for usability with strtotime() on my server here.
http://www.laprbass.com/RAY_strtotime.php?s=07-Sep-11
http://www.laprbass.com/RAY_strtotime.php?s=09%2F07%2F2011

I hope these suggestions help you move forward with your project.  And if you can post some test data that is representative of your expected input, I will be glad to show you how I might process it, and how I might visualize the data during the debugging process (see line 55 of the code snippet below).

Best regards, ~Ray
<?php // RAY_temp_hydrazi.php
error_reporting(E_ALL);


// TEST DATA FROM THE POST AT EE LOOKS LIKE THIS:
$dat = <<<ENDDAT
OrderID|CustomerID|Attention|Date|Buyer|Seller|Address|City|State|Zip|County|Book|Page|Plannum|Taxmap|Block|Lot|FileRefNo|LoanMaturedate|Lender|EstCloseDate|FloodNeed|PlotNeed|Need|LegalDescrip|P_S|Ord_Plan|Taxbill|Attachment_ID|type_form|date_submitted|Comment|ClientRefNum|LoanMatureDateV2
49201|101|Joe Smith|28-Feb-11|Albert Doe||1 Mountain Hwy|Somewhere|NH|03333|Stifford|3877|0445||42||173|||||||0|0|0|0|0||flood cert.|2011-02-28 09:30:49|||
49202|384|Gary Smithe|28-Feb-11|Cabbage Head,LLC||2 Super Street,|Everything|NH|03111|Stonystone|||||||||||||0|0|0|0|0||flood cert.|2011-02-28 09:33:55|Current Owner: Someone||
ENDDAT;


// THE URL OF THE CSV FILE
$url = 'RAY_temp_hydrazi.csv';

// THE FIELD DELIMITER
$dlm = '|';

// THE TARGET FIELD WE WANT TO REPLACE
$tgt = 'Date';

// THE SOURCE FIELD THAT REPLACES THE TARGET
$src = 'date_submitted';


// OPEN THE FILE
$fp = fopen($url, 'r');
if (!$fp) die("CANNOT USE $url");

// READ THE TITLES
$top = fgetcsv($fp, 0, $dlm);

// ITERATE OVER ALL OF THE ROWS OF THE FILE
$arr = array();
$num = 1;
while (!feof($fp))
{
    // INCREMENT THE LINE COUNTER
    $num++;

    // READ THE DATA AND MARRY IT UP WITH THE FIELD NAMES
    $dat = fgetcsv($fp, 0, $dlm);
    if (!$row = array_combine($top, $dat))
    {
        // HANDLE ERRORS OF MISMATCHED DATA
        echo PHP_EOL . "ERROR IN LINE NUMBER $num";
        echo PHP_EOL . "DUMP OF DATA: ";
        var_dump($dat);
    }

    // IF THE DATA MARRIES UP CORRECTLY
    else
    {
        // ACTIVATE THIS TO SEE THE ASSOCIATIVE ARRAY
        // var_dump($row);

        // REPLACE THE DATE FIELD
        $row[$tgt] = $row[$src];

        // PROCESS THE ROW INTO FORM INPUT CONTROLS WITH PRE-FILLED DATA
        $str = NULL;
        foreach ($row as $key => $val)
        {
            $str
            .= PHP_EOL
            . $key
            . '<input'
            . ' name="'
            . $key
            . '"'
            . ' value="'
            . $val
            . '"'
            . ' />'
            . '<br/>'
            ;
        }
        // SAVE THE INPUT FOR EACH FORM STATEMENTS HERE
        $arr[$num] = $str;
    }
}

// PREPARE THE HTML WITH THE PRE-POPULATED FORMS
$htm = '<!DOCTYPE HTML><html><body>';
foreach ($arr as $num => $str)
{
    $cnt = $num - 1;
    $htm .= "<h2>FORM NUMBER $cnt</h2>" . PHP_EOL;
    $htm .= '<form action="RAY_bounce_post.php" method="post">' . PHP_EOL;
    $htm .= $str;
    $htm .= '<input type="submit" value="SUBMIT FORM NUMBER ' . $cnt . '" />' . PHP_EOL;
    $htm .= '</form>';
}
$htm .= '</body></html>';

echo $htm;

Open in new window

0
 

Author Comment

by:hydrazi
ID: 36503782
Ok, I am finally back working on this after flooding emergencies!  

@helio, your example works for the date but my GCE Filenmber creation seems to be broken now.... I will comb through it and see if I can figure that out!  

@Ray_Paseur, thank you again.  I am already working on a new version of this system that will incorporate the things I have learned from your posts!  Very helpful!

0
 

Author Comment

by:hydrazi
ID: 36503870
@helio, on line 164, I think this is where my GCE Filenumbers are not listing correctly.  The number shows but not the $fid variable.  What do you think?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 82

Expert Comment

by:hielo
ID: 36507100
I don't see anything that stands out. On line 164 the values of those variables are "merged".  Put a "delimiter" in between. Instead of:
$fid . $sub

try:
$fid . '_'. $sub

Whatever appears to the left of the underscore will be your $fid.
0
 

Author Comment

by:hydrazi
ID: 36519075
So strange.  Dates work great with @hielo solution, but then my GCEFilenumbers do not work.  It gives no value to $fid and the numbering is totally wrong besides.  

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
ID: 36529991
try the attached code to see if it is executing the expected SQL statements and to verify that you get the correct number of items for each query:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Web Order Review and Insert</title>
	
	<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
	
</head>

<body>
	
	<div id="wrapper">

<?php

	include('conn.php');
		
	//******** Detect line endings on Macintosh **********//		
	ini_set('auto_detect_line_endings',TRUE);
	
	
	//******** Initialize the CSV file row to "0" **********//
	$row = -1;
	
	$cid_array = array('');
	
	$month_array = array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	
	
	//******** Open the CSV file (place filename here) **********//
	$handle = fopen("orders.csv", "r");
	
		
	echo '<form action="create_file.php" method="post">';
		
	while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
		++$row;
		//*********** Assign the CSV headers to array $tbl_headers **********//
		if($row == 0){
			$tbl_headers =  $data;	
			$temp=array_combine( $data, array_keys($data));
			extract(  $temp, EXTR_PREFIX_ALL, 'tblHeader_');
		}else{

			echo '<h1>Order #',$row,'</h1>';
			echo '<fieldset><ul>';
			
			//*********** Get CustomerID, Date from CSV file **********//
					$cid = $data[$tblHeader_CustomerID];
					$dp = substr($data[$tblHeader_date_submitted],0,10);
					$data[$tblHeader_Date] = $dp;

					$LoanMatureDate2 = $data[$tblHeader_LoanMaturedate];
					if($data[$tblHeader_LoanMaturedate] == ''){
						$data[$tblHeader_LoanMaturedate] = '0000-00-00';
					}

					$LoanMatureDate_V2 = $data[$tblHeader_LoanMatureDateV2];
					if(preg_match('/\d\d\-[A-Za-z][A-Za-z][A-Za-z]\-\d\d/', $LoanMatureDate_V2))
					{	
						$day = substr($LoanMatureDate_V2, 0, 2);

						$month = substr($LoanMatureDate_V2, 3, 3);
						
						foreach($month_array as $key2 => $value){
							
							if($month == $value){
								
								$month = $key2;
								
								if(strlen($month) == 1){
									
									$month = '0' . $month;
									
								}
								
							}
							
						}

						$year = '20' . substr($LoanMatureDate_V2, -2);

						$sql_date2 = $year . '-' . $month . '-' . $day;
												
						$data[$tblHeader_LoanMatureDateV2] = $sql_date2;
						
					}
					else if(preg_match('/\d\d\/\d\d\/\d\d\d\d/', $LoanMatureDate_V2))
					{
						$month = substr($LoanMatureDate_V2, 0, 2);
						$day = substr($LoanMatureDate_V2, 3, 2);
						$year = substr($LoanMatureDate_V2, -4);
						$sql_date2 = $year . '-' . $month . '-' . $day;
						$data[$tblHeader_LoanMatureDateV2] = $sql_date2;
					}

					if($data[$tblHeader_LoanMatureDateV2] == '')
					{
						$data[$tblHeader_LoanMatureDateV2] = '0000-00-00';
					}
			
			
			//*********** Retrieve, calculate, and display next GCE Number **********//
			$sql = "SELECT Filenumid FROM customerwebid WHERE `Customer ID` = '$cid'";
echo '<p>Line ',__LINE__,': sql=',htmlentities($sql,ENT_QUOTES),'</p>';
			$results = mysql_query($sql) or die(mysql_error());
echo '<p>Items Found: ', mysql_num_rows($results) ,'</p><hr />';
			$row_filenumid = mysql_fetch_assoc($results);
			$fid = $row_filenumid['Filenumid'];

			$sqlz = "SELECT MAX(`GCE File`) FROM filesthisyear WHERE `GCE File` LIKE '$fid%'";				
echo '<p>Line ',__LINE__,': sqlz=',htmlentities($sqlz,ENT_QUOTES),'</p>';
			$results = mysql_query($sqlz) or die(mysql_error());
echo '<p>Items Found: ', mysql_num_rows($results) ,'</p><hr />';
			$row2 = mysql_fetch_array($results);
			
			$k = 100;
			
			//*********** Determine last used GCE Number **********//
			if(in_array($fid, $cid_array)){				
				
				foreach($cid_array as $key => $value){					
					
					if($value == $fid){
						
						$k += 100;
						
					}
					
				}
				
				$cid_array[] = $fid;
				
			}else{
				
				$cid_array[] = $fid;
				
			}
			
			if($row2[0] != NULL){
				$sub = substr($row2[0], -6) + $k;		
			}else{
				$sub = '0' . date('y') + $k;
			}
			
			
			
			//*********** Make sure order always contains six numeric digits **********//
			if(strlen($sub) == 3){
				$sub = '000' . $sub;
			}elseif (strlen($sub) == 4) {
				$sub = '00' . $sub;
			}elseif (strlen($sub) == 5) {
				$sub = '0' . $sub;
			}
			
			//make_six($sub);
			
			//$gce_number = $fid . $sub;
			
			
			
			
			echo '<li><label><strong>GCE File Number</strong></label><input type="text" name="' . $row . '_gce" value="' . $fid . $sub . '" /></li>';				
					
			
			
			//*********** Create list items of each CSV field **********//
		    foreach($tbl_headers as $c=>$label) {
			
				echo '<li><label>' . $label . '</label>';
		
			    echo '<input type="text" value="' . htmlentities($data[$c],ENT_QUOTES) . '" name="' . $row . '_' . $label . '"/></li>';			
	
		    }
			
			echo '</ul></fieldset>';
		
		}
		
	}
		
	echo '<input type="submit" id="submit" value="Import Into Jobs"/>';
	
	
	//*********** Pass CSV header values to next script **********//
	echo '<input type="hidden" name="tbl_headers" value="' . implode(',', $tbl_headers) . '" />';
	
	echo '</form>';
		
	fclose($handle);
	
	function make_six($num){
		if(strlen($num) == 3){
			$num = '000' . $num;
		}elseif (strlen($num) == 4) {
			$num = '00' . $num;
		}elseif (strlen($num) == 5) {
			$num = '0' . $num;
		}
		
		return $num;
	}
	
	function make_date(){
		
	}
		

?>

</div>

</body>
</html>

Open in new window

0
 

Author Comment

by:hydrazi
ID: 36538532
Ok, tried that and it says the following:

Line 109: sql=SELECT Filenumid FROM customerwebid WHERE `Customer ID` = ''
Items Found: 0

Line 116: sqlz=SELECT MAX(`GCE File`) FROM filesthisyear WHERE `GCE File` LIKE '%'
Items Found: 1

So, something is now messing with the Customer ID field?  That's what my take is?  Thanks for all this help.  I find I learn a lot by doing things the right way, and also a lot by figuring out what is going wrong in something a little crazy like what I wrote!
0
 

Author Comment

by:hydrazi
ID: 36538564
So, it must be somewhere in the assignment of $cid.  
0
 
LVL 82

Accepted Solution

by:
hielo earned 300 total points
ID: 36549836
>>Line 109: sql=SELECT Filenumid FROM customerwebid WHERE `Customer ID` = ''
>>Items Found: 0
OK, clearly $cid is the problem, so the issue is here:

Do you see " ... WHERE `Customer ID` = '' Items Found: 0" for EVERY line of data in your CSV file or just for some?


If it is for every line of data, then most likely the problem is:
$cid = $data[$tblHeader_CustomerID];

which requires that the FIRST line in your csv file is the list of headers AND that it contains a header named
CustomerID with:
no spaces in between -ex:
 -> Customer ID (wrong - cannot have space between "Customer" and "ID")

the letter case IS case sensitive -ex:
 ->customerID (wrong - "c" must be upper case)
 ->CustomerId (wrong - "d" must be upper case)


If it is just for SOME of the lines, then try changing the code to:
$cid = $data[$tblHeader_CustomerID]; if(''==trim(strval($cid))) { continue; }
0
 

Author Comment

by:hydrazi
ID: 36551266
It's for every line.  I will take another look at the CSV files, but I am pretty sure that those conditions are correct.  Thanks for all this help.  You guys are awesome!
0
 
LVL 82

Expert Comment

by:hielo
ID: 36551905
also, if change line 44 from:
$tbl_headers =  $data;

to:
$tbl_headers =  $data; var_dump($data);

what do you see?
0
 

Author Comment

by:hydrazi
ID: 36560962
I will be trying this today, sorry for the delays!  
0
 

Author Comment

by:hydrazi
ID: 36569392
Here is what I am seeing from the dump:

array(34) { [0]=> string(7) "OrderID" [1]=> string(10) "CustomerID" [2]=> string(9) "Attention" [3]=> string(4) "Date" [4]=> string(5) "Buyer" [5]=> string(6) "Seller" [6]=> string(7) "Address" [7]=> string(4) "City" [8]=> string(5) "State" [9]=> string(3) "Zip" [10]=> string(6) "County" [11]=> string(4) "Book" [12]=> string(4) "Page" [13]=> string(7) "Plannum" [14]=> string(6) "Taxmap" [15]=> string(5) "Block" [16]=> string(3) "Lot" [17]=> string(9) "FileRefNo" [18]=> string(14) "LoanMaturedate" [19]=> string(6) "Lender" [20]=> string(12) "EstCloseDate" [21]=> string(9) "FloodNeed" [22]=> string(8) "PlotNeed" [23]=> string(4) "Need" [24]=> string(12) "LegalDescrip" [25]=> string(3) "P_S" [26]=> string(8) "Ord_Plan" [27]=> string(7) "Taxbill" [28]=> string(13) "Attachment_ID" [29]=> string(9) "type_form" [30]=> string(14) "date_submitted" [31]=> string(7) "Comment" [32]=> string(12) "ClientRefNum" [33]=> string(16) "LoanMatureDateV2" }
0
 
LVL 82

Expert Comment

by:hielo
ID: 36570583
Let's look at the first few lines of data read by fgetcsv.  If you change:
...
			//*********** Get CustomerID, Date from CSV file **********//
					$cid = $data[$tblHeader_CustomerID];
					$dp = substr($data[$tblHeader_date_submitted],0,10);
...

to:
...
			//*********** Get CustomerID, Date from CSV file **********//
					$cid = $data[$tblHeader_CustomerID];
					if(empty($cid) && $row<7)
					{
						var_dump($data);
					}
					$dp = substr($data[$tblHeader_date_submitted],0,10);
...

what do you see?

Open in new window

0
 

Author Comment

by:hydrazi
ID: 36570674
I am not sure why, but I point it to the number of the element and it worked!

$cid=$data[30]
0
 
LVL 82

Expert Comment

by:hielo
ID: 36571542
>>I am not sure why, but I point it to the number of the element and it worked!
>>$cid=$data[30]
according to your post (ID:36569392),
the first      column (index 0) is "OrderID"
the second column (index 1) is "CustomerID"

but now you say when you substitute 30 it works. Clearly the headers listed on the first line of your csv file do not match the order of the data.  In other words, it the CustomerID in the data rows appears in column 30, then your header "CustomerID" should also appear on the 30th column.

To clarify even further, basically your csv file currently is the equivalent of this:

"firstname","age","phone"
"(999)999-9999","John","23"
"(888)999-9999","Jane","21"

By simple inspection, it i clear that the
first column contains phone numbers
second column contains names
third column contains ages

So to fix it you need to "align" your headers so that they correspond to the correct data -ex:
"phone","firstname","age"
"(999)999-9999","John","23"
"(888)999-9999","Jane","21"
0
 

Author Comment

by:hydrazi
ID: 36587957
Sorry!  Wrong field.  I did this for fixing the date.  My previous code had worked for the creation of the GCE File Number.  So, I figured that the date coding had messed it up.  I changed the Date code to:


if($tbl_headers[$c] == 'Date'){
										
		$data[$c] = substr($data[30],0,10);
													
					
				}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now