Jump to content

Issue reading text file properly.....


ismoore

Recommended Posts

Hi,

I'm having some problems getting my form to read a text file properly.  Here is what the text file looks like upon creation:

 

12006GD16P 37
13406BP16H 217
13406BP16P 197
13406BP16T 516
13406BP16W 75
13406BP16Y 0
13406BP17D 171
13406BP17H 33
13406BP17N 191
13406BP17P 0
13406BP17T 43
4176B76H01 30
4176B76H06 100
4176B76H07 550
4180B53H02 200
4180B53H03 446
4180B53H06 75
4180B53H07 200
6563C02H01 350
6563C02H02 165
6563C04H01 170
6563C04H02 75
 

 

 

Now, here's the code I have to read this text file and place onto my form:
 
<?php
 
$filename = "/home/xxxxx/public_html/inventory/xxxxx.txt";
$fd = fopen($filename,"r");
$contents = fread ($fd,filesize ($filename));
fclose($fd);
 
function explode_assoc($glue1, $glue2, $array)
{
  $array2=explode($glue2, $array);
  foreach($array2 as  $val)
  {
            $pos=strpos($val,$glue1);
            $key=substr($val,0,$pos);
            $array3[$key] =substr($val,$pos+1,strlen($val));
  }
  return $array3;
}
 
//usage:
$inventoryarray=explode_assoc(" ","\n",$contents);
 
?>

 

 

