Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populate array

Status
Not open for further replies.

dingleberry

Programmer
Dec 13, 2002
143
US
Hi,

This is such a trivial request I'm a little embarrased to ask it but here goes. I've got a form element which is a multiple select list. It looks something like this...

<form name=&quot;resume2&quot; method=&quot;post&quot; action=&quot;insert_resume2.php&quot;>

<select name=&quot;interests[]&quot; size=&quot;6&quot; multiple>
<option value=&quot;Accounting&quot;>Accounting</option>
<option value=&quot;Admin-Clerical&quot;>Admin-Clerical</option>
<option value=&quot;Banking&quot;>Banking</option>
<option value=&quot;Biotech&quot;>Biotech</option>
<option value=&quot;Broadcast Journalism&quot;>Broadcast Journalism</option>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Next &gt;&quot;>

and in insert_resume2.php I do a

$interests = $_POST['interests'];

echo $interests[0];
echo $interests[1];
...
but all I get when I execute this is the word &quot;array&quot; and not the individual elements. I need the individual elements but am so new to php I don't know how to get them. I know I can print them with

foreach ($HTTP_POST_VARS[&quot;interests&quot;] as $value)
{
print &quot;$value&quot;;
}

but I still don't know how to get each individual element so that I can use it to populate a database. I'm very sorry this is so trivial but I'm really getting blue over this one. Any pointers?

Thanks,
Dan
 
If you are ever in doubt as to how a variable is structured, use print_r.

i.e: print_r($_POST); --BB
 
okay, I did that and it output the following...

Array([0] => 'first' [2] => 'second' [3] => 'third')

And in the form, I did select first, second, and third so success. However when I try to echo $interests[0]; it outputs the word array. ugghh. In perl, for instance, I could do a

foreach ($variable[@interests])
{
@newarray .= $variable;
$variable();
}

and later do a print $newarray[0];
and so on to print the individual elements. Now, I'm not even trying to print the elements, I'm simply trying to obtain the individual elements from the array so I can use insert them into a database. Any suggestions?

Thanks,
Dan
 
again, do a print_r... I'm not sure what's happening here... but you're just getting an array of arrays.

Here's what I would put at the bottom.

echo &quot;<pre>&quot;;
print_r($interests);

echo &quot;<hr>&quot;;
foreach ($interests as $interest) {
print_r($interest);
echo &quot;<hr>&quot;;
}

I have a feeling you're not assigning things right somewhere... in fact I'd guess that interests will only have one element which is an array, and that'll be the problem, but try that and see if I'm right or not.

-Rob

 
It's hard to advise in such general terms. Your example HTML could not provide the output you describe. It could be something as simple as a typographical error in the code you are actually running versus the code your are telling us about

Here's my test code:
test_formname.html:
Code:
<html><body>
<form method=post action=test_formname.php>
	<select name=&quot;foo[]&quot; size=3	multiple>
		<option value=one>1</option>
		<option value=two>2</option>
		<option value=three>3</option>
		<option value=four>4</option>
		<option value=five>5</option>
	</select>
	<input type=submit>
</form></body></html>

test_formname.php:
Code:
<?php
print '<pre>';
$foo = $_POST[&quot;foo&quot;];
print_r ($foo);
print &quot;\n&quot;;
print $foo[0];
?>

If I point my browser to test_formname.html, select displayed values 1 and 3, and submit, test_formname.php outputs:
Code:
Array
(
    [0] => one
    [1] => three
)
one

Which is expected behavior. Want the best answers? Ask the best questions: TANSTAAFL!
 
You guys are both right... I'm barking up the wrong tree. I tried

foreach ($interests as $interest) {
print_r($interest);
echo &quot;<hr>&quot;;
}
echo $interests[0];

and it returned what it was supposed to. Must be something in my mysql query. Back to the drawing board....

D'oh!
Thanks a ton for your help!
dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top