Text box with multiple records

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Text box with multiple records

Post by SpiritWebb »

I was wondering if anyone can point me in the right direction. I wanted to create a text box in php where you can input data, separated by commas and the script creates a record for each one.

For example:

I scan multiple bar codes, and in the text box each bar code is its own line. Now when I move to the drop down box it auto separates each with a comma, then in the drop down I can choose the location of where it is, and when I click the "add" button it then creates a new record for each new bar code added to the database. That way, when updating the bar code its "location" can be changed.

Any idea on how to do this? I am lost...

Thanks in advance!
Image

Image
User avatar
Chris
Posts: 1582
Joined: Wed Sep 30, 2009 7:22 pm

Re: Text box with multiple records

Post by Chris »

the explode() function is what you need.
http://php.net/explode

Here's sort of what your code would look like

Code: Select all

foreach( explode(",", $_POST['textbox']) as $key => $value )
{
    echo $key ." ". $value . "<br />";
}
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Text box with multiple records

Post by SpiritWebb »

I was wondering if this will work?

Code: Select all

# <?php
# // This is the string, delimited by a comma, rather than a new-line.
# $rawString = "piece1,piece2,piece3";
#  
# // Split the string into pieces
# $pieces = explode(",", $rawString);
#  
# // Build the top of the INSERT query
# $sql = "INSERT INTO `myTable`(`myField`) VALUES\n";
#  
# // Build the rest of the INSERT query by re-assembling the
# // pieces.
# $sql .= "('";
# $sql .= implode("'), ('", $pieces);
# $sql .= "')"; 
# ?>
Then the INSERT INTO should look like:

Code: Select all

# INSERT INTO `myTable`(`myField`) VALUES
# ('piece1'), ('piece2'), ('piece3')
I was wondering if this will work? However, if piece1, piece2 and piece3 there in the $rawstring am I only limited to 3 lines in the multiline text box? Due to what we do at work, I would need at least a 200 line limit. When adding data to the database...

Then when the info is added, how can I use the multi-line textbox to do a search on the database and update the record using a drop down box? Please advise...I am needing to get this done as quickly as possible...
Image

Image
User avatar
Lord Strife
Posts: 104
Joined: Wed Oct 28, 2009 3:10 pm

Re: Text box with multiple records

Post by Lord Strife »

might work ;)

Code: Select all


<?php
if(isset($_POST['submit']) AND $_POST['textarea'] != '' AND $_POST['location'] != ''){
	$info  = $_POST['textarea'];
	$location = $_POST['location'];
	$tapes = explode("\n", $info);
	/*foreach ($tapes as $tapenumber) {
		$sql = "INSERT INTO tapes(id, tapenumber, location) VALUES('',$tapenumber,$location)";
	}*/
	foreach ($tapes as $tapenumber) {
		echo $tapenumber;
		echo "<br/>";
	}
	echo $location;
}
else{
?>
<form action="test.php" method="post">
<textarea name="textarea" col="30" rows="10"></textarea>
<br />
<select name="location">
<option value="usa">Usa</option>
<option value="mexico">Mexico</option>
</select>
<br />
<input type="submit" name="submit" value="TEST">
</form>
<?php
}
?> 

User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Text box with multiple records

Post by SpiritWebb »

What I have now:

Code: Select all

    <center>
    <h1><p><center>Conway ODC TMS - New Tapes</center></p></h1> 
<?php
include 'connect.php';
?>

<?php
if(isset($_POST['submit']) AND $_POST['textarea'] != '' AND $_POST['location'] != ''){
   $info  = $_POST['textarea'];
   $location = $_POST['location'];
   $tapes = explode("\n", $info);
   foreach ($tapes as $tapenumber) {
      $sql = "INSERT INTO tapes(id, tapenumber, location) VALUES('',$tapenumber,$location)";
      mysql_query($sql) or die("could not update database" . mysql_error());
   }

}
else{
?>
<form action="tapeenter.php" method="post">
<textarea name="textarea" col="30" rows="10"></textarea>
<br />
Tape Location:
<select name="location">
<option selected>
<option>tape drive cwy
<option>tape drive lr
<option>Tape Storage Room
</select>
<br />
<input type="submit" name="submit" value="TEST">

</form>
<?php
}
?>
Image

Image
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Text box with multiple records

Post by SpiritWebb »

Thanks to Lord Strife, it is FULLY FUNCTIONAL now...WOOT! Thank you so much! Life saver!
Image

Image
User avatar
Lord Strife
Posts: 104
Joined: Wed Oct 28, 2009 3:10 pm

Re: Text box with multiple records

Post by Lord Strife »

no problem , anytime
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Text box with multiple records

Post by SpiritWebb »

The goal at which I am trying to accomplish is a fully working tape management system. Where basically you use the database to do the following:

Add new tapes (tape number, location, library)
Update tapes (keep the tape number and library the same, but change the location)
Search tapes (Be able to use a field to put x amount of tape numbers and do a search. Show the tape numbers and location of said tape(s))
Delete tapes (Be able to put a tape number(s) in a delete the volumes from the database - admin function)

And a few others, but that's what I am trying to fully accomplish. Then being able to upgrade it to do library searches to show who the customer is, the DBA's, their location, username/password if any, etc.


Thanks to Lord Strife, the Add new tapes function is working successfully, however the updating tapes doesn't work properly. Search and Delete isn't built yet.
Image

Image
Post Reply

Return to “Advanced Help and Support”