Jump to content

Recommended Posts

i have a table that displays a list of items and then a blank textbox to enter # used.  this is code:

 

//create table

$i = 0;

while ( $row = mysql_fetch_array($result2) ) {

$i++;

print "<tr>\n";

 

echo "<td>{$row['name']}</td>\n";

print "<td><input name=txtQty[] type=text/ size=5></td>";

print "<input type=\"hidden\" name=\"Item[]\" value='$row[name]' >\n";

echo "</tr>\n";

 

}//end while

 

 

with POST i can retrieve all values from Item[] - i need just ones with values, not blank ones.  and i need the $row['name'] value that is associated with the textbox.

 

what is the best way to gather this information?  any help would be great!

when i add this code:

 

$items = array();

foreach($_POST['Item'] as $item){

if ((isset($item))&& !empty($item)){

$items[] = $item;

print_r ($items);

 

 

what i get back is :

 

Array ( [0] => 14 ) Array ( [0] => 14 [1] => 15 ) Array ( [0] => 14 [1] => 15 [2] => 16 ) .......

 

thanks for the help, sorry if i'm not explaining myself very well.

 

my code:

if ( isset($_POST['submit']) ) { // if form is submitted, process it
$items = array();
foreach($_POST['Item'] as $item){
if ((isset($item))&& !empty($item)){
$items[] = $item;
}
}
print_r ($items);
}else {
echo "<form method='post' action=''>


with my textboxes being nested in a table

echo "<table style='position:absolute;left:1px;top:121;' ><tr><td></td></tr>
<tr><td>";


echo "<span id=spanEqUsed style=display: none; >";
if (($result)||(mysql_errno == 0)) 
{
	echo "<div class='notes' style='font-style:italic' align='left'><br/>
      Enter quantity of each type of equipment used. <br/>
      <br/><br/></div>";

	echo "<table width='100%'><tr valign=top>"; 
	if (mysql_num_rows($result)>0) 
	{ 
		//primary data 
		$i = 0; 
		while ($i < mysql_num_fields($result)) 
		{ 
			$i = 0; 
			while ( $row = mysql_fetch_array($result) ) { 
				$i++; 
				echo "<td align=center><strong>{$row['Name']}</strong>\n<br/><br/>";

				//nested data
				$result2 = mysql_query("select * from equipment where catid=".$row['ID'])or die(mysql_error());
				if ($result) { 
					print "<table width=200 border=0 class=style2>"; 


					//create table 
					$i = 0; 
					while ( $row = mysql_fetch_array($result2) ) { 
						$i++; 
						print "<tr>\n";

						echo "<td>{$row['name']}</td>\n";			
						print "<td><input name=txtQty[] type=text/ size=5></td>";
						print "<input type=\"hidden\" name=\"Item[]\" value=$row[id]>\n";
						print "<input type=\"hidden\" name=\"Name[]\" value=$row[name]>\n";
						echo "</tr>\n";

					}//end while 
					print "</table>\n"; 
				} else { 
					echo("<P>Error performing query: " . 
						mysql_error() . "</P>");

				} 
				echo "</td>";

			}

		}			

	} 
	echo "</tr>"; 
	echo "</table>";

i can get a list of textbox values and i can get an array of field items.  i cannot get them to correlate.

 

is there a way to combine the two arrays and then weed them out based on the items with 0 values?

 

i've tried array_merge, but it just tack the arrays end to end.  ?

If I understand correctly, you are putting out a bunch of text boxes for the user to enter a quantity, and you want to associate the quantity entered by the user with the appropriate "item".  The way you are going about it will probably work, but since different browsers seem to do whatever they want, I would not depend on it.  Maybe somebody will write a browser that chooses not to send empty text boxes.

 

What you have:

while ( $row = mysql_fetch_array($result2) ) {
                     $i++;
                     print "<tr>\n";

                     echo "<td>{$row['name']}</td>\n";         
                     print "<td><input name=txtQty[] type=text/ size=5></td>";
                     print "<input type=\"hidden\" name=\"Item[]\" value=$row[id]>\n";
                     print "<input type=\"hidden\" name=\"Name[]\" value=$row[name]>\n";
                     echo "</tr>\n";

                  }//end while 

when posted will give you three arrays in $_POST.  They are "txtQty", "Item", and "Name".  Choose one of the keys from any of the arrays, and use the same key to access the associated entries from the other arrays:

 

txtQty[1] is the quantity for Item[1] with Name[1]

 

However, just to be safe, when I do this, I force the unique ID from the database as the key to the index of data I want:

while ( $row = mysql_fetch_array($result2) ) {
                     $i++;
                     print "<tr>\n";

                     echo "<td>{$row['name']}</td>\n";         
                     print "<td><input name=txtQty[{$row['id']}] type=text/ size=5></td>";
                     print "<input type=\"hidden\" name=\"Item[{$row['id']}]\" value=$row[id]>\n";
                     print "<input type=\"hidden\" name=\"Name[{$row['id']}]\" value=$row[name]>\n";
                     echo "</tr>\n";

                  }//end while 

 

this will force the key to all three arrays within the $_POST array to be the ID from the database so I can then go straight to the database with something like this:

foreach ($_POST['txtQty'] as $key => $value) {
    $sql = "UPDATE someTable SET qtyCOl = {$value} WHERE id = '$key'";
    // then execute the sql statement 
}

actually, if you use this method, the hidden fields are not necessary at all.

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.