ann Posted August 29, 2006 Share Posted August 29, 2006 HiI have a from with 5X7 text input fields. (see http://trichuris.cap.ed.ac.uk/Lumbribase/search_array.php)I want to be able to iterate through the input fields after they are posted.Idealy I'd lke to store the input in a 2D array and post the array but I don't think that's possible.So I named them A00 to A46 and post them to the next php page. How do I get them into a form I can itterate through?I'm trying this[code]print "A00 -> $A00<br>";for($i=0; $i<7; $i++) { for ($j=0; $j<5; $j++) { $tmp="A".$i.$j; $junk[$i][$j]=$_POST[$$tmp]; print "i=$i -> j=$j -> tmp=$tmp -> junk[i][j]=$junk[$i][$j] -> Aij=$A$i$j<br>"; }}[/code] which gives the ouput (if the top left input (A00) is aaa)[code]A00 -> aaai=0 -> j=0 -> tmp=A00 -> junk[i][j]=Array[0] -> Aij=00i=0 -> j=1 -> tmp=A01 -> junk[i][j]=Array[1] -> Aij=01i=0 -> j=2 -> tmp=A02 -> junk[i][j]=Array[2] -> Aij=02i=0 -> j=3 -> tmp=A03 -> junk[i][j]=Array[3] -> Aij=03i=0 -> j=4 -> tmp=A04 -> junk[i][j]=Array[4] -> Aij=04i=1 -> j=0 -> tmp=A10 -> junk[i][j]=Array[0] -> Aij=10i=1 -> j=1 -> tmp=A11 -> junk[i][j]=Array[1] -> Aij=11<snip>i=6 -> j=2 -> tmp=A62 -> junk[i][j]=Array[2] -> Aij=62i=6 -> j=3 -> tmp=A63 -> junk[i][j]=Array[3] -> Aij=63i=6 -> j=4 -> tmp=A64 -> junk[i][j]=Array[4] -> Aij=64[/code]So how to dereference $tmp so that $_POST[$$tmp]; actually gets the variable with the name stored in the string $tmp?Thanks if you can helpAnn Quote Link to comment https://forums.phpfreaks.com/topic/19040-_post-an-array-or-dereference-a-string/ Share on other sites More sharing options...
ober Posted August 29, 2006 Share Posted August 29, 2006 Maybe the better question is to ask how you are naming your fields on the form. You can pass arrays and even multi-dimensional arrays through $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/19040-_post-an-array-or-dereference-a-string/#findComment-82353 Share on other sites More sharing options...
Barand Posted August 29, 2006 Share Posted August 29, 2006 Here's one wayI just did the first 2 compunds to illustrate the naming convention[code]<?phpif (isset($_POST['submit'])) { foreach ($_POST['A'] as $compound => $searcharray) { echo "$compound : <br/>"; foreach ($searcharray as $n => $val) { echo "$n : $val <br/>"; } }} ?><FORM method='post'>'<table valign=top border=0 cellpadding=5 cellspacing=0><tr><td colspan=2 bgcolor="#AAAAAA" align=center><font color="#FFFFFF">Select Individual Libraries</td></tr><tr><td bgcolor=#AAAAAA valign=top><table><tr valign=top><td height=20 width=100><font color="#FFFFFF">compound</td></tr><tr valign=top><td height=20 width=100><font color="#FFFFFF">Search0</td></tr><tr valign=top><td height=20 width=100><font color="#FFFFFF">Search1</td></tr><tr valign=top><td height=20 width=100><font color="#FFFFFF">Search2</td></tr><tr valign=top><td height=20 width=100><font color="#FFFFFF">Search3</td></tr><tr valign=top><td height=20 width=100><font color="#FFFFFF">Search4</td></tr></table></td><td bgcolor="#AAAAAA" align=center><table><tr><td valign=top bgcolor="#AACAAA"><table><tr valign=top><td align=center height=20><font color="#FFFFFF">Atrazine</td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Atrazine][0]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Atrazine][1]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Atrazine][2]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Atrazine][3]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Atrazine][4]"></td></tr></table></td><td valign=top bgcolor="#AACAAA"><table><tr valign=top><td align=center height=20><font color="#FFFFFF">Cadmium</td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Cadmium][0]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Cadmium][1]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Cadmium][2]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Cadmium][3]"></td></tr><tr valign=top><td align=center height=20><input type="text" size="4" name="A[Cadmium][4]"></td></tr></table></tr></table><BR/><input type="submit" name="submit" value="Search"></FORM>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19040-_post-an-array-or-dereference-a-string/#findComment-82358 Share on other sites More sharing options...
ann Posted August 29, 2006 Author Share Posted August 29, 2006 Thanks, much appreciated.I'm not sure how it work but it does and at the moment that's what matters. :) Quote Link to comment https://forums.phpfreaks.com/topic/19040-_post-an-array-or-dereference-a-string/#findComment-82384 Share on other sites More sharing options...
Barand Posted August 29, 2006 Share Posted August 29, 2006 It just passes the fields in a 2D array whose keys are [element][N].So you could[code]$fields = $post['A'];[/code]then first search value for Cadmium is in $fields['Cadmium'][0]; Quote Link to comment https://forums.phpfreaks.com/topic/19040-_post-an-array-or-dereference-a-string/#findComment-82390 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.