0
$queryStoreSecondaryInfo = $connection->query($sqlStoreSecondaryInfo);

if( $queryStoreSecondaryInfo ){
    echo "<br />Updated Secondary! <br />";
    $sqlUpdatedSecondaryInfo = " SELECT * FROM students_skills 
                                 WHERE ID='$_SESSION[ID]'  
                                 AND Email='$Email' ";
    $queryUpdatedSecondaryInfo = $connection->query($sqlUpdatedSecondaryInfo);
    while( $row = $queryUpdatedSecondaryInfo->fetch_assoc() ){
        $_SESSION["Landscaping"] = $row["Landscaping"];
        $_SESSION["Cleaning"] = $row["Cleaning"];
        $_SESSION["Delivery"] = $row["Delivery"];
        $_SESSION["Music"] = $row["Music"];
        $_SESSION["Maintenance"] = $row["Maintenance"];
        $_SESSION["Decoration"] = $row["Decoration"];
        $_SESSION["Painting"] = $row["Painting"];
        $_SESSION["PetCare"] = $row["PetCare"];
        $_SESSION["Tutoring"] = $row["Tutoring"];
        $_SESSION["Vehicles"] = $row["Vehicles"];
        $_SESSION["SnowRemoval"] = $row["SnowRemoval"];
        $_SESSION["Other"] = $row["Other"];

        foreach( $row as $skill => $value ){
            $postSkills=array();
            if( $value == "1" ){

                //if statement is true, add all column header names, 
                //ie. $skill values, together, and store in a variable, eg. $postSkills

            }
            echo $postSkills;
        }
    }
}

The SESSION values are coming from multiple checkboxes. I want to concat all names together, in a string, and store it as a session variable. I have seen other solutions and everywhere the response is to set the name of checkboxes as something like "name='array[]'. However, because of how my database is set up, I have to give each checkbox a unique name. Is there anyway to concat all the column header names together, without creating a separate database to store the full list of skills together?

Thanks!

1 Answer 1

1

Don't make $postSkills an array, let it be string. Store the skills in this by seperating them with '|' .

               $queryStoreSecondaryInfo = $connection->query($sqlStoreSecondaryInfo);

               if( $queryStoreSecondaryInfo ){
                   echo "<br />Updated Secondary! <br />";
                   $sqlUpdatedSecondaryInfo = " SELECT * FROM students_skills 
                                                WHERE ID='$_SESSION[ID]'  
                                                AND Email='$Email' ";
                   $queryUpdatedSecondaryInfo = $connection->query($sqlUpdatedSecondaryInfo);
                   while( $row = $queryUpdatedSecondaryInfo->fetch_assoc() ){
                       $_SESSION["Landscaping"] = $row["Landscaping"];
                       $_SESSION["Cleaning"] = $row["Cleaning"];
                       $_SESSION["Delivery"] = $row["Delivery"];
                       $_SESSION["Music"] = $row["Music"];
                       $_SESSION["Maintenance"] = $row["Maintenance"];
                       $_SESSION["Decoration"] = $row["Decoration"];
                       $_SESSION["Painting"] = $row["Painting"];
                       $_SESSION["PetCare"] = $row["PetCare"];
                       $_SESSION["Tutoring"] = $row["Tutoring"];
                       $_SESSION["Vehicles"] = $row["Vehicles"];
                       $_SESSION["SnowRemoval"] = $row["SnowRemoval"];
                       $_SESSION["Other"] = $row["Other"];

                       foreach( $row as $skill => $value ){
                           $postSkills="";
                           if( $value == "1" ){

                               //if statement is true, add all column header names, 
                               //ie. $skill values, together, and store in a variable, eg. $postSkills

                               $postSkills .= "$skill|";

                           }
                           if($postSkills!="") {
                               $postSkills = substr($postSkills,0,-1);
                           }
                           echo $postSkills;
                       }
                   }
               } 

Now $postSkills will contain names of all the skills for which value is 1. Something like this -

$postSkills = "Landscaping|Cleaning|Decoration|Vehicles";

In order to set session variables from this, you will need to -

    $skillsArr = explode("|", $postSkills);

    foreach($skillsArr as $skill) {
        $_SESSION["$skill"] = 1;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for tour answer. However, how would you set up an array to store the values as $postSkills[0] => $_SESSION["Landscaping"]; $postSkills[1] => $_SESSION["Cleaning"]; and so on.......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.