如何使用 php 和 codeigniter 隐藏检索到的数据值的重复部分

Posted

技术标签:

【中文标题】如何使用 php 和 codeigniter 隐藏检索到的数据值的重复部分【英文标题】:How to hide repeating part of the retrieved data values using php and codeigniter 【发布时间】:2018-01-14 14:42:40 【问题描述】:

由于这个 mysql 语句,我得到了下表

SELECT * FROM expenses, income 
WHERE expenses.projectname=income.projectname 
AND expenses.task=income.task

这些是我的项目表中的字段

这些是我的任务表的字段

在此表中,一个项目有许多任务。因此项目、客户、项目开始和结束日期列重复无意义。我怎样才能为所有任务只显示一次?如何在这里应用 php 隐藏逻辑?下图显示了我需要实现的目标。通过 MySQL 查询检索数据。但是我怎样才能隐藏不必要的重复值 这是 CodeIgniter 视图页面

<table class="table table-lg">
<thead >
    <tr class="filters">

       <th><input type="text" class="form-control" placeholder="Project" disabled></th>
        <th><input type="text" class="form-control" placeholder="Employee" disabled></th>
        <th><input type="text" class="form-control" placeholder="Task" disabled></th>
        <th><input type="text" class="form-control" placeholder="Expense" disabled></th>
        <th><input type="text" class="form-control" placeholder="Amount" disabled></th>
        <th><input type="text" class="form-control" placeholder="Paid/Not" disabled></th>
        <th><input type="text" class="form-control" placeholder="Client" disabled></th>
        <th><input type="text" class="form-control" placeholder="Cost" disabled></th>
        <th><input type="text" class="form-control" placeholder="Income " disabled></th>
        <th><input type="text" class="form-control" placeholder="Date" disabled></th>

    </tr>
</thead>
<tbody>
<?php
if(isset($view_data1) && is_array($view_data1) && count($view_data1)): $i=1;
foreach ($view_data1 as $key => $data)  
?>

<tr <?php if($i%2==0)echo 'class="even"';elseecho'class="odd"';?>>

    <td><?php echo $data['projectname']; ?></td> 
    <td><?php echo $data['employee']; ?></td> 
    <td><?php echo $data['task']; ?></td> 
    <td><?php echo $data['ExpenseName']; ?></td> 
    <td><?php echo $data['ExpenseAmount']; ?></td>
    <td><?php echo $data['pn']; ?></td>  
   <td><?php echo $data['cname']; ?></td>
   <td><?php echo $data['taskcost']; ?></td> 
   <td><?php echo $data['amount']; ?></td> 
   <td><?php echo $data['datetimepicker_mask']; ?></td> 
</tr>
<?php
    $i++;
      
    else:
?>
<tr>
    <td colspan="7" align="center" >No Records Found..</td>
</tr>
<?php
    endif;
?>
</tbody>                
</table>

【问题讨论】:

您的图片链接已损坏 @JYoThI 不是链接坏了。请看我编辑的版本 也许 SQL GROUP BY 可以帮助您:dev.mysql.com/doc/refman/5.7/en/group-by-handling.html - 类似于 SELECT projectname, ANY_VALUE(task) FROM expenses, income WHERE expenses.projectname=income.projectname AND expenses.task=income.task GROUP BY projectname @Dushee 感谢您的反馈... @Dushee 你能记下两张表的所有列吗?我们很容易知道哪一列在两张表上。谢谢 【参考方案1】:

这是我的处理方式。我希望这和你遇到的问题一样:)

*重要,变量$dcost和$cd是我自己设置的变量,请根据你的情况调整

之前 后 代码前

foreach(your sql variable)
echo '<tr><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['destination'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timeout'].'</td>';
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="mday">'.$cd['mday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-mrate="'.$cd['mrate'].'" class="mrate">'.rupiah($cd['mrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tm"></td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="lday">'.$cd['lday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-lrate="'.$cd['lrate'].'" class="lrate">'.rupiah($cd['lrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tl"></td></tr>';

代码后

