Laravel 8 如果标题相同,则合并 HTML 表格列

Posted

技术标签:

【中文标题】Laravel 8 如果标题相同,则合并 HTML 表格列【英文标题】:Laravel 8 Combine HTML table column if same header 【发布时间】:2021-02-19 08:38:43 【问题描述】:

好的,我正在创建一个选举网站 我有一个表格从每个部分获取候选人的票数,我的问题是我得到了多个具有相同候选人名称的列,我需要将具有相同标题的所有列合并到 1 列中,并且投票将是彼此下方。 表格图片:

表格代码index.blade.php

    <table class="table table-sm table-hover table-dark text-center">
        <thead class="text-center" style="font-weight:bolder;color:green">
            <tr>
                <td style="padding: 20px;">اضافه مرشح</td>
                <td style="padding: 20px;">اسم مرشح</td>
                <td style="padding: 20px;">Action</td> 
    <?php
                    
                     $counter = App\Models\Morshah::select('name')->where('dayra','=>','5')->groupBy('name')->get()->count();  
    
                    
                    ?>
                    @foreach($morshah as $morshahs) 
                    
                    
                    <td style="padding: 20px;">$morshahs->name</td>
                    @endforeach 
                    
                <td style="padding: 20px;">الباطل</td>
                <td style="padding: 20px;">الصحيح</td>
                <td style="padding: 20px;">الحضور</td>
                <td style="padding: 20px;">الدائره</td>
                <td style="padding: 20px;">اللجنه</td>
                
            </tr>
        </thead>
        <tbody>
            <?php $morshaheen = App\Models\Morshah::where('dayra','=','5')->get(); //dd($morshaheen);      ?>
    @if(count($data)>0)
            <tbody class="">
                @foreach($data as $datas)
                    <tr>
                    !! Form::open(['action' => ['AfterDaysController@addMorshah',$datas->id],'method' => 'POST','style'=>'box-shadow:3px 3px 15px;']) !!
                        <div class="form-group">
                           -- Form::hidden('_method','PUT') --
                           
                   <td>Form::submit('اضافه مرشح',['class' => 'btn-lg btn-warning'])</td>
                   <td> Form::text('name','',['class' => 'form-control'])
                    Form::hidden('dayra',$datas->dayra,['class' => 'form-control '])
                    Form::hidden('lagna',$datas->lagna,['class' => 'form-control '])
                </td>
                    Form::close() 
                   !! Form::open(['action' => ['AfterDaysController@update',$datas->id],'method' => 'POST','style'=>'box-shadow:3px 3px 15px;']) !!
                         <div class="form-group">
                            Form::hidden('_method','PUT')
                    <td>Form::submit('تعديل',['class' => 'btn-lg btn-warning'])</td>
                   @foreach($morshah as $morshahs)
    
                   @if($morshahs->dayra == $datas->dayra && $morshahs->lagna == $datas->lagna)
                   <td>Form::text('votes',$morshahs->votes,['class' => 'form-control'])</td>
                   @else
                   <td>Form::text('votes','',['class' => 'form-control'])</td>
                   @endif
                   @endforeach
                    
    
    
    
    
    
    
                    <td>Form::text('wrong',$datas->wrong,['class' => 'form-control'])</td>
                    <td>Form::text('right',$datas->right,['class' => 'form-control'])</td>
                    <td>Form::text('attend',$datas->attend,['class' => 'form-control'])</td>
                    
                </div>
                 Form::close() 
                <?php
                if($datas->dayra == '5')
                    $dayra = "اول المنتزه";
                else if($datas->dayra == '7')
                    $dayra = "ثان المنتزة";
                else if($datas->dayra == '8')
                    $dayra = "ثالث المنتزة";
                else if($datas->dayra == '9')
                    $dayra = "اول الرمل";
                else if($datas->dayra == '10')
                    $dayra = "ثان الرمل";
                else if($datas->dayra == '11')
                    $dayra = "سيدى جابر";
                else if($datas->dayra == '12')
                    $dayra = "باب شرقى";
                else if($datas->dayra == '13')
                    $dayra = "محرم بك";
                else if($datas->dayra == '15')
                    $dayra = "العطارين";
                else if($datas->dayra == '16')
                    $dayra = "المنشية";
                else if($datas->dayra == '17')
                    $dayra = "الجمرك";
                else if($datas->dayra == '19')
                    $dayra = "كرموز";
                else if($datas->dayra == '20')
                    $dayra = "اللبان";
                else if($datas->dayra == '21')
                    $dayra = "مينا البصل";
                else if($datas->dayra == '22')
                    $dayra = "الدخيلة";
                else if($datas->dayra == '23')
                    $dayra = "اول العامرية";
                else if($datas->dayra == '24')
                    $dayra = "ثان العامرية";
                else if($datas->dayra == '26')
                    $dayra = "برج العرب";
                
                ?>
                <td style=""><?php echo $dayra; ?></td>
                <td>$datas->lagna لجنه</td>
                   </tr>
                
                @endforeach
    
                </tbody>
    @endif
        </tbody>
    </table>

公共函数 index_montaza1()

        $datas = AfterDay::orderBy('id','asc')->where(['dayra'=>'5'])->get();
        $morshah = Morshah::orderBy('id','asc')->where(['dayra'=>'5'])->get();

    return view('after_montaza1.index')->with('data',$datas)->with(compact('morshah'));

【问题讨论】:

欢迎来到SO,我们实际上不明白这里的标题是什么,非英语的原因。无论如何,您不能将多行合二为一,但您可以忽略循环中的重复行 @sta 3 列的三个箭头标题是测试名称(确切地说是相同的名称),我希望将 3 列合并为 1 列,下面的数字(投票)是都在彼此之下 根据您的查询分组,它会在收集的同时轻松组合您的数据$datas = AfterDay::orderBy('id','asc')-&gt;where(['dayra'=&gt;'5'])-&gt;groupBy('name')-&gt;get(); 【参考方案1】:

不能随意组合,但可以用array_unique()忽略重复数据:

foreach (array_unique($data) as $datas) 
   // Do stuff with $d ...

【讨论】:

以上是关于Laravel 8 如果标题相同,则合并 HTML 表格列的主要内容,如果未能解决你的问题,请参考以下文章

如果第二个数组的项目包含数组的第一个对象的 id,则合并 Angular 8 中的两个对象数组

如果日期相同或 +- 7 天且 ID 相同,则合并 2 行

如果具有相同的 id 和相同的值或值在 PHP 中为空,则合并行

Swift:如果数组的自定义对象元素具有相同的键,则合并它们

如果两列中的值相同,则合并熊猫中的单元格

pyspark:如果列在不同行中具有相同的值,则合并两行或多行