3 days - so I'll ask - arrays

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

Post Reply
ppgpilot
Forum Newbie
Posts: 4
Joined: Wed Apr 25, 2007 4:55 pm

3 days - so I'll ask - arrays

Post by ppgpilot »

hi

I have a multi checkbox entry that I want to place into a mysql database.

I then need to pull the array data out of the db and display into a <select> box.

I know this is easy, but have not found the way to accomplish the task.

I have tried numerous ideas I found from the web, but nothing works - most of the time I get "Array" displayed

I have tried searilize() but just get s:5:"Array" in the mysql db table column

I can create an array and work with it, no problem - but I can't get the checkbox name="[]" value"xxx" to work in any fashion - I only get "Array" to display.

so here is the code:

Code: Select all

<input type="checkbox" name="features[]"  value="blue"  />
<input type="checkbox" name="features[]"  value="red"  />
<input type="checkbox" name="features[]"  value="yellow"  />
<input type="checkbox" name="features[]"  value="orange"  />
process:

Code: Select all

if(isset($_POST['features']))
{
$features = $_POST['features'];
}
 
print_r($features);  // =   Array ( [0] => Array ) 
echo "<br /><br />";
var_dump($features); //   =    array(1) { [0]=> string(5) "Array" }
This will not enter the database nor can I work with it in a loop: foreach, while, or for

So what am I doing wrong?

Any help would be appreciated...

David
Frank Shi
Forum Newbie
Posts: 6
Joined: Thu Mar 13, 2008 4:03 am

Re: 3 days - so I'll ask - arrays

Post by Frank Shi »

hi

I have a multi checkbox entry that I want to place into a mysql database.

I then need to pull the array data out of the db and display into a <select> box.

I know this is easy, but have not found the way to accomplish the task.

I have tried numerous ideas I found from the web, but nothing works - most of the time I get "Array" displayed

I have tried searilize() but just get s:5:"Array" in the mysql db table column

I can create an array and work with it, no problem - but I can't get the checkbox name="[]" value"xxx" to work in any fashion - I only get "Array" to display.

so here is the code:

Code: Select all

<input type="checkbox" name="features[]"  value="blue"  />
<input type="checkbox" name="features[]"  value="red"  />
<input type="checkbox" name="features[]"  value="yellow"  />
<input type="checkbox" name="features[]"  value="orange"  />
process:

Code: Select all

if(isset($_POST['features']))
{
$features = $_POST['features'];
}
 
print_r($features);  // =   Array ( [0] => Array ) 
echo "<br /><br />";
var_dump($features); //   =    array(1) { [0]=> string(5) "Array" }
This will not enter the database nor can I work with it in a loop: foreach, while, or for

So what am I doing wrong?

Any help would be appreciated...

David[/quote]

Code: Select all

 

need all code
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: 3 days - so I'll ask - arrays

Post by Christopher »

Do you have a form? Try this code:

Code: Select all

<form action="" method="post">
blue<input type="checkbox" name="features[]"  value="blue"  />
red<input type="checkbox" name="features[]"  value="red"  />
yellow<input type="checkbox" name="features[]"  value="yellow"  />
orange<input type="checkbox" name="features[]"  value="orange"  />
<input type="submit" name="submitform"  value="submit"  />
</form>
<?php
if(isset($_POST['features']))
{
    $features = $_POST['features'];
 
    print_r($features);  // =   Array ( [0] => Array )
    echo "<br /><br />";
    var_dump($features); //   =    array(1) { [0]=> string(5) "Array" } 
}
(#10850)
Post Reply