Help with passing arrays between php scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Help with passing arrays between php scripts

Post by coolen »

Can anybody put me on the right track on how to pass arrays between php scripts.


I thought it would be something like:

$preselectvar = array();
echo '<form action="test.php" method="post">';
echo ' <table border=0>';
echo ' <input type="hidden" name="preselectvar[]" value="'.$preselectvar.'">';

for ($i=0; $i< $num_results; $i++)
{
echo ' <tr>';
$row = mysql_fetch_array($result);
$name = htmlspecialchars(stripslashes($row["Variable"]));
$units= htmlspecialchars(stripslashes($row["Units"]));
$preselectvar[]=$name;
echo ' <td>'.$name.'</td>';
echo ' <td> <select name="constraint[]">';
echo ' <option value="*" selected>*';
echo ' <option value="=">=';
echo ' <option value=">">>';
echo ' <option value="<"><';
echo ' <option value=">=">>=';
echo ' <option value="<="><=';
echo ' </select></td>';
echo ' <td><input type=text maxlength=10 size=10 name="value['.$i.']"</td>';
echo ' <td>'.$units.'</td>';
echo ' </tr>';
}

echo ' </table>';

echo ' <table border=0>';
echo ' <tr>';
echo ' <td colspan=2><input type=submit value="Submit">';
echo ' <input type=reset value="Reset"></td>';
echo ' </tr>';
echo ' </table>';
echo '</form>';
?>


and that I can use the two arrays in test.php with:

$constraint=$_POST("constraint");
$preselectvar=$_POST("preselectvar");


But this doesn't work in either case.

So where do I look, or what is the solution ???

thanx in advance
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

You need to specify "multiple" in the select line

Code: Select all

print("<SELECT MULTIPLE NAME=Names&#1111;] SIZE=15>\n");
     while ($row = mysql_fetch_array($Result))
          print("<OPTION value=$row&#1111;id]>$row&#1111;Name]</OPTION>\n");
     print("</SELECT>\n");
Last edited by Bill H on Fri Sep 20, 2002 8:58 am, edited 2 times in total.
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

That still doesn't give an answer to the question, It only says how to get multiple answers in a selection box, I know that, but I don't have multiple answers I only have
multiple selectionboxes., Multiple values, and multiple variables that I want to pass in 3 different arrays to the next script.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Sorry, your post was a bit unclear. I just realized all the selection box code you posted has nothing to do with your question. Or maybe it dies, since you are showing constraint to be an array. If it is an array then you need the "multiple" as shown. If it is not, what is the question about arrays?

After several readings, I'm not real clear on what your question is. Maybe somebody else can help you.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

In your statement of problem:
$constraint=$_POST("constraint");
$preselectvar=$_POST("preselectvar");
the parentheses '(' and ')' should be square brackets '[' ']'. Was this a typo in the post or is it also in the code?
Last edited by nielsene on Fri Sep 20, 2002 9:19 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To enable us to help you better could you please expand on:
But this doesn't work in either case.
Are there any error messages, what's happening/not happening that shouldn't/should happen?

Also disregard this if it was just a typo when you posted but if you copied and pasted this code from the file you're trying to run - have you had a look at your source code?
This:

Code: Select all

echo ' &lt;td&gt;&lt;input type=text maxlength=10 size=10 name="value&#1111;'.$i.']"&lt;/td&gt;';
will become

Code: Select all

&lt;td&gt;&lt;input type=text maxlength=10 size=10 name="value&#1111;1]"&lt;/td&gt;
you need to close the input tag:

Code: Select all

echo '&lt;td&gt;&lt;input type="text" maxlength="10" size="10" name="value&#1111;'.$i.']" /&gt;&lt;/td&gt;';
Mac
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

