₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,134 members, 8,420,503 topics. Date: Thursday, 04 June 2026 at 10:27 PM

Toggle theme

Php Array Problem [SOLVED] - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingPhp Array Problem [SOLVED] (904 Views)

1 Reply

Php Array Problem [SOLVED] by adewasco2k(op):
hi mates, i just cant get the right approach to this problem, i have two arrays .
1. $attendance which has the following keys and values


Array
(
[attendance] => Array
(
[1] => 1
[2] => 1
[3] => 0
[4] => 1
[5] => 1
[6] => 0
[7] => 0
)


This is coming from a checkbox if checked value is 1 else value is 0

The second array is $remarks


[remark] => Array
(
[1] =>
[2] =>
[3] => 'sick'
[4] =>
[5] =>
[6] => 'leave'
[7] => 'On assignment'

)


Now this is what the key 1- 7 stands for the script is for employee's attendance the key 1-7 is the employeeID in the employee table in my database.

Now what i want to achieve is concatenate the array in such a way to look like this


Array
(
[0] => Array
(
[employeeID] => 7
[attendance] => 0
[remark] => 'On assignment'
)

[1] => Array
(
[employeeID] => 6
[attendance] => 0
[remark] => 'leave'
)

[2] => Array
(
[employeeID] => 5
[attendance] => 1
[remark] =>
)

//an so on
)


i already did it in a such a way that i first insert the attendance placing the remark as NULL then insert remarks as an update but i want a way to concatenate them and send it at once.

I hope somebody can understand what i want huh


Update
Just incase this might help, the template looks like this


<?php
$x=1;
foreach($employee as $employe)
{
$employee_id=$employe['employeeID'];
echo "<tr><td>".$x."</td>";
echo "<td>".$employe['name']."</td>";
echo "<td><input type='hidden' name='attendance[$employee_id]' value='0'/><input type='checkbox' name='attendance[$employee_id]' value='1'/></td>";
echo "<td><input type='text' name='remark[$employee_id]'/></td></tr>";
$x++;
}
?>


the template has an hidden checkbox checked with value of 0 and having same name with the other visible checkbox, so if the main checkbox is check the value will be 1 from the main check box otherwise value will be 0 following now is the text field name remark.

Just to add, the names on the picture below are fake and not real names just dummy data from my database grin

SOLUTION:


$attendance = $_POST['attendance'];
$remarks = $_POST['remark'];

$collect = array();
foreach ($attendance as $employeeID => $attending) {
$collect[] = array(
"employeeID" => $employeeID,
"attendance" => $attending,
"remark" => isset($remarks[$employeeID]) ? $remarks[$employeeID] : ""
);
}

$values = array();
foreach ($collect as $entry) {
$values[] = "(" . ((int) $entry["employeeID"]) . ",
CURDATE(),
" . ((int) $entry["attendance"]) . ",
'" . sql_escape($entry["remark"]) . "'
)";
}
$query = "INSERT INTO `attendance`
(`employeeID`, `date`, `status`, `remarks`)
VALUES
(" . implode(",", $values) . " )";

1 Reply

Please Help With An Array ProblemSimple Array Problem In PhpConverting Picture To Byte Array(blob) And Back To Picture234

How To Use Webservice Wsdl File In PHP5 Blogs That Can Tell You About Whats Going On In The Programming WorldIs Programming A Dead End Job? What Are Your Thoughts?