Home > database >  How to rowspan a td if it has a duplicate value
How to rowspan a td if it has a duplicate value

Time:03-30

I am trying to manipulate my table using rowspan but I'm kinda struggling.

My expected output should be if there's two duplicated names I want it to have a rowspan in the table. But my problem occurs if there's a third duplicate and its separated between another name (in this case 'ruby' is in between) my table will have an excess column.

My code is kinda like this

sample:

$gems = array(
 0 => array:2[
   name: Amber
   value: 20
 ]
 1 => array:2[
   name: Amber
   value: 30
 ]
 2 => array:2[
   name: Ruby
   value: 40
 ]
 3 => array:2[
   name: Amber
   value: 50
 ]
 4 => array:2[
   name: Emerald
   value: 60
 ]
);
 

This is how I map the rowspan

$rows = array();
foreach($rows as $key => $gem){
    $rows[$key] = $gem['name'];
}
$arr = array_replace($row,array_fill_keys(array_keys($row, null),''));
$rowspan = array_count_values($ar);

$rowspan output will be this which i use on the table.

array:3(
 "Amber" => 3    
 "Ruby" => 1
 "Emerald" => 1
)

Then my display is kinda like this (not exact). Im injecting the rowpan on $rowspanDuplicated

$html = '<table>'
foreach($gems as $key => $gem){
   $html .= '<tr>'
   $rowspanDuplicated = 'rowspan="'. $rowspan[$gem['name']].'"';
      if($rowspan[$gem['name']] <= 1){
         $rowcount = 1;
            $html .= '<td>' . $this->n($sv['name']). ' </td>'
      } else {
         if($rowcount<=1){
             $html .= '<td' . $rowspanDuplicated . '>' .$this->n($sv['name']) . '</td>';
         }
         if($rowspan[$sv['name']] == $rowcount){
             $rowcount=1;
         } else { 
             $rowcount  ; 
         }
      }

  $html .= '<td>' . $this->n($sv['value']). ' </td>'

  $html .= '</tr>'
}
$html = '</table>'

The problem to this code is that $gem[3] will also have a rowspan

I want my table something like this.

_________________
Amber   | 20
        |-----
        | 30
________|________
Ruby    | 40
_________________
Amber   | 50
________|________
Emerald | 60
_________________

CodePudding user response:

I think you should group your array based on name in the first place.

public function group($gems)
{
    $grouppedGems = [];

    foreach($gems as $gem) {
        if (!array_key_exists($gem['name'], $grouppedGems))
            $grouppedGems[$gem['name']] = [];

        $grouppedGems[$gem['name']][] = $gem;
    }

    return $grouppedGems;
}

$html = '<table>'

foreach($gems as $name => $gem){
    $html .= '<tr><td rowspan="' . count($gem) . '">' . $name . '</td><td>';

    foreach($gem as $item) {
        $html .="<tr><td>{$this->n($item['value'])}</td></tr>";
        
    }


    $html .= '</td></tr>';
}

$html = '</table>';

I didn't test the html string, but I think it will work. BTW, you won't need rowspan, bu I added it just in case.

CodePudding user response:

blade version

@foreach($gems as $gem)
<tr>
  <td
  @if(!$loop->last && $gems[$loop->index 1]->gem['name'] == $gem['name']) rowspan=2 >{{$gem['name']}}</td>
  <td>{{$gem['value']</td>
<tr>
@endforeach
  • Related