Sorry, I just realised I mad a lot of typos, and not alone in this post :O(

The () in stead of [] in _POST was a typo in the mail

Everything is working fine (I just needed to realise indeed that the hidden field needs to be in a loop to create a hidden entry for each preselectvar like :

for ($i=0; $i< $num_results; $i++)
{

echo ' <input type="hidden" name="preselectvar['.$i.']"value='.$preselectvar[$i].'>';
}


it is working now, but the only thing that puzzles me is now:

say the preselectvar has 6 entries:
1
2
3
4
5
6

then in the test.php script I get :

$preselectvar[]=$HTTP_POST_VARS["preselectvar"]

for ($i=0; $i < sizeof($preselectvar);$i++)
{
echo 'preselectvar['.$i.']: '.$preselectvar[$i].'<br>';
}

this does give me:
1
2
3
4
5
6
Array

This array also is an extra entry in the other arrays I pass.

is that always the case with arrays ??? thus should I discard the last entry in all cases. Or should I reconstruct the array in another way ?

Thanx
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

are you still doing

Code: Select all

echo ' <input type="hidden" name="preselectvar&#1111;]" value="'.$preselectvar.'">';
after your loop to populate it? If so that is what is adding the array entry at the end.
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

No,

I do as I showed in the post above yours:

for ($i=0; $i < sizeof($preselectvar);$i++)
{
echo 'preselectvar['.$i.']: '.$preselectvar[$i].'<br>';
}


But it does the same for the other two arrays. One in the loop as select and one in the loop as input type=text. Those two also have array entered as last entry
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

can you post the result of

print_r($preselectvar) on the processing page?
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

AARRRGGGHH,,

that does look a bit different then I expect :O(

Array ( [0] => Config [1] => nb_in1 [2] => nb_in2 [3] => nb_out1 [4] => squewing [5] => unfolding [6] => Array ( [0] => Config [1] => nb_in1 [2] => nb_in2 [3] => nb_out1 [4] => squewing [5] => unfolding ) ) conditions size: 7
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK, that's what I suspected, can you repost your complete, revised script that generates the form?
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

I did see however that if I do a :

print_r($HTTP_POST_VARS["preselectvar"]);

it does give me the expected output :


Array ( [0] => Config [1] => nb_in1 [2] => nb_in2 [3] => nb_out1 [4] => squewing [5] => unfolding )


So isn't the

preselectvar[]=HTTP_POST_VARS["preselectvar"];

the one that is wrong ???
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Massive Technique Instance Pre-Selector </title>
</head>

<body>
<h1> Massive Technique Instance Pre-Selector </h1>

<?php

if (! $db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}

mysql_select_db("exploration");

$technique_name=$HTTP_POST_VARS["technique_name"];

echo '<form action="tinsert_technique_values.php" method="post">';
echo ' <table border=0>';

echo ' <input type="hidden" name="technique_name" value="'.$technique_name.'">';

echo ' <tr>';
echo ' <td><strong>Give search constraints for:</strong></td>';
echo ' <td><strong>'.$technique_name.'</strong> </td>';
echo ' </tr>';

echo ' <tr><td><strong> "*" = any </strong></td></tr>';
echo ' <tr><td><strong> "=" = exact match </strong></td></tr>';
echo ' <tr><td><strong> ">" = all nr bigger then nr </strong></td></tr>';
echo ' <tr><td><strong> "<" = all nr smaller then nr </strong></td></tr>';
echo ' <tr><td><strong> ">=" = all nr bigger or equal then nr </strong></td></tr>';
echo ' <tr><td><strong> "<=" = all nr smaller or equal then nr </strong></td></tr>';
echo ' </table>';

$query = "select Variable,Units from technique_variables where Technique='".$technique_name."' and KindID=2 and (TypeID=2 || TypeID=3) ";
$result = mysql_query($query);
if (!$result)
{
echo " Error: something went wrong with query: ".$query;
exit;
}
$num_results = mysql_num_rows($result);

echo ' <table border=3 cols='.$num_results.' bgcolor="#CCCCCC" >';
for ($i=0; $i< $num_results; $i++)
{
echo ' <tr>';
$row = mysql_fetch_array($result);
$name = htmlspecialchars(stripslashes($row["Variable"]));
$units= htmlspecialchars(stripslashes($row["Units"]));
$preselectvar[]=$name;
echo ' <td>'.$name.'</td>';
echo ' <td> <select name="conditions['.$i.']">';
echo ' <option value="*" selected>*';
echo ' <option value="=">=';
echo ' <option value=">">>';
echo ' <option value="<"><';
echo ' <option value=">=">>=';
echo ' <option value="<="><=';
echo ' </select></td>';
echo ' <td><input type=text maxlength=10 size=10 name="values['.$i.']"</td>';
echo ' <td>'.$units.'</td>';
echo ' </tr>';
}

for ($j=0; $j< $num_results; $j++)
{

echo ' <input type="hidden" name="preselectvar['.$j.']"value='.$preselectvar[$j].'>';
}

echo ' </table>';

echo ' <table border=0>';
echo ' <tr>';
echo ' <td colspan=2><input type=submit value="Submit">';
echo ' <input type=reset value="Reset"></td>';
echo ' </tr>';
echo ' </table>';
echo '</form>';
?>

</body>
</html>
Last edited by coolen on Sat Sep 21, 2002 8:36 am, edited 1 time in total.
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

got it, (I think) it should indeed be

preselectvar=$HTTP_POST_VAR["preselectvar"];

I don't understand why I couldn't find anything about this in the manuals :O(


if this was the solution, please thanks to all that helped me,
Post Reply