Exoon Posted October 29, 2013 Share Posted October 29, 2013 Hi, I'm still new to php and trying to get this function to work, im not sure what im doing wrong. i want the end result to be like $breakfast_cereal = "Cornflakes;Porridge;Special K;"; <?php function getAnswers($input) { // Don't bother doing anything if it's an empty array if (!empty($input)) { return; } foreach($_POST[''.$input.''] as $results) { $results = trim($results); $input .= "$results;"; } } echo getAnswers('breakfast_cereal'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/ Share on other sites More sharing options...
Psycho Posted October 29, 2013 Share Posted October 29, 2013 (edited) Read this bit of code again. // Don't bother doing anything if it's an empty array if (!empty($input)) { return; } Also, in this code, why are you adding unnecessary quotes - i.e. concatenating an empty string? foreach($_POST[''.$input.''] as $results) { Should use $_POST[$input] But, there's no need to even use a foreach() loop. Just implode() the array using "; " as the glue. EDIT: I would give you a one-line solution to solve all those problems, but I suspect this is homework. So, I'll give you this advise. The only PHP functions you need to use are array_filter() and implode(). Edited October 29, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456066 Share on other sites More sharing options...
Exoon Posted October 29, 2013 Author Share Posted October 29, 2013 i just tried removing that bit of code and i still don't get anything returned. Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456067 Share on other sites More sharing options...
Psycho Posted October 29, 2013 Share Posted October 29, 2013 (edited) i just tried removing that bit of code and i still don't get anything returned. Reread your reply with the emphasis that I've added. FYI: I'm not trying to be difficult, but this is obviously schoolwork, so I want to help guide you to seeing the problem for yourself. You'll learn a lot more that way. Edited October 29, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456068 Share on other sites More sharing options...
Exoon Posted October 29, 2013 Author Share Posted October 29, 2013 (edited) I assure you this isn't homework, Im 26 years old but i appreciate your help so far. Am i along the right lines so far? function getAnswers($input) { $input = implode(";", $_POST[$input]); } echo getAnswers('breakfast_cereal'); Im guessing its not working because each of the values in the array dont have the ; at the end any more? Is the way you're suggesting going to end up with the result i want? Edited October 29, 2013 by Exoon Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456069 Share on other sites More sharing options...
Ch0cu3r Posted October 29, 2013 Share Posted October 29, 2013 You need make the function return the value of $input if you want to echo it function getAnswers($input) { $input = implode(";", $_POST[$input]); return $input; } echo getAnswers('breakfast_cereal'); Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456075 Share on other sites More sharing options...
Exoon Posted October 29, 2013 Author Share Posted October 29, 2013 Ah, its working now, thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456076 Share on other sites More sharing options...
Psycho Posted October 29, 2013 Share Posted October 29, 2013 (edited) And to follow up with my previous response, you should use array_filter() to remove empty values before you concatenate them with the semi-colon. function getAnswers($input) { return implode(';', array_filter($_POST[$input])); } Edited October 29, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456078 Share on other sites More sharing options...
Exoon Posted October 29, 2013 Author Share Posted October 29, 2013 (edited) How can i automate this so it calls the function for every entry in the database. I want it to check all of the entries in my course_menuname column This is how i want it to end up like: $breakfast_cereal = getAnswers('breakfast_cereal'); $breakfast_toast = getAnswers('breakfast_toast'); $breakfast_toastextra = getAnswers('breakfast_toastextra'); $breakfast_colddrinks = getAnswers('breakfast_colddrinks'); $breakfast_hotdrinks = getAnswers('breakfast_hotdrinks'); This is my table This is what ive got so far $query = "SELECT course_menuname FROM course"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $row[course_menuname] = getAnswers($row[course_menuname]); } Edited October 29, 2013 by Exoon Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456080 Share on other sites More sharing options...
Psycho Posted October 29, 2013 Share Posted October 29, 2013 I'm not sure what you trying to accomplish. But, I would venture to say you are doing it in a way that is more difficult than it needs to be. Let me state what I *think* you are doing and how I think it should be accomplished. I assume you are receiving a form of POST data and in that post data there are multiple field sets for various menu options. For each of those menu options you are wanting to create a concatenated list of the POST values into a variable. What you have above should work and is actually preferable (making them array values) instead of making them individual variables - except you are putting the values back into the $row variable which is getting overwritten on each iteration of the loop! Plus, you are not passing the value to the function, you are passing a string.You would want to put the values into a new array. $output = array(); while($row = mysql_fetch_array($result)) { $output[course_menuname] = getAnswers($row['course_menuname']); } But, you can probably do what you need without a DB query at all. Are there any POST fields that are arrays which you do NOT want to be processed in this way? If not, you could simply iterate over the POST values $output = array(); foreach($_POST as $menu => $values) { if(is_array($values)) { $output[$menu] = getAnswers($values); } } Or, if you may have other fields that are in arrays - just put all the menu fields in a parent sub-array: E.g. $_POST['menu']['lunch_main'] Lastly, what are you doing with these concatenated values. I hope you are not storing these in the database. It will make it difficult, if not impossible, to perform queries to search/calculate based upon certain options. Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456088 Share on other sites More sharing options...
Solution Exoon Posted October 29, 2013 Author Solution Share Posted October 29, 2013 (edited) Yes i am just using post fields and they are all being used, unless they are left blank if that matters? This is what i need: $breakfast_cereal = getAnswers('breakfast_cereal'); $breakfast_toast = getAnswers('breakfast_toast'); $breakfast_toastextra = getAnswers('breakfast_toastextra'); $breakfast_colddrinks = getAnswers('breakfast_colddrinks'); $breakfast_hotdrinks = getAnswers('breakfast_hotdrinks'); So i can then just enter them values into a database, there must be an easy solution I just tried both of the solutions you posted but it didn't see to display anything back This is what i tried: $output = array(); while($row = mysql_fetch_array($result)) { $output[$row['course_menuname']] = getAnswers($row['course_menuname']); } echo $output['breakfast_cereal']; and $output = array(); foreach($_POST as $menu => $values) { if(is_array($values)) { $output[$menu] = getAnswers($values); } } echo $output['breakfast_cereal']; I get this error with the 2nd bit of code. Warning: Illegal offset type in isset or empty in /volume1/web/horder/add-menuset.php on line 9 Heres some screenshots hopefully clears it up abit what im doing. Admin Panel to add all the choices Front end for user to select their dinner choice This is how they are stored into the DB Edited October 29, 2013 by Exoon Quote Link to comment https://forums.phpfreaks.com/topic/283412-how-can-i-get-this-function-to-work/#findComment-1456094 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.