大家好,我目前有一个地址簿,显示如下信息:
Extension Name Department Email Cellphone
1 jurgen home 1@ 1
2 dawn work 2@ 2
3 mike away 3@ 33
有没有办法按部门显示信息?例如:它显示了每个部门下面的用户,他们基本上将它们分组?温柔:)我是新编码:D
Extension Name Email Cellphone
ALPINE DEALER PRINCIPAL
7813 Garth 123@alpinemotors.co.za 2
7898 Sam 3653@alpinemotors.co.za 4
AFTER-SALES DIRECTOR
7709 Bruce 6342@alpinemotors.co.za 3
继承我的view.php页面的代码(在旁注上我做了一个搜索栏,但不知道如何编码它来主动搜索名字字段,所以当他们在搜索栏中输入时,表格过滤而没有按下输入)
default:
$sql ="SELECT * FROM address ORDER BY name ASC";
$data = mysql_query($sql);
?>
<h2>Alpine VW Extension List</h2>
<table class="tableStyleClass">
<tr>
<th width="100">Extension</th>
<th width="100">Name</th>
<th width="200">Department</th>
<th width="200">Email</th>
<th width="200">Cellphone</th>
<th width="200" colspan="2">Admin</th>
</tr>
<td colspan="5" align="right"><?php if($disable!=1){?><div align="right <a href="<?=$_SERVER['PHP_SELF'];?>?mode=add"?mode=add>Add Contact</a><?php }else{?>Contact Book is Full<?php } ?></div></td>
<?php
$rowColor = 0;
while($info = mysql_fetch_array( $data )){
if($rowColor==0){
?>
<tr class="oddClassStyle">
<?php
$rowColor =1;
}elseif($rowColor==1){
?>
<tr class="evenClassStyle">
<?php
$rowColor = 0;
}
?>
<td><?=$info['Ext'];?></td>
<td><?=$info['name'];?></td>
<td><?=$info['department'];?></td>
<td><a href=mailto:"<?=$info['email'];?>"><?=$info['email'];?></a></td>
<td><?=$info['phone'];?></td>
<td><a href="<?=$_SERVER['PHP_SELF'];?>?id=<?=$info['id'];?>&name=<?=$info['name'];?>&phone=<?=$info['phone'];?>&email=<?=$info['email']; >&mode=edit" >Edit </a></td>
<td><a href="<?=$_SERVER['PHP_SELF'];?>?id=<?=$info['id']; >&mode=remove">Remove</a></td>
</tr>
<?php
}
?>
</table>
<?php
break;
(上面的代码是由下面的答案固定的)
好了,现在我已经将我的编辑按钮排序似乎无法正常工作。它应该显示当前信息,然后一旦编辑,你点击编辑按钮,它会改变该用户的信息。
以下是我之前使用的代码:
case 'edit':
?>
<h2>Editing: <?=$_GET['name'];?></h2>
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=edited" method="post">
<table width="399" class="tableStyleClassTwo">
<tr><td width="87">Name:</td>
<td width="551"><div align="left">
<input type="text" value="<?=$_GET['name'];?>" name="name" />
</div></td></tr>
<tr><td>Phone:</td><td><div align="left">
<input type="text" value="<?=$_GET['phone'];?>" name="phone" />
</div></td></tr>
<tr><td>Email:</td><td><div align="left">
<input type="text" value="<?=$_GET['email'];?>" name="email" />
</div></td></tr>
<tr><td>Extension:</td><td><div align="left">
<input type="text" value="<?=$_GET['Ext'];?>" name="Ext" />
</div></td></tr>
<tr><td colspan="2" align="center"><a href="javascript:history.go(-1);">Back</a> |<input name="Submit" type="submit" value="Save Changes" /></td></tr>
<input type="hidden" name="mode" value="edited">
<input type="hidden" name="id" value="<?=$_GET['id'];?>">
</table>
</form>
<?php
break;
case 'edited':
$name = $_POST['name'];
$phone = $_POST['phone'];
$department = $_POST['department'];
$email = $_POST['email'];
$Ext = $_POST ['Ext'];
$id = $_POST['id'];
$sql = "UPDATE address SET name = '" . $name ."', phone = '" . $phone . "', email = '" . $email . "', Ext = '" . $Ext . "' WHERE id = '" . $id . "'";
mysql_query($sql);
header('location: ' . $_SERVER['PHP_SELF']);
break;
答案 0 :(得分:0)
尝试此查询 -
$sql = "SELECT t1.Ext, t1.name, t1.email, t1.cellphone FROM address t1 join address t2 on t1.id=t2.id ORDER BY t2.department";
编辑:
<?php
$sql = "SELECT t1.Ext, t1.name, t1.email, t1.cellphone FROM address t1 join address t2 on t1.id=t2.id ORDER BY t2.department";
$data = mysql_query($sql);
?>
<h2>Alpine VW Extension List</h2>
<table class="tableStyleClass">
<tr>
<th width="100">Extension</th>
<th width="100">Name</th>
<th width="200">Email</th>
<th width="200">Cellphone</th>
<th width="200" colspan="2">Admin</th>
</tr>
<td colspan="5" align="right"><?php if($disable!=1){?><div align="right <a href="<?=$_SERVER['PHP_SELF'];?>?mode=add"?mode=add>Add Contact</a><?php }else{?>Contact Book is Full<?php } ?></div></td>
<?php
$rowColor = 0;
$deptName = "";
while($info = mysql_fetch_array( $data )){
if($deptName === "" || $deptName != $info['department']) {
$deptName = $info['department'];
echo "<tr> <th colspan=6> $deptName </td> </th> </tr>\n";
}
if($rowColor==0){
?>
<tr class="oddClassStyle">
<?php
$rowColor =1;
}elseif($rowColor==1){
?>
<tr class="evenClassStyle">
<?php
$rowColor = 0;
}
?>
<td><?=$info['Ext'];?></td>
<td><?=$info['name'];?></td>
<td><a href=mailto:"<?=$info['email'];?>"><?=$info['email'];?></a></td>
<td><?=$info['phone'];?></td>
<td><a href="<?=$_SERVER['PHP_SELF'];?>?id=<?=$info['id'];?>&name=<?=$info['name'];?>&phone=<?=$info['phone'];?>&email=<?=$info['email']; >&mode=edit" >Edit </a></td>
<td><a href="<?=$_SERVER['PHP_SELF'];?>?id=<?=$info['id']; >&mode=remove">Remove</a></td>
</tr>
<?php
}
?>
</table>
<?php
break;
?>
答案 1 :(得分:0)
这是另一种方式:
$depts = [];
$res = mysql_query("SELECT * FROM address ORDER BY department, name");
// build the appropriate data structure that organizes contacts into different departments
// by looping through each contact, check if department exist (if not create the department) and add contact to the department
while ($row = mysql_fetch_assoc($res)) {
if (!isset($depts[$row['department']])) {
$depts[$row['department']] = [];
}
$depts[$row['department']][] = $row;
}
?>
<h2>Alpine VW Extension List</h2>
<table class="tableStyleClass">
<tr>
<th width="100">Extension</th>
<th width="100">Name</th>
<th width="200">Email</th>
<th width="200">Cellphone</th>
<th width="200" colspan="2">Admin</th>
</tr>
<tr>
<td colspan="6">
<?php if ($disable != 1) : ?>
<a href="<?= $_SERVER['PHP_SELF'] ?>?mode=add">Add Contact</a>
<?php else : ?>
<span>Contact Book is Full</span>
<?php endif ?>
</td>
</tr>
<?php foreach ($depts as $dname => $contacts) : ?>
<tr>
<td colspan="6"><?= $dname ?></td>
</tr>
<?php foreach ($contacts as $contact) : ?>
<tr>
<td><?= $contact['ext'] ?></td>
<td><?= $contact['name'] ?></td>
<td><a href="mailto:"<?= $contact['email'] ?>"><?= $contact['email'] ?></a></td>
<td><?= $contact['phone'] ?></td>
<td><a href="<?= $_SERVER['PHP_SELF'] ?>?id=<?= $contact['id'] ?>&mode=edit" >Edit </a></td>
<td><a href="<?= $_SERVER['PHP_SELF'] ?>?id=<?= $contact['id'] ?>&mode=remove">Remove</a></td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
</table>
另一方面,请停止使用mysql_*功能。有一个很大的红色警告,表示已弃用有充分理由。请改用mysqli或更好PDO!
更好地利用CSS(并使用较少的属性,如align="right"
)。您可以使用:nth-child选择器(特别是:even
和:odd
)来为您的表格执行交替样式。