sherric Posted October 6, 2009 Share Posted October 6, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/ Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 try this $items = array(); foreach($_POST['item'] as $item){ if (isset($item) && !empty($item){ $items[] = $item; } } Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-931939 Share on other sites More sharing options...
sherric Posted October 6, 2009 Author Share Posted October 6, 2009 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 ) ....... Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-931980 Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 thats because you are doing print_r every loop iteration. do a print_r after the loop is done Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932002 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 $items = array_filter($_POST['items']); Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932004 Share on other sites More sharing options...
sherric Posted October 7, 2009 Author Share Posted October 7, 2009 all of the above methods net me the items array - but not the values in the textboxes that are related to the items. btw a certain amount of snide is expected, as long as its attached to good info:) Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932048 Share on other sites More sharing options...
mikesta707 Posted October 7, 2009 Share Posted October 7, 2009 then you're textboxes don't have the right name or you are doing something else wrong, because i tested this and it works perfectly fine. post your code Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932052 Share on other sites More sharing options...
.josh Posted October 7, 2009 Share Posted October 7, 2009 you use "Item" in your form, but my code had "items". Did you change that to what it should be? Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932055 Share on other sites More sharing options...
sherric Posted October 7, 2009 Author Share Posted October 7, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932058 Share on other sites More sharing options...
sherric Posted October 7, 2009 Author Share Posted October 7, 2009 i set my Item = your items Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932064 Share on other sites More sharing options...
.josh Posted October 7, 2009 Share Posted October 7, 2009 Are you using the correct field names in your $row[...] 's (case sensitive)? Another thing I see that may be wrong is that you are using $i as a counter in two different places in your nested loops. Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932068 Share on other sites More sharing options...
sherric Posted October 7, 2009 Author Share Posted October 7, 2009 correct field names. changed second $i to $I - no change. Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932075 Share on other sites More sharing options...
.josh Posted October 7, 2009 Share Posted October 7, 2009 well, you are getting more than one "array" printed out, right? so that means at least one of your queries is succeeding. My guess is one of them is returning nothing or else otherwise failing. Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932079 Share on other sites More sharing options...
sherric Posted October 7, 2009 Author Share Posted October 7, 2009 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. ? Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932084 Share on other sites More sharing options...
DavidAM Posted October 7, 2009 Share Posted October 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932086 Share on other sites More sharing options...
sherric Posted October 7, 2009 Author Share Posted October 7, 2009 DavidAM - you are the man! i ran the code you suggested - minus the hidden fields, and sifted it to weed out the blank entries, and it works like a charm. many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/176744-retrieve-value-of-array-based-on-textbox-array/#findComment-932095 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.