Good Morning,
I'm really struggling with an upload issue, and am hoping that someone here has an answer to it, so I won't go completely bald working on it..
I am trying to import the contents of a .csv file into a database, but want to give the uploading party the option to map which fields in the .csv file belong to which fields in the DB table.
I have the browse / file select working fine, and the display of the first record in the file working fine, and displayed so they can make the mapping selections.
But what I can't seem to do is to bring the name of the .csv through to the third screen, which is where I will do the DB inserts based on their selections.
The variable $uploadfile is used on the first screen in the browse to locate the correct .csv file. Then on the second screen I open that file and assign it to the variable $fp. using fgetcsv, I extract the first record and display it for the mapping process.
BUT, when I get to the third screen I have lost the connection to the .csv as well as the filelocation and can't seem to get to the rest of the data.
I'm assuming it is something simple and obvious that I am missing, but can't for the life of me figure it out. Any direction you could provide would be greatly appreciated..
I've thought about just taking the raw .csv file and writing it to a temporary table at the same time of the mapping selection, then just transferring the data from that table to where it belongs based on the mapping selections but it seem to me that there should be a better way to do it.
Here is the basic code being used
thanks
doug
<?
}elseif($section=='upload') { // Displays Screen 1
// This is where the .csv file is selected using the browse button
?>
<form name="form1" method="post" action="<?=$PHP_SELF?>" enctype="multipart/form-data">
<input type="hidden" name="section" value="mapping">
<input type="file" name="uploadfile" size="35">
<input type="submit" name="sendbtn" value=" Next >> ">
</form>
<?
}elseif($section=='mapping') { // Displays Screen 3
$fp = fopen ("$uploadfile","r");
if (!$fp) {
echo "<p>Unable to open remote file.</p>";
exit;
}
// retrieve first record and display with mapping options
if (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
$count=count($data);
?>
<form name="mapping" method="post" action="<?=$PHP_SELF?>">
<input type="hidden" name="section" value="write_to_db">
<input type="hidden" name="fp" value="<?=$fp?>">
<table width="90%" border="0" cellspacing="10" cellpadding="0" align="center">
<?
for ($x=0; $x<$count; ++$x){
?>
<tr class="text1" bgcolor="#FFFFFF">
<td width="50%" align="right">
<div align="right"><b>
<?=$data[$x]?>
</b></div>
</td>
<td width="50%" align="left">
<div align="center">
<select size="1" class="text1" name="mapitem<?=$x?>" onChange="return(checkvalue( this ))">
<option value="nomap">Don't Map This Field</option>
<option value="email">Email Address</option>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
<option value="address1">Street Address 1</option>
<option value="address2">Street Address 2</option>
<option value="city">City</option>
<option value="state">State / Province</option>
<option value="zip">Zip / Postal Code</option>
<option value="phone">Phone Number</option>
<option value="fax">Fax Phone Number</option>
<option value="ip">IP address</option>
<option value="optin">Optin Date Time</option>
</select>
</div>
</td>
</tr>
<?
}
?>
<tr class="text1" bgcolor="#FFFFFF">
<td colspan="2" align="center">
<input type="submit" name="sendbtn" value=" Next >> ">
</td>
</tr>
</table>
</form>
<?
}
}elseif($section=='write_to_db') { // Displays Screen 3
// this is where the records would be written to the DB, but just displaying them on the screen
// I've tried bringing the $fp and the $uploadfile using hidden fields, and neither seems to be right
if (!$fp) {
echo "<p>Unable to open remote file.</p>";
exit;
}
$row=0;
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
$count = count($data);
$row++;
echo "<p> $count fields in line $row: <br /></p>\n";
for ($c=0; $c < $count; $c++) {
echo $data[$c] . "<br />\n";
}
}
?>