I have the following php array:
Array
(
[0] => Array
(
[name] => page_1
[clicks] => 3
[time] => 14250
)
[1] => Array
(
[name] => page_2
[clicks] => 1
[time] => 123
)
[2] => Array
(
[name] => page_3
)
[3] => Array
(
[name] => page_4
[clicks] => 2
[time] => 1450
)
)
and the following table:
tbl_usage
--------
page_1
page_2
page_3
page_4
What is the best way to add the 'clicks' and 'time' from the array to the proper page column - so the table looks like this:
page_1 page_2 page_3 page_4
-------- ------- --------- ----------
3:14250 1:123 0:0 2:1450
$data = array(...); // your data array;
$values = array();
foreach ($data $v)
{
$clicks = (int)$v['clicks'];
$time = (int)$v['time'];
$values[$v['name']] = ($clicks > 0 ? $clicks : 0) . ':' . ($time > 0 ? $time : 0);
}
$sql = "INSERT INTO `tbl_usage` (`" . implode("`, `", array_keys($values)) . "`) VALUES ('" . implode("', '", $values) . "')";
But I think that you need rethink the structure of the table.