Forum Moderators: coopster

Message Too Old, No Replies

Inserting two separate array values into the same row of a table.

         

ModernMerlin

12:58 am on Apr 16, 2012 (gmt 0)

10+ Year Member



I have two separate arrays coming from a form on the first page. When I go to the second page I use:


$uniquehash=$_POST['uniquehash'];
$spell_name2=$_POST['spell_name2'];

if(is_array($spell_name2)){
foreach ($spell_name2 as $sValue2) {
print $sValue2;
echo "    ";


To get the first array values and the same thing to get the second array values with the variable $knowledge2.

If I put the insert statement into the first foreach it will work properly but wont also insert the $knowledge2 variable obviously. I need to know how to get two arrays to insert into a table on the same row? The table has the primary key, user_id, spell_choice, level_knowledge. The last two being the arrays.

Any help would be appreciated! Thanks!

MM

ModernMerlin

12:59 am on Apr 16, 2012 (gmt 0)

10+ Year Member



Forgot to add the $knowledge2=$_POST['knowledge2']; at the top as well. Oops

ModernMerlin

1:55 am on Apr 16, 2012 (gmt 0)

10+ Year Member



Meaning I still need help. I just forgot to put that last bit on top of the my first post.

incrediBILL

2:31 am on Apr 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Assuming they are 100% synchronized and, one obvious approach is to count the rows and get the elements indexed in $knowledge2 based on the current row.

ModernMerlin

9:32 pm on Apr 16, 2012 (gmt 0)

10+ Year Member



I havent touched PHP in a few years so Im considering myself semi new again lol. Im not sure exactly what you mean by 100% synchronized?

eelixduppy

12:09 am on Apr 17, 2012 (gmt 0)



He means that both arrays are indexed in the same way. So $spell_name[0] matches up with $knowledge[0], $spell_name[1] matches with $knowledge[1], etc...

ModernMerlin

1:07 am on Apr 17, 2012 (gmt 0)

10+ Year Member



They do. But I dont know how to make the loop to insert that.

incrediBILL

7:52 am on Apr 17, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, try something like this where $row is the array index number:

if(is_array($spell_name2)){
foreach ($spell_name2 as $row=>$sValue2) {
print $sValue2;
print $knowledge[$row];
}

ModernMerlin

9:35 am on Apr 17, 2012 (gmt 0)

10+ Year Member



Thank you so much! Off to try that now...

ModernMerlin

8:27 pm on Apr 20, 2012 (gmt 0)

10+ Year Member



Sorry it took me so long to get back to you but it worked beautifully once I made sure the variables matched :) Thank you soooooo much!