foreach(your sql variable)
echo '<tr>';
if($i - 1 == 1)
    $i = 0;
echo '  <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['destination'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timeout'].'</td>';
else
echo '  <td colspan="5" style="border-right:1px solid #000;">&nbsp;</td>';

echo '<td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="mday">'.$cd['mday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-mrate="'.$cd['mrate'].'" class="mrate">'.rupiah($cd['mrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tm"></td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="lday">'.$cd['lday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-lrate="'.$cd['lrate'].'" class="lrate">'.rupiah($cd['lrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tl"></td></tr>';

【讨论】:

这没有提供问题的答案。您可以search for similar questions,或参考页面右侧的相关和链接问题找到答案。如果您有一个相关但不同的问题,ask a new question,并包含指向此问题的链接以帮助提供上下文。见:Ask questions, get answers, no distractions 对不起,我找到了我自己做的答案,请重新检查我已更改的 cmets【参考方案2】:

使用in_array() 函数并将colspanrowspan 设置为td

在您的查看页面中使用以下代码,

<table class="table table-lg">
<thead >
    <tr class="filters">

       <th><input type="text" class="form-control" placeholder="Project" disabled></th>
        <th><input type="text" class="form-control" placeholder="Employee" disabled></th>
        <th><input type="text" class="form-control" placeholder="Task" disabled></th>
        <th><input type="text" class="form-control" placeholder="Expense" disabled></th>
        <th><input type="text" class="form-control" placeholder="Amount" disabled></th>
        <th><input type="text" class="form-control" placeholder="Paid/Not" disabled></th>
        <th><input type="text" class="form-control" placeholder="Client" disabled></th>
        <th><input type="text" class="form-control" placeholder="Cost" disabled></th>
        <th><input type="text" class="form-control" placeholder="Income " disabled></th>
        <th><input type="text" class="form-control" placeholder="Date" disabled></th>

    </tr>
</thead>
<tbody>
<?php
if(isset($view_data1) && is_array($view_data1) && count($view_data1)): $i=1;

foreach ($view_data1 as $key => $data) 
 
   $number_of_task[$data['projectname']][] =  $data['task'];


foreach ($view_data1 as $key => $data)  
$array = array();
?>

<tr <?php if($i%2==0)echo 'class="even"';elseecho'class="odd"';?>>

<?php if(!in_array($data['projectname'],$array))  
 $array[] = $data['projectname'];
?>
    <td><?php echo $data['projectname']; ?></td> 
    <td><?php echo $data['employee']; ?></td> 
<?php   else  ?>    
<td colspan="2" rowspan="<?php echo count($number_of_task[$data['projectname']]); ?>"> </td> 
    <td><?php echo $data['task']; ?></td> 
    <td><?php echo $data['ExpenseName']; ?></td> 
    <td><?php echo $data['ExpenseAmount']; ?></td>
    <td><?php echo $data['pn']; ?></td>  
   <td><?php echo $data['cname']; ?></td>
   <td><?php echo $data['taskcost']; ?></td> 
   <td><?php echo $data['amount']; ?></td> 
   <td><?php echo $data['datetimepicker_mask']; ?></td> 
</tr>
<?php
    $i++;
      

    else:
?>
<tr>
    <td colspan="7" align="center" >No Records Found..</td>
</tr>
<?php
    endif;
?>
</tbody>                
</table>

【讨论】:

【参考方案3】:
if($key == 0)
   $project_name = $data['projectname'];
   $first = true;
else
   $first = false;
   if($project_name != $data['projectname'])
      $project_name = $data['projectname'];
      $first = true;
   



<td><?php     
if($first == true)
  ehco $data['projectname'];
else
  echo "";

</td>

在你的 foreach 中尝试这样,我没有厌倦它,但它可能会起作用,你可能会得到一些想法。在第一个循环中,它将检查 $key,首先它将为 0,如果 $key 不为 0,则 $first 变量将为真,然后它将检查 $project 名称到循环项目名称,如果它不匹配它将替换它.对您要删除或清空的客户端和其他列使用相同的内容。我希望这行得通,或者它能给你一些想法。

【讨论】:

【参考方案4】:

首先,你应该重新排列 $viewdata1 数据结构。基本上,将主要项目数据字段和任务数据字段分开,然后以更好的方式将它们组合起来..

我的假设是: 1. [ task, ExpenseName, ExpenseAmount, pn, cname, taskcost, amount, datetimepicker_mask ] => 都是任务数据字段 2. [ projectname , employee ] => 都是项目数据字段

在您的 model(我们将其命名为 project_model)中,1. 有一个 ONLY 查询功能strong> [ projectname or employee or the projectID or all other project data fields ] ,我们将其命名为 allProject() 。确保它返回类似

[ 
  0 => [ 
          'id' => 101,
          'projectName' => 'Desktop App' , 
          other project data fields
        ] , 
  1 => [ 
          'id' => 102,
          'projectName' => 'Mobile App' , 
          other project data fields
        ] , 
  2 => [ 
          'id' => 103,
          'projectName' => 'Andriod App' , 
          other project data fields
        ] , 
  etc... 
]

2. 有一个基于 [ projectname 或 projectID ] 搜索/查询 [ task, ExpenseName, ExpenseAmount, pn, cname, taskcost, amount, datetimepicker_mask ] 的功能......我们来命名它task_query(projectID) ... 确保它返回类似

[
  0 => [ 
            'id' => 222,
            'projectID' => 101
            'task' => 'task 1',
            'description' => 'description 1',
            other task data fields
          ] , 
  1 => [ 
            'id' => 236,
            'projectID' => 101
            'task' => 'task 2',
            'description' => 'description 2',
            other task data fields
          ] , 
  2 => [ 
            'id' => 245,
            'projectID' => 101
            'task' => 'task 3',
            'description' => 'description 3',
            other task data fields
          ] , 
  etc.
]

控制器中,

$data['viewdata1'] = $this->project_model->allProject();
foreach($data['viewdata1'] as $key => $value)

  $value['taskData'] = $this->project_model->task_query($value['id']);

$data['viewdata1'] 应该看起来像...

[ 
  0 => [ 
          'id' => 101,
          'projectName' => 'Desktop App' , 
          other project data fields,
          'taskData' => [
                          0 => [ 
                                    'id' => 222,
                                    'projectID' => 101,
                                    'task' => 'task 1',
                                    'description' => 'description 1',
                                    other task data fields
                                  ] , 
                          1 => [ 
                                    'id' => 236,
                                    'projectID' => 101,
                                    'task' => 'task 2',
                                    'description' => 'description 2',
                                    other task data fields
                                  ] , 
                          2 => [ 
                                    'id' => 245,
                                    'projectID' => 101,
                                    'task' => 'task 3',
                                    'description' => 'description 3',
                                    other task data fields
                                  ] , 
                          etc.
                        ]
        ] , 
  1 => [ 
          'id' => 102,
          'projectName' => 'Mobile App' , 
          other project data fields,
          'taskData' => [
                          0 => [ 
                                    'id' => 123,
                                    'projectID' => 102,
                                    'task' => 'task 1b',
                                    'description' => 'description 1b',
                                    other task data fields
                                  ] , 
                          1 => [ 
                                    'id' => 124,
                                    'projectID' => 102,
                                    'task' => 'task 2b',
                                    'description' => 'description 2b',
                                    other task data fields
                                  ] , 
                          2 => [ 
                                    'id' => 125,
                                    'projectID' => 102,
                                    'task' => 'task 3b',
                                    'description' => 'description 3b',
                                    other task data fields
                                  ] , 
                          etc.
                        ]
        ] , 
  2 => [ 
          'id' => 103,
          'projectName' => 'Andriod App' , 
          other project data fields,
          'taskData' => [
                          0 => [ 
                                    'id' => 567,
                                    'projectID' => 103,
                                    'task' => 'task 1c',
                                    'description' => 'description 1c',
                                    other task data fields
                                  ] , 
                          1 => [ 
                                    'id' => 568,
                                    'projectID' => 103,
                                    'task' => 'task 2c',
                                    'description' => 'description 2c',
                                    other task data fields
                                  ] , 
                          2 => [ 
                                    'id' => 569,
                                    'projectID' => 103,
                                    'task' => 'task 3c',
                                    'description' => 'description 3c',
                                    other task data fields
                                  ] , 
                          etc.
                        ]
        ] , 
  etc... 
]

一切准备就绪

<table class="table table-lg">
<thead >
    <tr class="filters">

       <th><input type="text" class="form-control" placeholder="Project" disabled></th>
        <th><input type="text" class="form-control" placeholder="Employee" disabled></th>
        <th><input type="text" class="form-control" placeholder="Task" disabled></th>
        <th><input type="text" class="form-control" placeholder="Expense" disabled></th>
        <th><input type="text" class="form-control" placeholder="Amount" disabled></th>
        <th><input type="text" class="form-control" placeholder="Paid/Not" disabled></th>
        <th><input type="text" class="form-control" placeholder="Client" disabled></th>
        <th><input type="text" class="form-control" placeholder="Cost" disabled></th>
        <th><input type="text" class="form-control" placeholder="Income " disabled></th>
        <th><input type="text" class="form-control" placeholder="Date" disabled></th>

    </tr>
</thead>
<tbody>
<?php
if(isset($view_data1) && is_array($view_data1) && count($view_data1)): $i=1;
foreach ($view_data1 as $key1 => $data1)  
    $howManyTasks = count($data1['taskData']);
      ?>

      <tr <?php if($i%2==0)echo 'class="even"';elseecho'class="odd"';?>>
          <td rowspan="<?= $howManyTasks ?>"><?php echo $data1['projectname']; ?></td> 
          <td rowspan="<?= $howManyTasks ?>"><?php echo $data1['employee']; ?></td> 

          <?php foreach($data1['taskData'] as $key2 => $data2) ?>
                <td><?php echo $data2['task']; ?></td> 
                <td><?php echo $data2['ExpenseName']; ?></td> 
                <td><?php echo $data2['ExpenseAmount']; ?></td>
                <td><?php echo $data2['pn']; ?></td>  
               <td><?php echo $data2['cname']; ?></td>
               <td><?php echo $data2['taskcost']; ?></td> 
               <td><?php echo $data2['amount']; ?></td> 
               <td><?php echo $data2['datetimepicker_mask']; ?></td> 
          </tr>
          <?php if($key2 != ($howManyTasks-1) ) ?> <tr> <?php  ?>
          <?php  ?>
      <?php
          $i++;
      
    else:
?>
<tr>
    <td colspan="7" align="center" >No Records Found..</td>
</tr>
<?php
    endif;
?>
</tbody>                
</table>

【讨论】:

【参考方案5】:

将您的查询编辑为SELECT * FROM expenses, income WHERE expenses.projectname=income.projectname AND expenses.task=income.task Blockquote GROUP BY PROJECTID

【讨论】:

【参考方案6】:

逻辑

我实现的隐藏逻辑,按以下顺序工作:

初始条件 主循环 条件检查 记住最后一个条件

HTML 表和 PHP 如下所示:

<table border="1">
    <thead>
        <tr>
            <th>Project</th>
            <th>Client</th>
            <th>Project Start</th>
            <th>Project End</th>
            <th>Task</th>
            <th>Description</th>
            <th>Commission</th>
            <th>Task Start</th>
            <th>Task End</th>
        </tr>
    </thead>
    <tbody>
        <!-- Initial Condition -->
        <?php $the_project = "";?>

        <!-- Main Loop -->
        <?php foreach ($data as $x => $datum)  ?>
            <tr>

            <!-- Condition Check -->
            <!-- If not the same with the last project, it will show the columns. -->
            <!-- That's why the Initial Condition is empty so that the 1st time this Condition Check run will always result true. -->
            <?php if($datum['project'] != $the_project)  ?>
                <td><?php echo $datum['project'];?></td>
                <td><?php echo $datum['client'];?></td>
                <td><?php echo $datum['proj_start'];?></td>
                <td><?php echo $datum['proj_end'];?></td>
            <?php  else  ?>
                <td colspan="4">&nbsp;</td>
            <?php  ?>
            <!-- /Condition Check -->

                <td><?php echo $datum['task'];?></td>
                <td><?php echo $datum['desc'];?></td>
                <td><?php echo $datum['commission'];?></td>
                <td><?php echo $datum['task_start'];?></td>
                <td><?php echo $datum['task_end'];?></td>
            </tr>

            <!-- Remember Last Condition -->
            <?php $the_project = $datum['project'];?>
        <?php ?>   
        <!-- /Main Loop -->     
    </tbody>
</table>

您不必比较所有ProjectClientStart DateEnd Date,假设同一个项目将具有相同的ClientStart DateEnd Date。这就是为什么它只比较项目 ($the_project)。

数据

您可以将其用作示例数据,其作用类似于查询中的数据。

<?php 
    $data = array(
        array(
            "project"   =>  "Mobile App",
            "client"    =>  "Client One",
            "proj_start"=>  "12-12-2017",
            "proj_end"  =>  "12-12-2019",
            "task"      =>  "task 1",
            "desc"      =>  "desc 1",
            "commission"=>  "1000",
            "task_start"=>  "01-01-2018",
            "task_end"  =>  "10-01-2018"
        ),
        array(
            "project"   =>  "Mobile App",
            "client"    =>  "Client One",
            "proj_start"=>  "12-12-2017",
            "proj_end"  =>  "12-12-2019",
            "task"      =>  "task 2",
            "desc"      =>  "desc 2",
            "commission"=>  "1000",
            "task_start"=>  "01-02-2018",
            "task_end"  =>  "10-02-2018"
        ),
        array(
            "project"   =>  "Mobile App",
            "client"    =>  "Client One",
            "proj_start"=>  "12-12-2017",
            "proj_end"  =>  "12-12-2019",
            "task"      =>  "task 3",
            "desc"      =>  "desc 3",
            "commission"=>  "1000",
            "task_start"=>  "01-04-2018",
            "task_end"  =>  "10-05-2018"
        ),
        array(
            "project"   =>  "Web App",
            "client"    =>  "Client Two",
            "proj_start"=>  "12-12-2017",
            "proj_end"  =>  "12-12-2019",
            "task"      =>  "task 1",
            "desc"      =>  "desc 1",
            "commission"=>  "1000",
            "task_start"=>  "01-01-2018",
            "task_end"  =>  "10-01-2018"
        ),
        array(
            "project"   =>  "Web App",
            "client"    =>  "Client Two",
            "proj_start"=>  "12-12-2017",
            "proj_end"  =>  "12-12-2019",
            "task"      =>  "task 2",
            "desc"      =>  "desc 2",
            "commission"=>  "1000",
            "task_start"=>  "01-02-2018",
            "task_end"  =>  "10-02-2018"
        )
    );
?>

提示 1

我认为 $i 不再需要,因为您可以在代码示例中从 $key 获得相同的结果。唯一的区别是$key从0开始。

提示 2

如果你还需要$i,你可以简化这段代码

<tr <?php if($i%2==0)echo 'class="even"';elseecho'class="odd"';?>>

进入

<tr class="<?php echo ($i%2==0) ? "even":"odd" ?>">

提示 3

我不知道&lt;tr&gt; 中的类偶数或奇数的目的,但如果您想为每个类设计不同的颜色,您可能需要检查Bootstrap 并将其设置为&lt;table class="table table-striped"&gt;

【讨论】:

【参考方案7】:

如果我理解正确,您可以尝试以下操作。

<?php

$arrData = [
    [
        "projectname" => "First Project",
        "employee" => "First Client",
        "projectstart" => "12-12-2017",
        "projectend" => "12-12-2019",
        "task" => "Task 1",
        "description" => "Description 1",
        "commission" => "1000",
        "taststart" => "01-01-2018",
        "taskend" => "10-01-2018"
    ],
    [
        "projectname" => "First Project",
        "employee" => "First Client",
        "projectstart" => "12-12-2017",
        "projectend" => "12-12-2019",
        "task" => "Task 2",
        "description" => "Description 2",
        "commission" => "1000",
        "taststart" => "01-01-2018",
        "taskend" => "10-01-2018"
    ],
    [
        "projectname" => "First Project",
        "employee" => "First Client",
        "projectstart" => "12-12-2017",
        "projectend" => "12-12-2019",
        "task" => "Task 3",
        "description" => "Description 3",
        "commission" => "1000",
        "taststart" => "01-01-2018",
        "taskend" => "10-01-2018"
    ],

    [
        "projectname" => "Second Project",
        "employee" => "Second Client",
        "projectstart" => "12-12-2017",
        "projectend" => "12-12-2019",
        "task" => "Task 4",
        "description" => "Description 4",
        "commission" => "1000",
        "taststart" => "01-01-2018",
        "taskend" => "10-01-2018"
    ],
    [
        "projectname" => "Second Project",
        "employee" => "Second Client",
        "projectstart" => "12-12-2017",
        "projectend" => "12-12-2019",
        "task" => "Task 5",
        "description" => "Description 5",
        "commission" => "1000",
        "taststart" => "01-01-2018",
        "taskend" => "10-01-2018"
    ],
    [
        "projectname" => "Second Project",
        "employee" => "Second Client",
        "projectstart" => "12-12-2017",
        "projectend" => "12-12-2019",
        "task" => "Task 6",
        "description" => "Description 6",
        "commission" => "1000",
        "taststart" => "01-01-2018",
        "taskend" => "10-01-2018"
    ],


];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="container">
    <table class="table table-striped table-hover">
    <thead>
     <tr>
        <th>Project</th>
        <th>Client</th>
        <th>Project start on</th>
        <th>Project end on</th>
        <th>task</th>
        <th>description</th>
        <th>commission</th>
        <th>task start on</th>
        <th>task end on</th>
     </tr>
    </thead>
    <tbody>
    <?php
    $strSavedProjectName = false;
    foreach($arrData AS $arrItem)
    
    ?>
     <tr>
        <?php
        if (!$strSavedProjectName || $strSavedProjectName!= $arrItem['projectname'])    :
        ?>
        <td><?=$arrItem['projectname']; ?>
        <td><?=$arrItem['employee']; ?>
        <td><?=$arrItem['projectstart']; ?>
        <td><?=$arrItem['projectend']; ?>
        <?php
        else :
        ?>
        <td colspan="4"></td>
        <?php
        endif;
        ?>
        <td><?=$arrItem['task']; ?>
        <td><?=$arrItem['description']; ?>
        <td><?=$arrItem['commission']; ?>
        <td><?=$arrItem['taststart']; ?>
        <td><?=$arrItem['taskend']; ?>
     </tr>
    <?php
        $strSavedProjectName = $arrItem['projectname'];
    
    ?>
    </tbody>
    </table>
</div>
    <!-- jQuery (necessary for Bootstrap's javascript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>

这是一个基于您的数据的完整示例 - 因此您应该能够轻松地将其转换为您的视图。

【讨论】:

【参考方案8】:

我没有测试这段代码,但想法是记住最后一个键(在本例中为项目名称和员工),然后将其与当前键进行比较。

您的示例代码中的字段与您的 Word 文档示例表不匹配,所以我只使用了您的代码示例。

    <tbody>

        <?php
        if (isset($view_data1) && is_array($view_data1) && count($view_data1)) 

            $i = 1;
            $last_key = '';

            foreach ($view_data1 as $key => $data) 

                $stripe = 'odd';
                if ($i % 2 == 0) 
                     $stripe = 'even';
                
                echo "<tr class='$stripe'>";
                $i++;

                $current_key = $data['projectname'] . $data['employee'];

                if ($current_key !== $last_key) 

                    echo "<td>" . $data['projectname'] . "</td>";
                    echo "<td>" . $data['employee'] . "</td>";
                 else 

                    echo "<td colspan='2'></td>";
                

                $last_key = $current_key;
                ?>

            <td><?php echo $data['task']; ?></td>
            <td><?php echo $data['ExpenseName']; ?></td>
            <td><?php echo $data['ExpenseAmount']; ?></td>
            <td><?php echo $data['pn']; ?></td>
            <td><?php echo $data['cname']; ?></td>
            <td><?php echo $data['taskcost']; ?></td>
            <td><?php echo $data['amount']; ?></td>
            <td><?php echo $data['datetimepicker_mask']; ?></td>
        </tr>
        <?php
    
 else 
    ?>
    <tr><td colspan="10" align="center" >No Records Found..</td></tr>
    <?php

?>

</tbody>
</table>

【讨论】:

这可能是最简单的方法,或多或少地使用程序的其余部分。【参考方案9】:

不要这样合并表格。在模型中使用连接查询并通过控制器在视图中调用它

public function getExpenses()
     $this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
     $this->db->from('addexpense');
     $this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
     $query = $this->db->get();
     return $query->result();

【讨论】:

【参考方案10】:

不要同时选择两个表。加入他们的条件。

SELECT expenses.*,income.*,expenses.id as p_id FROM expenses
join income ON expenses.task=income.task AND expenses.projectname=income.projectname

更改每个部分的视图

<table class="table table-lg">
<thead >
    <tr class="filters">

       <th ><input type="text" class="form-control" placeholder="Project" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Employee" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Task" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Expense" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Amount" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Paid/Not" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Client" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Cost" disabled></th>
        <th ><input type="text" class="form-control" placeholder="Income " disabled></th>
        <th ><input type="text" class="form-control" placeholder="Date" disabled></th>
    </tr>
</thead>
<tbody>
<?php
if(isset($view_data1) && is_array($view_data1) && count($view_data1)): $i=1;
$is_exists=array();
foreach ($view_data1 as $key => $data)  
?>

<tr <?php if($i%2==0)echo 'class="even"';elseecho'class="odd"';?>>
<?php
    if(!in_array($data['p_id'], $is_exists))
        $is_exists[]=$data['p_id'];
    ?>
        <td><?php echo $data['projectname']; ?></td> 
        <td><?php echo $data['employee']; ?></td> 
        <td><?php echo $data['task']; ?></td> 
        <td><?php echo $data['ExpenseName']; ?></td> 
    <?php
    else
     echo "<td rowspan='4'></td>";
    
    ?>

   <td><?php echo $data['ExpenseAmount']; ?></td>
   <td><?php echo $data['pn']; ?></td>  
   <td><?php echo $data['cname']; ?></td>
   <td><?php echo $data['taskcost']; ?></td> 
   <td><?php echo $data['amount']; ?></td> 
   <td><?php echo $data['datetimepicker_mask']; ?></td> 
</tr>
<?php
    $i++;
      
    else:
?>
<tr>
    <td colspan="7" align="center" >No Records Found..</td>
</tr>
<?php
    endif;
?>

</tbody>                
</table>

【讨论】:

这是我的查看代码 Mr.Kundan。我没有使用 ms word 设计的表格的视图代码。我想更改上面的视图代码,以便获得我从 ms word 设计的表格。 非常感谢您的回答。但它没有任何改变 你是否也在 foreach.. 中加入 else 条件? 让我们continue this discussion in chat。 Yes Mr.Kundan..else 如果没有记录会显示错误信息

以上是关于如何使用 php 和 codeigniter 隐藏检索到的数据值的重复部分的主要内容,如果未能解决你的问题,请参考以下文章

在 PHP codeigniter 中动态获取数据时如何隐藏 td?

如何在 Codeigniter 上隐藏 URL 字符串中的数据

从 url 隐藏目录名称和 index.php

CodeIgniter框架隐藏index.php

IIS 环境 CodeIgniter 隐藏 URL 中的 index.php

使用 Codeigniter,我如何向其他开发人员隐藏 Paypal 凭据?