Jump to content

Archived

This topic is now archived and is closed to further replies.

ann

$_POST an array or dereference a string

Recommended Posts

Hi

I 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 -> aaa
i=0 -> j=0 -> tmp=A00 -> junk[i][j]=Array[0] -> Aij=00
i=0 -> j=1 -> tmp=A01 -> junk[i][j]=Array[1] -> Aij=01
i=0 -> j=2 -> tmp=A02 -> junk[i][j]=Array[2] -> Aij=02
i=0 -> j=3 -> tmp=A03 -> junk[i][j]=Array[3] -> Aij=03
i=0 -> j=4 -> tmp=A04 -> junk[i][j]=Array[4] -> Aij=04
i=1 -> j=0 -> tmp=A10 -> junk[i][j]=Array[0] -> Aij=10
i=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=62
i=6 -> j=3 -> tmp=A63 -> junk[i][j]=Array[3] -> Aij=63
i=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 help

Ann

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Here's one way

I just did the first 2 compunds to illustrate the naming convention

[code]<?php
if (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]

Share this post


Link to post
Share on other sites
Thanks, much appreciated.

I'm not sure how it work but it does and at the moment that's what matters.  :)

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites

×

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.