jeda Posted September 3, 2009 Share Posted September 3, 2009 Hi everyone, I'm brand new to the forums and have already gained incredible amounts of insight into php coding from reading other forum posts. I'm not much of a developer; I only know enough php to get into trouble, but I'm enjoying the learning process. I'm hoping that someone can help me out with a basic script or some pointers for writing a script. Here's what I need: I have an html form that asks users to consider situations and rank how likely they would be to fall asleep in that situation. For record-keeping purposes, the results are recorded with a full description that includes both letters and numbers. For instance in situation 1, if the user picks "5 - Very Likely to Fall Asleep" then that's exactly what is saved as the value for that form item. When the form is processed, all responses are thrown into an array and saved to the database as a single record (like "1 - unlikely to fall asleep, 5- very likely to fall asleep, etc.). This is set and can't be changed; however, I now need a piece of code that takes the array that holds all the info, strips out ALL letters, spaces, and dashes (in other words takes out everything but the numbers), and adds all the numbers together. The result of the script should be the sum of the numbers. Please let me know if I can clarify this at all. Thanks and look forward to any pointers you can offer! John Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 preg_match_all('~(\d)+~',$input,$output); echo array_sum($output[0]); Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 I have an html form that asks users to consider situations and rank how likely they would be to fall asleep in that situation. For record-keeping purposes, the results are recorded with a full description that includes both letters and numbers. For instance in situation 1, if the user picks "5 - Very Likely to Fall Asleep" then that's exactly what is saved as the value for that form item. When the form is processed, all responses are thrown into an array and saved to the database as a single record (like "1 - unlikely to fall asleep, 5- very likely to fall asleep, etc.). That is overall a bad idea. A field in a database should only contain one element only. Look into database normalization. Quote Link to comment Share on other sites More sharing options...
jeda Posted September 4, 2009 Author Share Posted September 4, 2009 Excellent help guys! I very much appreciate you both taking the time to respond. I'll try the code sample garethp provided this weekend. Daniel0, thanks for the link; I'm very new to systems administration, and it's more of a hobby. Database normalization is not something I was familiar with. I'm going to study the topic and attempt to implement normalization this weekend. Regards! Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 4, 2009 Share Posted September 4, 2009 Just to let you know, $input has to be a string, not an array Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.