Now the above information works as far as reading the text file and posting the information on the form.  The problem is when I submit the form, the order isn't being saved to the MySql Database.  I know it's a text file issue because if I open the text file and click save (keep in mind, I'm not making any changes), the format of the text file changes to one complete block of text instead of separated lines as such:
 
12006GD16P 3713406BP16H 21713406BP16P 19713406BP16T 51613406BP16W 7513406BP16Y 013406BP17D 17113406BP17H 3313406BP17N 19113406BP17P 013406BP17T 434176B76H01 304176B76H06 1004176B76H07 5504180B53H02 2004180B53H03 4464180B53H06 754180B53H07 2006563C02H01 3506563C02H02 1656563C04H01 1706563C04H02 75

 

 

If I use the text file as shown above, all as one block of text, the form will submit fine and store to the database.  The only problem now is the form doesn't appear properly because it recognizes this as 1 line instead of several.

 

Here is what the form looks like when reading the text file with proper formatting (but doesn't save to database):

 

y6b6.png

 

Here is what the form looks like when reading the text file with improper formatting (but saves properly to database):

 

lt2f.png

 

Anyone have any thoughts on what I'm doing wrong?  Thanks in advance for your help.

Edited by ismoore
Link to comment
Share on other sites

What is code you're using to save the contents to the text file? When you're saving to the text file you are most probaly not including the newlines.

 

The code for reading the text file could be simplified as

$textfile = "/home/xxxxx/public_html/inventory/xxxxx.txt";
 
function explode_assoc($filename)
{
	$data = file($filename, FILE_IGNORE_NEW_LINES);

	$array = array();
	foreach($data as $line)
	{
		list($key, $value) = explode(" ", $line);
		$array[$key] = $value;
	}

  	return $array;
}
 
//usage:
$inventoryarray = explode_assoc($textfile);

To write the inventory array to the text file

function write_inventory($filename, $data_array)
{
	$data = '';

	foreach($data_array as $key => $value)
	{
		$data .= "$key $value\n";
	}

	file_put_contents($filename, $data);
}

write_inventory($textfile, $inventoryarray);
Link to comment
Share on other sites

This is a brief sample from me how to make multiple inserting values in the database.

 

insert.php

<?php
 
$filename = "posts.txt";
$fd = fopen($filename,"r");
$contents = fread ($fd,filesize ($filename));
fclose($fd);
 
function explode_assoc($glue1, $glue2, $array)
{
  $array2=explode($glue2, $array);
  foreach($array2 as  $val)
  {
            $pos=strpos($val,$glue1);
            $key=substr($val,0,$pos);
            $array3[$key] =substr($val,$pos+1,strlen($val));
  }
  return $array3;
}
 
//usage:
$inventoryarray=explode_assoc(" ","\n",$contents);
 
?>

<table border="0" cellpadding="2">
    <thead>
    <th align="left">PartNumber</th>
    <th align="left">PartDescription</th>
    <th align="left">CurrentInventory</th>
    <th align="left">OrderQuality</th>
    </thead>
    
    <form method="post" action="post_data.php">
        <?php foreach ($inventoryarray as $key => $value): ?>
    <tr>
        <td><input type="text" style="border: none;" name="partNum[]" value="<?php echo $key; ?>" /></td>
        <td><input type="text" style="border: none;" name="PartDesc[]" value="BAR BUS" /></td>
        <td><input type="text" name="currentInv[]" style="border: none;" value="<?php echo $value; ?>"</td>
        <td><input type="text" name="someName[]" value="0" /></td>
    </tr>
    <?php endforeach; ?>
    
    <tr><td><input type="submit" value="Go!" /></td></tr>
        
    </form>
    
</table>

posts.txt

12006GD16P 37
13406BP16H 217
13406BP16P 197
13406BP16T 516
13406BP16W 75
13406BP16Y 0
13406BP17D 171
13406BP17H 33
13406BP17N 191
13406BP17P 0
13406BP17T 43
4176B76H01 30
4176B76H06 100
4176B76H07 550
4180B53H02 200
4180B53H03 446
4180B53H06 75
4180B53H07 200
6563C02H01 350
6563C02H02 165
6563C04H01 170
6563C04H02 75

posts_data.php

<?php

//print_r($_POST); exit; 
// build query...
$sql = "INSERT INTO `tbl_name`";

// implode keys of $array...
$sql .= " (`" . implode("`, `", array_keys($_POST)) . "`) VALUES";

// count an array keys
$count = count($_POST['partNum']);

// loops values of 
// multiple dimensional array;

$i=0;

while ($i < $count):
    
    $sql .="(";

    foreach ($_POST as $col) {
        $sql .= "'" . $col[$i] . "',";
    }
    // trim the commas between sections
    $sql = rtrim($sql, ',');
    
    $i++;
    
    $sql .= "),";
endwhile;

// trim the last comma from the string
$sql = rtrim($sql, ',');


//execute the query to db
//mysql_query($sql, $conn);

echo $sql; 


RESULTS:

 

 

INSERT INTO `tbl_name` (`partNum`, `PartDesc`, `currentInv`, `someName`) VALUES('12006GD16P','BAR BUS','37','0'),('13406BP16H','BAR BUS','217','0'),('13406BP16P','BAR BUS','197','0'),('13406BP16T','BAR BUS','516','0'),('13406BP16W','BAR BUS','75','0'),('13406BP16Y','BAR BUS','0','0'),('13406BP17D','BAR BUS','171','0'),('13406BP17H','BAR BUS','33','0'),('13406BP17N','BAR BUS','191','0'),('13406BP17P','BAR BUS','0','0'),('13406BP17T','BAR BUS','43','0'),('4176B76H01','BAR BUS','30','0'),('4176B76H06','BAR BUS','100','0'),('4176B76H07','BAR BUS','550','0'),('4180B53H02','BAR BUS','200','0'),('4180B53H03','BAR BUS','446','0'),('4180B53H06','BAR BUS','75','0'),('4180B53H07','BAR BUS','200','0'),('6563C02H01','BAR BUS','350','0'),('6563C02H02','BAR BUS','165','0'),('6563C04H01','BAR BUS','170','0'),('6563C04H02','BAR BUS','75','0')

 

Create your database table with auto_increment id field and try to insert the data.

 

Note; There's a lot more you should be doing (sanitize data, etc....), but I've not the time to get into all of it right now.

Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.