Jump to content

How can i get this function to work ?


Exoon
Go to solution Solved by Exoon,

Recommended Posts

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;";


 

J1FLs.png

 

 



    <?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');
    ?>


 

 

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

I assure you this isn't homework, Im 26 years old  :happy-04: 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 by Exoon
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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

fk3Nruk.png

 

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 by Exoon
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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

ZxfD1fl.png

 

 

Front end for user to select their dinner choice

FnsWsEa.png

 

 

This is how they are stored into the DB

ilqbArB.png

Edited by Exoon
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.