I am making a function where I track my users' registration. The users can choose whether they want to register at Red, Blue, Yellow, and Green. I have this table structure in my view. (Please see the screenshot) My target is how I can do this output based on their chosen registration color.
The first image is my target output.
The second image is my current output.
I have provided more explanation here with 3 conditions
if the previous color is the same as the current color, the data will go down.
example:
prev - red
next - red
the logs should go to the next column if the current color is not the same as the previous color
prev- red
next - blue
If the colors are Yellow And Green, the data will go down
Any suggestion/s on how can I achieve my target? Thank you
Controller:
function transaction(){
$data= $this->dashboards->queryTransaction();
$res = array();
$prev = '';
$next = '';
$output = '<table id="displayLogs" class="table-bordered" >';
$current = 0;
$res[$current] = array();
foreach ($data as $f) {
$next = $f['logs'];
switch ($f['logs']) {
case "red":
$btn = "bg-danger";
break;
case "blue":
$btn = "bg-primary";
break;
case "green":
$btn = "bg-success";
break;
case "yellow":
$btn = "bg-warning";
break;
default:
$btn = "bg-primary";
break;
}
if ($prev == $next || $prev == '' || $next == "yellow" || $next == "green") {
} else {
$current++;
$res[$current] = array();
}
$res[$current][] = $btn;
// prevent change from same color like this case : RED -> YELLOW -> RED
if ($next != "yellow" && $next != "green") {
$prev = $next;
}
}
$max = count($res[0]);
foreach ($res as $btn) {
if (count($btn) > $max) {
$max = count($btn);
}
}
for ($i = 0; $i < $max; $i++) {
foreach ($res as $btn) {
if (array_key_exists($i, $btn)) {
$output .= '<td ><span data-toggle="tooltip" class="badge ' . $btn[$i] .
'" style="font-size: 23px; margin: 0px; border-radius: 50px;">ʘ</span></td>';
}else{
$output .= '<td ></td>';
}
}
$output .= '</tr>';
}
echo json_encode($output);
}


