Php 将数据从MySql拉入数组,然后比较数组';将数据转换为现有变量

Php 将数据从MySql拉入数组,然后比较数组';将数据转换为现有变量,php,mysql,arrays,Php,Mysql,Arrays,我有一个我正在处理的应用程序,它是在Dreamweaver CS5中生成的。代码非常神奇,创建网站的人无法提供帮助,因此我正在努力完成代码,并尽可能地“简化”其中的一些内容。我更像是一个php初学者。因此,似乎这并不难。应用程序“应该”做什么的描述:customer表应该显示数据(它应该显示),并且应该显示表中的前缀、后缀和状态,但是它有主键的自动增量id,文本部分不会显示,只显示数字。因此,我的前任使用if-else语句将数字转换为数据(您将在代码中看到这有多么麻烦)。这是我的代码: <

我有一个我正在处理的应用程序,它是在Dreamweaver CS5中生成的。代码非常神奇,创建网站的人无法提供帮助,因此我正在努力完成代码,并尽可能地“简化”其中的一些内容。我更像是一个php初学者。因此,似乎这并不难。应用程序“应该”做什么的描述:customer表应该显示数据(它应该显示),并且应该显示表中的前缀、后缀和状态,但是它有主键的自动增量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. &amp; Mrs.'; }
                else if($row_DetailRS1['prefix']==5) {
                    echo 'Dr.'; }
                    else if($row_DetailRS1['prefix']==6) {
                        echo 'Dr. &amp; Mrs.'; }
                        else if($row_DetailRS1['prefix']==7) {
                            echo 'Mr. &amp; 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. &amp; Mrs.'; }
                                        else if($row_DetailRS1['prefix']==11) {
                                            echo 'Mr. &amp; 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. &amp; Mrs.'; }
                                                        else if($row_DetailRS1['prefix']==15) {
                                                            echo 'Atty.'; }
                                                            else if($row_DetailRS1['prefix']==16) {
                                                                echo 'Atty. &amp; Mrs.'; }
                                                                else if($row_DetailRS1['prefix']==17) {
                                                                    echo 'Mr. &amp; 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 &amp; Mrs.'; }
                                                                                else if($row_DetailRS1['prefix']==21) {
                                                                                    echo 'Mrs. &amp; 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. &amp; Mrs.'; }
                                                                                                else if($row_DetailRS1['prefix']==25) {
                                                                                                    echo 'Mr. &amp; 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); ?>

无标题文件
身份证件
前缀
企业名称
名字
配偶名字
姓
后缀
地址
地址续
城市
陈述
邮政编码
电话号码
备用电话号码
电子邮件
董事会成员
受托人
音乐家
辅助人员
活跃的
已故的
评论

I此代码需要重新编写。它在PHP中做了很多工作,而这些工作应该在SQL中完成。您有能力创建新的MySQL表吗?如果是这样,我将创建2个新表(前缀、后缀),并将states查询拉入主查询中,这样您就不会执行单独的查询(额外的DB hit)

例如,下面是我如何创建前缀表:

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;
然后您就可以直接使用
前缀\u字符串
,而不是if/else。对后缀和状态执行同样的操作


如果您没有创建表的能力,那么我将创建PHP函数,将整数ID转换为适当的字符串。

如果有问题,您必须在此代码中隔离它才能解决,目前有点混乱。看起来,recordIDGETVAR用于查找数据库中的用户帐户。您应该验证请求中是否传递了id,以及是否从数据库加载了用户帐户。然后一步一步地查找可能不起作用的其他内容,您可以在执行过程中清理代码。我开始清理那些虚假的else-if块

前缀块

$aPrefixMap = array(
    1       => 'Mr.',
    2       => 'Mrs.',
    3       => 'Ms.',
    4       => 'Mr. &amp; Mrs.',
    5       => 'Dr.',
    6       => 'Dr. &amp; Mrs.',
    7       => 'Mr. &amp; Dr.',
    8       => 'Drs.',
    9       => 'Hon.',
    10      => 'Hon. &amp; Mrs.',
    11      => 'Mr. &amp; Hon.',
    12      => 'Hons.',
    13      => 'Rev.',
    14      => 'Rev. &amp; Mrs.',
    15      => 'Atty.',
    16      => 'Atty. &amp; Mrs.',
    17      => 'Mr. &amp; Atty.',
    18      => 'Attys.',
    19      => 'Sen.',
    20      => 'Sen &amp; Mrs.',
    21      => 'Mrs. &amp; Sen.',
    22      => 'Sens.',
    23      => 'Rep.',
    24      => 'Rep. &amp; Mrs.',
    25      => 'Mr. &amp; 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];
后缀块

$aPrefixMap = array(
    1       => 'Mr.',
    2       => 'Mrs.',
    3       => 'Ms.',
    4       => 'Mr. &amp; Mrs.',
    5       => 'Dr.',
    6       => 'Dr. &amp; Mrs.',
    7       => 'Mr. &amp; Dr.',
    8       => 'Drs.',
    9       => 'Hon.',
    10      => 'Hon. &amp; Mrs.',
    11      => 'Mr. &amp; Hon.',
    12      => 'Hons.',
    13      => 'Rev.',
    14      => 'Rev. &amp; Mrs.',
    15      => 'Atty.',
    16      => 'Atty. &amp; Mrs.',
    17      => 'Mr. &amp; Atty.',
    18      => 'Attys.',
    19      => 'Sen.',
    20      => 'Sen &amp; Mrs.',
    21      => 'Mrs. &amp; Sen.',
    22      => 'Sens.',
    23      => 'Rep.',
    24      => 'Rep. &amp; Mrs.',
    25      => 'Mr. &amp; 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];

您可以先将这些horid elseif块更改为查看数组的一行程序。已经有前缀和后缀的表格,我的表格如下:adtype、广告商、chair、creditcardtype、customer、捐赠者、DonortType、events、paymenttype、prefix、sponsortype、state、subpackage、subpackage、subscriber、suffix、user。前缀和后缀表的结构如下:前缀-id(int(11)),后缀类型(text);后缀id(int(11)),后缀类型(text);state id(tinyint(4))、state(varchar(32))、state_abbr(varchar(8))感谢您的提示,该id将客户记录信息传递到此详细信息页面,该部分起作用,但当我列出前缀、后缀和状态时,它就是其中的一部分。代码将项目的主键从表中取出,表被规范化,冗余最小,留给我的注释对于应用程序应该做什么非常具体,但是代码非常混乱,很难理解。非常感谢您的帮助。这非常有效,有没有办法从表中提取数组数据,而不是列出所有值?可能是这样的:@MikeM是的,这样的查询应该可以将数据放入数组,但我认为您需要更改
$array[]=$row[0]
$array[$row[0]]=$row[1]所以你有一个像$array[id]=>state_abbr这样的数组。只要小心,数据库中的id就会与网页中的id关联起来。我尝试了这个方法,我得到了很多错误:
$query=“SELECT id,state_abbr from state”$r=mysql\u查询($query,$customer)$aStateMap=array();虽然($row=mysql_fetch_assoc($r)){$aStateMap[$row[0]]=$row[1];}
你知道为什么这对我不起作用吗?很难说,我觉得它基本上是对的。在调用此代码时,请确保$customer有效,并检查$row的值,还可以在运行while循环之前测试$r。