PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 30th, 2012, 12:07 PM
rePete rePete is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 658 rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
Issue with getting values from form??

Hi,

I am trying to develop my own custom form in wordpress and I am having trouble!

I have a form in one php file that has 4 checkbox (values 1, 2, 3, 4) with the same input name. eg. form_cb[].

In my other php file, I use the following code so that I can use the values from the 4 checkbox if it is checked.
Code:
$mycheck = join (', ', $_POST['form_cb']);


I use this code to help me store values into my database in this format: 1, 3, 4.

Which I then use with this code to retrieve later:
$my_array = explode(',',$find_values);
foreach ($my_array as $entry){.... blah blah

I keep getting the following error when there are zero checkbox selected:
join() [function.join]: Invalid arguments passed in.....blah blah

I have tried adding this code before it because I read it somewhere online:
$mycheck = array();

But that doesn't work.

I have come to the conclusion that the error occurs because the join is expecting it to be atleast 1 value and when no checkbox is selected, it produces "undefined" or "" value?? Is there another way of writing this code $mycheck = join (', ', $_POST['form_cb']); to get the same result, so that this issue does not occur.

A "join" that will accept undefined or "" as a value and not cause issues.

Thank you : )

Reply With Quote
  #2  
Old December 30th, 2012, 12:21 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 9 h 36 m 16 sec
Reputation Power: 813
Hi,

stop right there. Do not store lists or arrays or whatever in a database. See normalization (especially the part on the First normal form) to understand why this is a bad idea and what you should do instead.

This should also solve your join() problem, because you won't need this function at all.

Reply With Quote
  #3  
Old December 30th, 2012, 12:29 PM
rePete rePete is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 658 rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
Hi,

Another plugin stores favourite post IDs of each user this way and it seems to work ok.

The reason being is that the database table could become very very big if for example one user could select 20 colors each.

Also how would I do it your way?

The values of the 4 checkbox is being sent in one POST??

Thanks

Reply With Quote
  #4  
Old December 30th, 2012, 12:31 PM
rePete rePete is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 658 rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
How do you get those 4 values of the checkbox to the function without using this:
$mycheck = join (', ', $_POST['form_cb']);

Reply With Quote
  #5  
Old December 30th, 2012, 12:57 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 9 h 36 m 16 sec
Reputation Power: 813
Quote:
Originally Posted by rePete
Another plugin stores favourite post IDs of each user this way and it seems to work ok.


Well, yeah. Shooting myself in the foot also "works".

And just because other plugins do the same doesn't mean it's good. Not every open source "developer" knows what he's doing -- actually, Wordpress itself isn't really the pinnacle of software development.



Quote:
Originally Posted by rePete
The reason being is that the database table could become very very big if for example one user could select 20 colors each.


So? MySQL can handle billions of rows. Should you ever reach that point, you'll need a new server architecture, anyway.



Quote:
Originally Posted by rePete
Also how would I do it your way?


Have you read to linked articles? They go through a concrete example and explain what to do. To cut it short: Make a "chosen_colors" table and store each choice individually as a row (together with the user ID).

Last edited by Jacques1 : December 30th, 2012 at 01:09 PM.

Reply With Quote
  #6  
Old December 30th, 2012, 03:36 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,947 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 55 m 23 sec
Reputation Power: 7053
To avoid the error you would use a conditional to check whether any of the checkboxes were checked before trying to join them.

However, Jacques1 is correct, what you've described is a bad design. A lot of WordPress plugins have bad architectures, partly because WordPress itself has a poor architecture.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #7  
Old December 30th, 2012, 10:59 PM
BarryG BarryG is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: Sydney Australia
Posts: 131 BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 7 h 51 m 28 sec
Reputation Power: 83
Quote:
Originally Posted by rePete
Also how would I do it your way?

The values of the 4 checkbox is being sent in one POST??

Thanks


No, they are not. You have overlooked the fact that a checkbox is only sent if it is checked. None checked means none sent, just as you've discovered.
IF you were going to do this, then you should test for the presence of the checkbox value in the $_POST data to see that it was checked

But don't do it for this, as like other have said, this is bad design.

Reply With Quote
  #8  
Old December 30th, 2012, 11:02 PM
rePete rePete is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 658 rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
Thanks E-Oreo,

Conditions worked to prevent join being executed when it is an empty value.

Is it bad though to use "multiple" values in one attribute in my situation?

From my understanding, it stores the group of values as a string.

A set of numbers collected would be separated by a comma and stored as a long string in the database.

Then to retrieve, a script will take the long string and separate each individual value by using the comma.

I can see where it would be an issue if it was a full name for example where you need control for searching but for just collecting a group of numbers to use so you know what posts to grab for example, I do not see what the issue could be??

Please enlighten me because I am still learning this stuff.

Reply With Quote
  #9  
Old December 31st, 2012, 02:20 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 9 h 36 m 16 sec
Reputation Power: 813
Quote:
Originally Posted by rePete
I can see where it would be an issue if it was a full name for example where you need control for searching but for just collecting a group of numbers to use so you know what posts to grab for example, I do not see what the issue could be??


There are several problems:
  • composite values (in your case comma-separated lists) are more or less inaccessible to the database system, so you're basically circumventing the whole system; instead of using the built in database functionalities (counting, joining, selecting etc.), you have to re-implement everything in your application; that's obviously bad design
  • VARCHARs can contain anything, so there's no guarantee for correct and sensible data; what if your application is buggy or somebody edits the data and you end up with nonsense strings like ",,aaa,"? What do you make out of that?
  • there's no way to enforce referential integrity (making sure that the color IDs actually point to existing colors)
I mean, sure, if you don't give a damn about correct data and proper design, you can use comma separated lists.

But why would you do that? While you were still fighting with that join() stuff, you might as well have created the new color table and rewritten a few queries. Then you'd have a working and clean solution now.

Last edited by Jacques1 : December 31st, 2012 at 02:26 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Issue with getting values from form??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap