2017-07-25 201 views
0

我在这里看到类似的问题,但是当我尝试实现它时,代码是不同的。我使用php和html连接到我的db2数据库,从表中提取数据并在HTML表中显示拉数据。我试图添加功能到我的'删除'按钮,并希望能够标记一个复选框,突出显示表中选定的行。我正在处理我的delete.php,但现在我想实现这一行选择。我在桌子的一开始就找到了一行代码,但是如何突出显示该行?这是我到目前为止有:表格中的复选框选择

<html> 
<head><title>DB Testing</title></head> 




<style> 
table{ 
    font-family: arial, sans-serif; 
    border-collapse: collapse; 
    width: 80%; 

} 
td, th { 
    border: 1px solid #dddddd; 
    text-align: left; 
    padding: 8px; 
} 


</style> 
<body> 

<?php 
//db2 express c (v10.5) in local 
$database = "db"; 
$user = "user"; 
$password = "password"; 


//create connection 
$conn = db2_connect($database, $user, $password); 

//check connection 
if($conn) { 
    //echo "DB2 Connection succeeded.<br><br>"; 
    } else{ 
    exit("failed".db2_conn_errormsg()); 
    } 

//select fields from database 
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY, 
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D' 
and AVAILABILITY = 'Y'"; 
$stmt = db2_prepare($conn, $sql); 

//db2_execute executes a sql statement that was prepared by db2_prepare 
if($stmt){ 
$result = db2_execute($stmt); 
if(!$result){ 
    echo "exec errormsg: " .db2_stmt_errormsg($stmt); 
} 

//the echos below output the data in an html table 
echo '<table border = 1>'; 
echo '<thead>'; 
echo '<tr>'; 

      echo'<th></th>'; 
      echo'<th>A</th>'; 
      echo'<th>B</th>'; 
      echo'<th>START_TIME</th>'; 
      echo'<th>END_TIME</th>'; 
      echo'<th>START_DAY</th>'; 
      echo'<th>END_DAY</th>'; 
      echo'<th>C</th>'; 
      echo'<th>ID</th>'; 
      echo'<th>BUSINESS_AREA</th>'; 
      echo'<th>ENVIRONMENT</th>'; 
echo '</tr>';   
echo '</thead>'; 
echo '<tbody>';  
while($row = db2_fetch_assoc($stmt)) { 


    echo '<tr>'; 
    echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" /></td>"; 
    echo '<td>' . $row['A'] . '</td>'; 
    echo '<td>' . $row['B'] . '</td>'; 
    echo '<td>' . $row['START_TIME'] . '</td>'; 
    echo '<td>' . $row['END_TIME'] . '</td>'; 
    echo '<td>' . $row['START_DAY'] . '</td>'; 
    echo '<td>' . $row['END_DAY'] . '</td>'; 
    echo '<td>' . $row['C'] . '</td>'; 
    echo '<td>' . $row['ID'] . '</td>'; 
    echo '<td>' . $row['BUSINESS_AREA'] . '</td>'; 
    echo '<td>' . $row['ENVIRONMENT'] . '</td>'; 

    echo '</tr>'; 

echo '</tbody>'; 

} 
echo '</table>'; 
}else { 
echo "exec errormsg: ".db2_stmt_errormsg($stmt); 
} 
db2_close($conn); 

?> 

<?php 
function print_r2($val){ 
    echo '<pre>'; 
    print_r($val); 
    echo '</pre>'; 
    } 
    ?> 


</body> 
</html> 

回答

0

你需要一些JavaScript这样的伎俩,看到的片段:

function hl(ob){ 
 
    if(ob.checked == true){ 
 
\t ob.parentNode.parentNode.style.backgroundColor='red'; 
 
    } 
 
    if(ob.checked != true){ 
 
\t ob.parentNode.parentNode.style.backgroundColor='white'; 
 
    } 
 
}
<table border=1 cellspacing=0 cellpadding=5> 
 
\t <tr> 
 
\t \t <td><input type="checkbox" name="checkbox" value="1" onClick="hl(this)" /></td> 
 
\t \t <td>1</td> 
 
\t \t <td>2</td> 
 
