我有一个我正在处理的应用程序,这是在Dreamweaver CS5中生成的。代码非常荒谬,制作网站的人无法提供帮助,因此我正在努力解决代码并试图尽可能“简化”其中的一些代码。我更像是初学者 - 中级用php。所以看起来似乎并不那么困难。应用程序“应该”做什么的描述:客户表应该显示数据(它做的)并且应该从表中显示前缀,后缀和状态,但是它具有主键的自动增量ID,文本部分将是不显示,只显示数字。所以我的前任使用if else语句将数字转换为数据(你会在代码中看到这是多么繁琐)。所以这是我的代码:
<?php require_once('Connections/customer.php'); ?><?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_DetailRS1 = 30;
$pageNum_DetailRS1 = 0;
if (isset($_GET['pageNum_DetailRS1'])) {
$pageNum_DetailRS1 = $_GET['pageNum_DetailRS1'];
}
$startRow_DetailRS1 = $pageNum_DetailRS1 * $maxRows_DetailRS1;
$colname_DetailRS1 = "-1";
if (isset($_GET['recordID'])) {
$colname_DetailRS1 = $_GET['recordID'];
}
mysql_select_db($database_customer, $customer);
$query_DetailRS1 = sprintf("SELECT * FROM customer WHERE id = %s", GetSQLValueString ($colname_DetailRS1, "int"));
$query_limit_DetailRS1 = sprintf("%s LIMIT %d, %d", $query_DetailRS1, $startRow_DetailRS1, $maxRows_DetailRS1);
$DetailRS1 = mysql_query($query_limit_DetailRS1, $customer) or die(mysql_error());
$row_DetailRS1 = mysql_fetch_assoc($DetailRS1);
if (isset($_GET['totalRows_DetailRS1'])) {
$totalRows_DetailRS1 = $_GET['totalRows_DetailRS1'];
} else {
$all_DetailRS1 = mysql_query($query_DetailRS1);
$totalRows_DetailRS1 = mysql_num_rows($all_DetailRS1);
}
$totalPages_DetailRS1 = ceil($totalRows_DetailRS1/$maxRows_DetailRS1)-1;
$query = "SELECT id, state_abbr FROM state WHERE id =".$row_DetailRS1['state']."";
$result = mysql_select_db($database_customer, $customer)
or die($query."<br/><br/>".mysql_error());
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="41%" border="0" align="center" cellpadding="2" cellspacing="5">
<tr>
<td width="157">Id</td>
<td width="469"><?php echo $row_DetailRS1['id']; ?></td>
</tr>
<tr>
<td>Prefix</td>
<td><?php if($row_DetailRS1['prefix']==1) {
echo 'Mr.';}
else if($row_DetailRS1['prefix']==2) {
echo 'Mrs.'; }
else if($row_DetailRS1['prefix']==3) {
echo 'Ms.'; }
else if($row_DetailRS1['prefix']==4) {
echo 'Mr. & Mrs.'; }
else if($row_DetailRS1['prefix']==5) {
echo 'Dr.'; }
else if($row_DetailRS1['prefix']==6) {
echo 'Dr. & Mrs.'; }
else if($row_DetailRS1['prefix']==7) {
echo 'Mr. & Dr.'; }
else if($row_DetailRS1['prefix']==8) {
echo 'Drs.'; }
else if($row_DetailRS1['prefix']==9) {
echo 'Hon.';}
else if($row_DetailRS1['prefix']==10) {
echo 'Hon. & Mrs.'; }
else if($row_DetailRS1['prefix']==11) {
echo 'Mr. & Hon.'; }
else if($row_DetailRS1['prefix']==12) {
echo 'Hons.'; }
else if($row_DetailRS1['prefix']==13) {
echo 'Rev.'; }
else if($row_DetailRS1['prefix']==14) {
echo 'Rev. & Mrs.'; }
else if($row_DetailRS1['prefix']==15) {
echo 'Atty.'; }
else if($row_DetailRS1['prefix']==16) {
echo 'Atty. & Mrs.'; }
else if($row_DetailRS1['prefix']==17) {
echo 'Mr. & Atty.'; }
else if($row_DetailRS1['prefix']==18) {
echo 'Attys.'; }
else if($row_DetailRS1['prefix']==19) {
echo 'Sen.'; }
else if($row_DetailRS1['prefix']==20) {
echo 'Sen & Mrs.'; }
else if($row_DetailRS1['prefix']==21) {
echo 'Mrs. & Sen.'; }
else if($row_DetailRS1['prefix']==22) {
echo 'Sens.'; }
else if($row_DetailRS1['prefix']==23) {
echo 'Rep.'; }
else if($row_DetailRS1['prefix']==24) {
echo 'Rep. & Mrs.'; }
else if($row_DetailRS1['prefix']==25) {
echo 'Mr. & Rep.'; }
else if($row_DetailRS1['prefix']==26) {
echo 'Reps.'; }
else if($row_DetailRS1['prefix']==27) {
echo 'Mayor'; }
else if($row_DetailRS1['prefix']==28) {
echo 'Admin'; }; ?></td>
</tr>
<tr>
<td>Business Name</td>
<td><?php echo $row_DetailRS1['businessName']; ?></td>
</tr>
<tr>
<td>First Name</td>
<td><?php echo $row_DetailRS1['firstName']; ?></td>
</tr>
<tr>
<td>Spouse Name</td>
<td><?php echo $row_DetailRS1['spouseName']; ?></td>
</tr>
<tr>
<td>Last Name</td>
<td><?php echo $row_DetailRS1['lastName']; ?></td>
</tr>
<tr>
<td>Suffix</td>
<td><?php if($row_DetailRS1['suffix']==1) {
echo 'Sr.';}
else if($row_DetailRS1['suffix']==2) {
echo 'Jr.'; }
else if($row_DetailRS1['suffix']==3) {
echo 'I'; }
else if($row_DetailRS1['suffix']==4) {
echo 'II' ; }
else if($row_DetailRS1['suffix']==5) {
echo 'III'; }
else if($row_DetailRS1['suffix']==6) {
echo 'IV'; }
else if($row_DetailRS1['suffix']==7) {
echo 'V'; }
else if($row_DetailRS1['suffix']==8) {
echo 'Esq.'; }; ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php echo $row_DetailRS1['address']; ?></td>
</tr>
<tr>
<td>Address Continued</td>
<td><?php echo $row_DetailRS1['addressCont']; ?></td>
</tr>
<tr>
<td>City</td>
<td><?php echo $row_DetailRS1['city']; ?></td>
</tr>
<tr>
<td>State</td>
<td><?php while($row = mysql_fetch_assoc($customer, $result)) {
if($row_DetailRS1['state'] == $row) {
echo '$row';}} ?></td>
</tr>
<tr>
<td>Zip Code</td>
<td><?php echo $row_DetailRS1['zipCode']; ?></td>
</tr>
<tr>
<td>Phone Number</td>
<td><?php echo $row_DetailRS1['phoneNum']; ?></td>
</tr>
<tr>
<td>Alt. Phone Number</td>
<td><?php echo $row_DetailRS1['altPhoneNum']; ?></td>
</tr>
<tr>
<td>E-mail</td>
<td><?php echo $row_DetailRS1['email']; ?></td>
</tr>
<tr>
<td>Board Member</td>
<td><?php echo $row_DetailRS1['boardMember']; ?></td>
</tr>
<tr>
<td>Trustee</td>
<td><?php echo $row_DetailRS1['trustee']; ?></td>
</tr>
<tr>
<td>Musician</td>
<td><?php echo $row_DetailRS1['musician']; ?></td>
</tr>
<tr>
<td>Support Staff</td>
<td><?php echo $row_DetailRS1['supportStaff']; ?></td>
</tr>
<tr>
<td>Active</td>
<td><?php echo $row_DetailRS1['active']; ?></td>
</tr>
<tr>
<td>Deceased</td>
<td><?php echo $row_DetailRS1['deceased']; ?></td>
</tr>
<tr>
<td>Comments</td>
<td><?php echo $row_DetailRS1['comments']; ?></td>
</tr>
</table>
</body>
</html><?php
mysql_free_result($DetailRS1); ?>
答案 0 :(得分:1)
我需要重新编写此代码。它在PHP中做了很多应该在SQL中完成的工作。你有能力创建新的MySQL表吗?如果是这样,我会创建2个新表(前缀,后缀),然后我将状态查询拉入主查询,这样你就不会进行单独的查询(额外的数据库命中)。
举个例子,这就是我如何做前缀表:
create table prefixes (
prefix_id integer primary key,
prefix_string varchar(20)
);
insert into prefixes (prefix_id, prefix_string) values (1, 'Mr.');
insert into prefixes (prefix_id, prefix_string) values (2, 'Mrs.');
... etc ...
然后主要查询将是:
select c.*, p.prefix_string
from customer c, prefixes p
where c.prefix = p.prefix_id;
然后您可以直接使用prefix_string
而不是if / else乱。对后缀和状态做同样的事情。
如果你没有能力创建表,那么我就创建了PHP函数,它将整数ID转换为适当的字符串。
答案 1 :(得分:0)
如果出现问题,您必须在此代码中将其隔离以解决问题,此刻它有点乱。看起来 recordID GETVAR 用于查找数据库中的用户帐户。您应该验证请求中是否传递了ID,并且是否已从数据库加载用户帐户。然后逐步找出其他可能无法正常工作的内容,您可以随时清理代码。我已经开始清理那些虚假的 else,如果阻止。
前缀阻止
$aPrefixMap = array(
1 => 'Mr.',
2 => 'Mrs.',
3 => 'Ms.',
4 => 'Mr. & Mrs.',
5 => 'Dr.',
6 => 'Dr. & Mrs.',
7 => 'Mr. & Dr.',
8 => 'Drs.',
9 => 'Hon.',
10 => 'Hon. & Mrs.',
11 => 'Mr. & Hon.',
12 => 'Hons.',
13 => 'Rev.',
14 => 'Rev. & Mrs.',
15 => 'Atty.',
16 => 'Atty. & Mrs.',
17 => 'Mr. & Atty.',
18 => 'Attys.',
19 => 'Sen.',
20 => 'Sen & Mrs.',
21 => 'Mrs. & Sen.',
22 => 'Sens.',
23 => 'Rep.',
24 => 'Rep. & Mrs.',
25 => 'Mr. & Rep.',
26 => 'Reps.',
27 => 'Mayor',
28 => 'Admin'
);
$iPrefix = $row_DetailRS1['prefix'];
if(array_key_exists($iPrefix, $aPrefixMap))
echo $aPrefixMap[$iPrefix];
后缀阻止
$aRowDetailMap = array(
1 => 'Sr.',
2 => 'Jr.',
3 => 'I',
4 => 'II',
5 => 'III',
6 => 'IV',
7 => 'V',
8 => 'Esq.'
);
$iDetailSuffix = $row_DetailRS1['suffix'];
if(array_key_exists($iDetailSuffix, $aRowDetailMap))
echo $aRowDetailMap[$iDetailSuffix];