Forum Moderators: coopster

Message Too Old, No Replies

array from post, maybe needs to be multidimensional?

         

mgworek

7:27 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



I am new to arrays. I was able to create an array from a form and echo back the values.

But i need to have 3 fields on a form go together, 15 times.

example, name email cell but the form will have 15 lines of that.

do i have seperate array for each field or make them multidimensional?

name[], email[], cell[] or do i need something[name], something[email].............

Hope this makes sense.

Thank you.

PHP_Chimp

8:09 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using a method of POST for your forms then using email[] as the name/id for the input will give you -
$_POST[email][], so you have a multidimensional array anyway.
This is probably what you want, as it should be easy for you to iterate through that array.

mgworek

8:28 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



If I keep them seperate like that, how would I tie the arrays together?

Like I said, I am new to them and trying to figure out.

I dont want to issue 15 different mysql insert commands, would rather just write a loop to do it with the arrays.

Thanx for the reply.

PHP_Chimp

2:16 pm on Nov 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you explain a little more about the form structure. As are the 15 email addresses for different people?
So would you have -
name[0] = 'bob';
email[0] = 'bob@example.com'; // bobs email address
cell[0] = 0000000000;
name[1] = 'fred';
email[1] = 'fred@example.com'; // freds email address
cell[1] = 11111111111;

As then you can like everything together using the numeric keys.

It may help you to build a test array with the data you want and run it through the code below. As this will help you to see what the contents of that array looks like.


<?php
$test = array(array('bob', 'bob@example.com', '000000000'), array('fred', 'fred@example.com', 111111111));
// cell number as a string so you dont just get 0
$test1 = array('bob' => array('bob@example.com', '000000000'), 'fred' => array('fred@example.com', 111111111));
echo '<pre>TEST<br />';
[url=http://uk3.php.net/manual/en/function.print-r.php]print_r[/url]($test);
echo '</pre>';
echo '<pre>TEST1<br />';
print_r($test1);
echo '</pre>';
?>

Your loop could look something like -


foreach ($test as $v) {
$q = "INSERT INTO <table_name> (name, email, cell) VALUES $v[0], $v[1], $v[2];";
// mysql_query
}