\t \t <td>3</td> 
 
\t \t <td>4</td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td><input type="checkbox" name="checkbox" value="2" onClick="hl(this)" /></td> 
 
\t \t <td>1</td> 
 
\t \t <td>2</td> 
 
\t \t <td>3</td> 
 
\t \t <td>4</td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td><input type="checkbox" name="checkbox" value="3" onClick="hl(this)" /></td> 
 
\t \t <td>1</td> 
 
\t \t <td>2</td> 
 
\t \t <td>3</td> 
 
\t \t <td>4</td> 
 
\t </tr> 
 
</table>

编辑

<html> 
<head> 
<title>DB Testing</title> 
<script type="text/javascript"> 
function hl(ob){ 
    if(ob.checked == true){ 
     ob.parentNode.parentNode.style.backgroundColor='red'; 
    } 
    if(ob.checked != true){ 
     ob.parentNode.parentNode.style.backgroundColor='white'; 
    } 
} 
</script> 
<style> 
table{ 
    font-family: arial, sans-serif; 
    border-collapse: collapse; 
    width: 80%; 

} 
td, th { 
    border: 1px solid #dddddd; 
    text-align: left; 
    padding: 8px; 
} 
</style> 
</head> 
<body> 

<?php 
//db2 express c (v10.5) in local 
$database = "db"; 
$user = "user"; 
$password = "password"; 


//create connection 
$conn = db2_connect($database, $user, $password); 

//check connection 
if($conn) { 
    //echo "DB2 Connection succeeded.<br><br>"; 
    } else{ 
    exit("failed".db2_conn_errormsg()); 
    } 

//select fields from database 
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY, 
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D' 
and AVAILABILITY = 'Y'"; 
$stmt = db2_prepare($conn, $sql); 

//db2_execute executes a sql statement that was prepared by db2_prepare 
if($stmt){ 
$result = db2_execute($stmt); 
if(!$result){ 
    echo "exec errormsg: " .db2_stmt_errormsg($stmt); 
} 
//the echos below output the data in an html table 
echo '<table border = 1>'; 
echo '<thead>'; 
echo '<tr>'; 

      echo'<th></th>'; 
      echo'<th>A</th>'; 
      echo'<th>B</th>'; 
      echo'<th>START_TIME</th>'; 
      echo'<th>END_TIME</th>'; 
      echo'<th>START_DAY</th>'; 
      echo'<th>END_DAY</th>'; 
      echo'<th>C</th>'; 
      echo'<th>ID</th>'; 
      echo'<th>BUSINESS_AREA</th>'; 
      echo'<th>ENVIRONMENT</th>'; 
echo '</tr>';   
echo '</thead>'; 
echo '<tbody>';  
while($row = db2_fetch_assoc($stmt)) { 


    echo '<tr>'; 
    echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" onClick=\"hl(this)\" /></td>"; 
    echo '<td>' . $row['A'] . '</td>'; 
    echo '<td>' . $row['B'] . '</td>'; 
    echo '<td>' . $row['START_TIME'] . '</td>'; 
    echo '<td>' . $row['END_TIME'] . '</td>'; 
    echo '<td>' . $row['START_DAY'] . '</td>'; 
    echo '<td>' . $row['END_DAY'] . '</td>'; 
    echo '<td>' . $row['C'] . '</td>'; 
    echo '<td>' . $row['ID'] . '</td>'; 
    echo '<td>' . $row['BUSINESS_AREA'] . '</td>'; 
    echo '<td>' . $row['ENVIRONMENT'] . '</td>'; 

    echo '</tr>'; 

echo '</tbody>'; 

} 
echo '</table>'; 
}else { 
echo "exec errormsg: ".db2_stmt_errormsg($stmt); 
} 
db2_close($conn); 

?> 

<?php 
function print_r2($val){ 
    echo '<pre>'; 
    print_r($val); 
    echo '</pre>'; 
    } 
    ?> 


</body> 
</html> 
+0

我应该在哪里添加这个代码? – mruby

+0

@mruby,它不是一个复制和粘贴代码,它是一个例子。检查我的答案,我已经更新了它。 (见**编辑**) – IonS