User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2392 | Replies: 4 | Solved
Reply
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Help checkbox array storage and retrieval

  #1  
Nov 27th, 2007
I am trying to store the contents of checkboxes in a field in a mysql database and retireve them later...

  1. <input type="checkbox" name="days[]" class="other" value="Monday" />
  2. <label class="left">Monday</label> <br />
  3. <input type="checkbox" name="days[]" class="other" value="Tuesday" />
  4. <label class="left">Tuesday</label> <br />
  5. <input type="checkbox" name="days[]" class="other" value="Wednesday" />
  6. <label class="left">Wednesday</label> <br />
  7. <input type="checkbox" name="days[]" class="other" value="Thursday" />
  8. <label class="left">Thursday</label> <br />
  9. <input type="checkbox" name="days[]" class="other" value="Friday" />
  10. <label class="left">Friday</label> <br />
  11. <input type="checkbox" name="days[]" class="other" value="Saturday" />
  12. <label class="left">Saturday</label> <br />
  13. <input type="checkbox" name="days[]" class="other" value="Sunday" />
  14. <label class="left">Sunday</label> <br />

  1. $days = implode(",",$_POST['days']);
  2. .
  3. .
  4. mysql_query("INSERT INTO users (..., days, ...) VALUES (.., '$days', ..)") or die("something went wrong during the registration. MySQL said: ".mysql_error());

(the "..." means that there is other information, yes those are confirmed working, inserted as well)


This is what I use to retrieve the data:

  1. $result = mysql_query("SELECT * FROM users where verified = 0");
  2. while($info = mysql_fetch_array($result, MYSQL_ASSOC)) {
  3. .
  4. .
  5. .
  6. $days = explode(",", $info['days']);
  7. foreach ($days as $value){
  8. echo $value;
  9. echo "<br/>";
  10. }
  11. .
  12. .
  13. .
  14. }

(obviously, the .'s mean other - confirmed working - code)


Problem is that the $value comes out as "array"...what the heck am I doing wrong?

Thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: checkbox array storage and retrieval

  #2  
Nov 28th, 2007
try to have a counter so you can display your data:

  1. $a=0;
  2. foreach ($days as $value){
  3. $a=$a+1;
  4. }
  5. for($i=0;$i<$a;$i++)
  6. {
  7. echo $days[$a];
  8. echo "<br/>";
  9. }
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: checkbox array storage and retrieval

  #3  
Nov 28th, 2007
Ok. I just tried that..same thing...

when I echo a$ (after the first foreach), it gives a value of 1

so then I echo $days[0] (the first record)
its gives a value of 'array'

Got frustrated, so I even tried to populate $days like this(starting with an empty array and adding each value the array one by one, then implode to a single comma-delimited string...seems logical) -- ended up with same dang result though...grrrr
  1. $days_array = array("");
  2. foreach ($_POST['days'] as $val2){
  3. $days_array[] = $val2;
  4. }
  5. $days = implode(",",$days_array);

I guess my next step is to take them out of the checkbox array, give each box a seperate name and add them to an array manually in the code.

I think its just a matter of missing something simple...guess I am just overlooking it....
Reply With Quote  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: checkbox array storage and retrieval

  #4  
Nov 28th, 2007
Here's what confusing me...

when I store the imploded array, it SHOULD be a comma-delimated string of the values...BUT, it appears to have the value 'ARRAY', which tells me that the implode didn't work, i.e. the $_POST['days'] array stored not as a string of values seperated by commas, but as an array(hence the value ARRAY showing up in the $data field).

Am I missing a step somewhere when storing the field??
Last edited by verbob : Nov 28th, 2007 at 2:58 pm.
Reply With Quote  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: checkbox array storage and retrieval

  #5  
Nov 28th, 2007
heres a stupid mistake...LOL

When I created the form with the checkboxes, the post was to a file called register.php...I changed it to the name of this file register2.php, but amoungst my edits, I inadvertently reset it back to register.php, hence it was posting from the old file not the new one and thus giving me the error(s).

Gawd...I had the code right many many hours ago but one stupid CTRL-Z too many in Dreamweaver and it messed it all up arrrgh haha

Thanks guys
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC