I am doing a PHP form that requires fields to be filled up - and there is a section on editing the fields in an application form by the surfer after the form has been submitted...
The skill sets are ticked in the original page but when it comes to editing the skill set, the code I typed up shows the checked boxes arranged in a different order compared to the original page.
Here is the original application page with the checkboxes immediately after they are checked but BEFORE submission and the potential editing of the form:
And here is the application page after the fields have been submitted and processed by the server into the database if the applicant wants to edit the fields. They are in a complete disorder!
I would like the second image to produce a checkbox list just like the first when the application was being done.
The fields are selected or retrieved from a database table named skillset and compared with the values inserted from that table into another table named emprecords. By running a for loop in the emprecords table, I am able to echo or print out the skills set list (after imploding the string that separates each skill by a comma in the emprecords database) that a particular applicant has inserted but I am not able to print the selected checkboxes in the correct order for the lists of skills that were in the Array. I hope the images above will help. Here is the PHP code for the page that edits the fields for an applicant on a website:
<br><br><H2 align="center">SKILLS SET</H2>
<br>
<label for="skills" size="3">Pick Your Skill(s): </label>
<br><br>
<tr>
<table border='1' cellspacing='0'>
<colgroup>
<col span='1'>
</colgroup>
<tr>
<td>Engineering Services</td>
<td>Information Technologies</td>
<tr>
<td valign="top">
<?php
$id = $_GET["id"];
$query2 = "SELECT * FROM emprecords WHERE id ='$id'";
$record_set2 = $dbs->prepare($query2);
$record_set2 -> execute();
$row2 = $record_set2->fetch(PDO::FETCH_ASSOC);
$sk = $row2['skills'];
$skills1 = explode(",", $sk);
for ($i=0; $i< count($skills1); $i++) {
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills1'checked>$skills1[$i]<br>";
}
$list = "
SELECT *
FROM skillsset
WHERE category='Engineering'
ORDER BY skills ASC";
$listAHI = $dbs ->prepare($list);
$listAHI -> execute();
if(!isset($_POST['submitd'])) {
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC))
{
$skills = $row["skills"];
echo "
<form action='' method='post'>
<input type='checkbox' id='skills' name='skills[]' value='$skills'> $skills<br> ";
}
}
else {
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC)) {
$skills = $row["skills"];
if(strlen($skills)>0){
if(isset($_POST['skills']) and in_array($skills, $_POST['skills'])) {
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills' checked>$skills<br>";
}
if(isset($_POST['skills']) and !in_array($skills, $_POST['skills'])){
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills' unchecked>$skills<br>";
} else {
if(!in_array($skills, $_POST['skills'])) {
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills' unchecked>$skills<br>";
}
}
}
echo "</form>";
}
}
?>
</td>
<td valign="top">
<?php
$list = "
SELECT *
FROM skillsset
WHERE category='Information'
ORDER BY skills ASC";
$listAHI = $dbs ->prepare($list);
$listAHI -> execute();
if(!isset($_POST['submitd'])){
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC))
{
$skills = $row["skills"];
echo "
<form action='' method='post'>
<input type='checkbox' id='skills' name='skills[]' value='$skills'> $skills<br> ";
}
}
else {
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC)) {
$skills = $row["skills"];
if(strlen($skills)>0) {
if(isset($_POST['skills']) and in_array($skills, $_POST['skills'])) {
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills' checked>$skills<br>";
}
if(isset($_POST['skills']) and !in_array($skills, $_POST['skills'])) {
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills' unchecked>$skills<br>";
} else {
if(!in_array($skills, $_POST['skills'])){
echo "<input type='checkbox' id='skills' name='skills[]' value='$skills' unchecked>$skills<br>";
}
}
}
echo "</form>";
}
}
?>
</td>
</tr>
</table>
Please try and help me solve this puzzle.