简体   繁体   中英

Pulling data from MySql into an array, then comparing the array's data to an existing variable

I have an application I am working on, this was produced in Dreamweaver CS5. The code is quite rediculous, the person that made the site is not able to help so I am struggling through the code and trying to "simplify" some of it as I can. I am more of a beginner-intermediate with php. So it seems as if it could be not so difficult. Description of what the application "should" do: The customer table should display the data(which it does) and should show Prefix, Suffix, and State from the tables, however it has auto increment id's for the primary key and the text portion will not display, only the numbers. So my predecessor used if else statements to convert the number to data (you will see in the code how cumbersome this is). So here is my code:

<?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 this code needs to be reworked. It's doing a lot of work in PHP that should be done in SQL. Do you have the ability to create new MySQL tables? If so, I would create 2 new tables (prefixes, suffixes) and I would pull the states query into the main query so you're not doing a separate query (extra DB hit).

As an example, here is how I'd do the prefixes table:

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 ...

Then the main query would be something like:

select c.*, p.prefix_string
  from customer c, prefixes p
  where c.prefix = p.prefix_id;

Then you'd be able to use prefix_string directly instead of the if/else mess. Do the same with suffixes and states.

If you don't have the ability to create tables, then I'd create PHP functions which convert the integer IDs to appropriate strings.

If there's a problem you'll have to isolate it in this code to resolve it, it's a bit messy at the moment. It looks like the recordID GETVAR is being used to lookup a user account in the database. You should verify an id is getting passed in the request and that the user account is getting loaded from the database. Then step through an find out what else might not be working, you can clean up the code as you go. I've started out by cleaning up those spurious else if blocks.

Prefix Block

$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];

Suffix Block

$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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM