我在做动态表时遇到问题
我的表结构如下:
======================================================================
tender_id | coc | datasheet | datecode | shelflife | Suppliername
======================================================================
201 | No | Yes | Yes | No | Supplier1
201 | Yes | No | Yes | No | Supplier2
201 | No | No | No | No | Supplier3
我在PHP中的预期结果是
=============================================
COC | Datasheet | Date Code | Supplier Name
=============================================
- Yes Yes Supplier1
Yes - Yes Supplier2
结果应仅显示值是否为“是”。如果所有值都为“否”,则不应显示标题。如果One值为Yes且One值为No,则还应显示标题。例如,在上述条件下,所有供应商的保质期值均为“否”。在这种情况下,即使是它不应该显示的标题。我试过一些脚本,但是有一些问题。
这是我试过的脚本:
$sql_shelf = "SELECT tender_id, suppliername, coc, date_code, shelf_life , datasheet FROM comparitive_statement1 WHERE (coc='Yes' OR technical_compliance='Yes' OR date_code='Yes' OR shelf_life='Yes' OR datasheet='Yes') tender_id='$tender_id' group by suppliername";
$result = mysql_query($sql_shelf) or die($sql_shelf."<br/><br/>".mysql_error());
$i = 0;
while ($list = mysql_fetch_array($result)) {
if($i == 0){
echo '<tr>';
$sh = $list['shelf_life'];
if ($sh=="No") {
} else {
echo '<td><b>Shelf Life</b></td>';
}
echo '</tr>';
$i++;
}
echo '<tr>';
$sl1 = $list['shelf_life'];
if ($sl1=="No") {
} else {
echo "<td>{$list['shelf_life']}</td>";\
echo "</tr>";
}
++$i;
}
脚本有点长,所以我刚刚提供了与shelflife相关的记录。
答案 0 :(得分:0)
不是说这是最有效的方式,但是......
// $data is the array you have after while ($list = mysql_fetch_array($result)) { $data[]=$row; }
$data = [
[ 'tender_id'=>201, 'coc'=>'No', 'datasheet'=>'Yes', 'datecode'=>'Yes', 'shelflife'=>'No', 'Suppliername'=>'Supplier1' ],
[ 'tender_id'=>201, 'coc'=>'Yes', 'datasheet'=>'No', 'datecode'=>'Yes', 'shelflife'=>'No', 'Suppliername'=>'Supplier2' ],
[ 'tender_id'=>201, 'coc'=>'No', 'datasheet'=>'No', 'datecode'=>'No', 'shelflife'=>'No', 'Suppliername'=>'Supplier3' ]
];
// remove all 'No' fields
$data = array_map(
function($e) {
return array_filter($e, function($f) { return 'No'!==$f; });
},
$data
);
/* => $data == [
[ 'tender_id'=>201'datasheet'=>'Yes''datecode'=>'Yes''Suppliername'=>'Supplier1'],
[ 'tender_id'=>201'coc'=>'Yes''datecode'=>'Yes''Suppliername'=>'Supplier2'],
[ 'tender_id'=>201'Suppliername'=>'Supplier3']
]
*/
// now get an array of the keys present in any row
$fields = array_reduce(
$data,
function($carry, $item) {
return $carry + array_keys($item);
},
[]
);
/* => $fields == [ 'tender_id', 'datasheet', 'datecode', 'Suppliername']
after the filter no row in $data contained an element 'shelflife'=>...
so it's also not present in $fields
*/
// make a "template" for empty fields
$fields = array_combine( $fields, array_fill(0, count($fields), ' - '));
/* => $fields == [ 'tender_id'=>' - ', 'datasheet'=>' - ', 'datecode'=>' - ', 'Suppliername'=>' - '] */
// now merge all rows in $data with this template, i.e. if a field is missing in a row add the default element
$data = array_map(
function($e) use ($fields) {
return $e + $fields;
},
$data
);
/* => $data == [
[ 'tender_id'=>201, 'datasheet'=>'Yes','datecode'=>'Yes', 'Suppliername'=>'Supplier1'],
[ 'tender_id'=>201, 'coc'=>'Yes', 'datecode'=>'Yes', 'Suppliername'=>'Supplier2', 'datasheet'=>' - '],
[ 'tender_id'=>201, 'Suppliername'=>'Supplier3', 'datasheet'=>' - ', 'datecode'=>' - ']
]
*/
现在您可以使用$ data 和 $字段来打印表格。
答案 1 :(得分:-1)
$sql_shelf = "SELECT * FROM comparitive_statement1 WHERE (coc='Yes' OR datecode='Yes' OR shelflife='Yes' OR datasheet='Yes') group by suppliername";
$result = mysql_query($sql_shelf) or die($sql_shelf."<br/><br/>".mysql_error());
$output="<table border='1'>";
$output .= "<tr>
<td>COC</td>
<td>Datasheet</td>
<td class='deleteColumnDatecode'>DateCode</td>
<td class='deleteColumn'>Shelf Life</td>
<td>SupplierName</td></tr>";
$shelflife = false;
$datecode=false;
while ($list = mysql_fetch_array($result)) {
$output .= "<tr>
<td>".$list['coc']."</td>
<td>".$list['datasheet']."</td>
<td class='deleteColumnDatecode'>".$list['datecode']."</td>
<td class='deleteColumn'>".$list['shelflife']."</td>
<td>".$list['Suppliername']."</td>
</tr>";
if( strcasecmp($list['shelflife'],"Yes")== 0) {
$shelflife = true;
}
if( strcasecmp($list['datecode'],"Yes")== 0) {
$datecode = true;
}
}
if( $shelflife ){
$output = str_replace( 'deleteColumn', '', $output);
}
if( $datecode ){
$output = str_replace( 'deleteColumnDatecode', '', $output);
}
echo $output;
?>
<html>
<head>
<title>comparitive_statement1</title>
<style>
td.deleteColumn,td.deleteColumnDatecode {
display: none;
}
</style>
</head>
</html>
答案 2 :(得分:-2)
希望这能解决您的问题
<?php
$table_items = array("coc","datasheet","datecode","shelflife ");
foreach($table_items as $value){
$result = mysqli_num_rows(mysqli_query($con,"SELECT * FROM comparitive_statement1 WHERE ".$value." = 'Yes'"));
if($result>0){
echo "<td>".$result."</td>";
}
}
?>