<html>
<head>
<title>truck page</title>
<style type="text/css">
tr {
padding: 4px;
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$db = new mysqli("localhost", "root", "", "db1");
function getdata($form_element_name) {
$var = strip_tags($_GET[$form_element_name]);
return $var;
}
?>
<fieldset>
<legend> add a new truck </legend>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
<label>truck number <input type="text" name="truck_number" /></label>
<label>owner name <input type="text" name="truck_owner_name" /></label>
<label>owner phone <input type="text" name="truck_owner_ph" maxlength="10" /></label>
<input type="submit" name="truck_add" value="add" />
<input type="reset" />
</form>
<?php if(isset($_GET['truck_add'])) {
$truck_number = getdata('truck_number');
$truck_owner_name = getdata('truck_owner_name');
$truck_owner_ph = getdata('truck_owner_ph');
$sql_truck_add = "INSERT INTO truck (truck_number, truck_owner_name, truck_owner_ph)
VALUES ('$truck_number', '$truck_owner_name', '$truck_owner_ph' )";
$result_truck_add = $db->query($sql_truck_add);
header("Location: http://localhost/bkp/truckpage.php");
} ?>
</fieldset>
<table>
<tr>
<th>truck number</th>
<th>owner name</th>
<th>phone</th>
<th>operation</th>
<th></th>
</tr>
<?php
$sql_truck_retrieve = "SELECT * from truck";
$result_truck_retrieve= $db->query($sql_truck_retrieve);
while($row=$result_truck_retrieve->fetch_assoc()) {
$truck_num_current = $row['truck_number']; ?>
<tr id="<?php echo "{$truck_num_current}"; ?>">
<td><?php echo "{$row['truck_number']}"; ?></td>
<td><?php echo "{$row['truck_owner_name']}"; ?></td>
<td><?php echo "{$row['truck_owner_ph']}"; ?></td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_numcur" value="<?php echo "{$row['truck_number']}"; ?>" />
<input type="submit" name="truck_update" value="update" />
<input type="submit" name="truck_delete" value="delete" />
</form>
</td>
<td>
<?php // action to be taken if either update or delete is selected
if(isset($_GET['truck_update'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) { ?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_update_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="text" name="truck_num_update" placeholder="truck number" />
<input type="text" name="owner_name_update" placeholder="owner name" />
<input type="text" name="owner_phone_update" placeholder="owner phone" />
<input type="submit" value="save" name="truck_update_yes" />
<input type="submit" value="cancel" name="truck_update_no" />
</form>
<?php }
}
else {
if(isset($_GET['truck_delete'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<span>confirm? </span>
<input type="hidden" name="truck_del_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="submit" name="truck_delete_yes" value="yes" />
<input type="submit" name="truck_delete_no" value="no" />
</form>
<?php }
}
}
//
// action to be taken after delete confirmation
if(isset($_GET['truck_delete_yes'])) {
$truck_del_num = $_GET['truck_del_num'];
$sql_truck_del = "DELETE from truck WHERE truck_number = '{$truck_del_num}'";
$result_truck_del = $db->query($sql_truck_del);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_delete_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
//---------------------------------------------------------------------------
//header("Location: http://localhost/bkp/truckpage.php/#$truck_num_current");
//---------------------------------------------------------------------------
}
}
//
// action to be taken after update confirmation
if(isset($_GET['truck_update_yes'])) {
$truck_update_num = $_GET['truck_update_num'];
$truck_num_update = getdata('truck_num_update');
$owner_name_update = getdata('owner_name_update');
$owner_phone_update = getdata('owner_phone_update');
$sql_truck_update = "UPDATE truck SET truck_number = '$truck_num_update', truck_owner_name = '$owner_name_update', truck_owner_ph = '$owner_phone_update' WHERE truck_number = '{$truck_update_num}' ";
$result_truck_update = $db->query($sql_truck_update);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_update_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
}
}
//
?>
</td>
</tr>
<?php } ?>
</table>
</body>
我正在尝试从db表中提取值并将它们添加为表行。行正确显示但是当我尝试为表行添加css样式时它不起作用。我试图给一个边框和填充。我是网络开发的新手,无法理解为什么css部分不工作。任何人都可以告诉如何让css部分在这个PHP代码中工作?
答案 0 :(得分:1)
问题在于你不能非常有效地设置tr
元素的样式。您需要定位父table
或子td
元素,以获得您正在寻找的效果。
查看此JSFiddle示例:http://jsfiddle.net/v6zNt/
<强> CSS:强>
table {
border: 1px solid #000;
}
/* notice how these styles does not apply anywhere */
tr {
border: 1px solid green;
padding: 4px;
}
td {
border: 1px solid red;
padding: 4px;
}
HTML:
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
您可以设置表格样式,只是他们有自己的一套规则需要注意。这是一个很好的介绍:http://css-tricks.com/complete-guide-table-element/