Jump to content


Photo

$_POST an array or dereference a string


  • Please log in to reply
4 replies to this topic

#1 ann

ann
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 29 August 2006 - 05:13 PM

Hi

I have a from with 5X7 text input fields. (see http://trichuris.cap...earch_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
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>";
	}
}
 

which gives the ouput (if the top left input (A00) is aaa)

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


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

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 August 2006 - 06:04 PM

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.

Info: PHP Manual


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 August 2006 - 06:11 PM

Here's one way

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

<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 ann

ann
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 29 August 2006 - 06:58 PM

Thanks, much appreciated.

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

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 August 2006 - 07:06 PM

It just passes the fields in a 2D array whose keys are [element][N].

So you could

$fields = $post['A'];

then first search value for Cadmium is in $fields['Cadmium'][0];
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users