Dus*_*hee 12 php mysql codeigniter
由于这个MySQL语句,我已经获得了下表
SELECT * FROM expenses, income
WHERE expenses.projectname=income.projectname
AND expenses.task=income.task
Run Code Online (Sandbox Code Playgroud)
在此表中,一个项目有许多任务.因此项目,客户,项目开始和结束日期列无意义地重复.如何才能为所有任务显示一次?如何在这里应用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"';}else{echo'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>
Run Code Online (Sandbox Code Playgroud)
不要同时选择两个表。加入他们的条件。
SELECT expenses.*,income.*,expenses.id as p_id FROM expenses
join income ON expenses.task=income.task AND expenses.projectname=income.projectname
Run Code Online (Sandbox Code Playgroud)
更改每个部分的视图
<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"';}else{echo'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>
Run Code Online (Sandbox Code Playgroud)