Hello!

I am declaring a couple of variables in form input field as arrays, one is a textbox for user input, one is a display val from my db.

I DO NOT want or need a temp table (I think) for the user input!


VISUAL EXAMPLE OF FORM
THINK 'DIET' - i.e. tracking the calories you consumed today.
user is presented with list of foods and a blank textbox to enter how many they ate today:

USER INPUT DB ROW1 DB HIDDEN

of item eaten name of item calories per 1 of item

2nd item eaten name cals
3 rd item blank name of item cals
3rd item eaten
4th item blank and so forth........

user submits to page 2, program calcs cals for each item - ie. if dieter ate 2 bananas, each are 70 cals, total is 140
and displays only NON BLANK:

EATEN ITEMS NUMBER EATEN CALS FOR ALL
...........
..........
.........
TOTAL LINE, gives user total cals eaten today.


Each input value will therefore belong to that array, with key values of 0 through 93 (yes, it's a 2-dim array). NOTE: USER DOES NOT HAVE TO ENTER A VALUE, THAT WILL BE DISCARDED LATER.
So:

I'm posting both arrays, blank or otherwise, to page 2 of a form.
I want to match them up and then discard the user blanks.

ISSUE:
I need to put those 2 arrays together so that key 0 in arr1 matches key 0 in arr2. There are 93 keys in each table!

PROBLEM:
I understand I can't use array_merge or array_merge_recursive because this will just append one array to the other, since the key ID is numeric.

Is there any way to do this so I don't end up with sub-sub-arrays and all?

BTW, my code works.... it's just the arrays on the second page I'm having trouble with...

This is how I got the user input array (brackets omitted for viewing) on the first page

CODE SNIP
connect to db
query db
numrows=mysql_numrows($query)
for ($i=0; $i < $numrows; $i++) {
while ($dbrow = (mysql_fetch_assoc($query))){
$cnames = $dbrow["cname"];

then display an empty textbox for user input, the other is an array of row 'name' changed to variable arr2 values from my db which holds the variable for each row from db. also displaying that name on the page.

input brackets and table rows omitted so this displays on the forum:

INPUT TYPE=\"text\" NAME=\"uoils[]\" SIZE=\"30\">

INPUT TYPE=\"hidden\" NAME=\"arr2[]\" VALUE=\"$cnames\">$cnames</TD>

end loops
end form
post form


Okay, now we're at the second page.

I set a variable for each $_POST array:

$arrnew1 = $_POST['arr1'];

#get the array of the names
$arrnew2 = $_POST['arr2'];

#This is probably wrong - or would create a multi-dim array. from here, I'm stumped:

$pg2array = array(userval=>$arrnew1, nameval+>$arrnew2)

if I ask for the $key => val of $arrnew1 array I get keys 0 thru 93
if I ask for the same of $arrnew2, I also get keys 0 thru 93.

GOOD!!!

However, how do I put these 2 arrays together so that it's now set that the arrays appear like this

arraynew2 [0] value 1 IS CONNECTED TO arraynew2 value 1
arraynew2 [1] value 2 IS CONNECTED TO arraynew2 value 2
etc?

I'd think there would be some way to do this, but I know the array_merge won't work, it will just append, because the keys are numbers...

::sigh::

I LOVE this PHP stuff. This ain't an easy project to start learning with, though!

Any takers? :😁:
Strat

    sorry -

    input fields should have looked like THIS:

    INPUT TYPE=\"text\" NAME=\"arr1[]\" SIZE=\"30\"

    INPUT TYPE=\"hidden\" NAME=\"arr2[]\" VALUE=\"$cname\">$cname

    POST SHOULD HAVE RECEIVED ARRAYS arr1 and arr2 and have the variables set for them as described in my first post.

    apologies...

      aw. shoot ... and this was wrong...

      just so you know the code works, so far...

      this was wrong on page 2
      $pg2array = array(userval=>$arrnew1, nameval+>$arrnew2)

      this is what is really was:
      $pg2array = array(userval=>$arrnew1, nameval=>$arrnew2)

      sorry, sorry...

      Strat

        Write a Reply...