条件运算符未按预期执行

时间:2015-03-12 18:41:16

标签: php mysql

我目前正在尝试比较用户选择的两组返回值,但是当我去比较结果中的数据时,elseif语句默认,尽管信息明显相反。值是什么并不重要,程序总是引用elseif。任何帮助将非常感谢。谢谢!

// DB Constant Defines
define('DB_NAME','NurseData'); 
define('DB_USER','root'); 
define('DB_PASSWORD','root'); 
define('DB_HOST','localhost'); 

$state1 = $_REQUEST['state1'];
$state2 = $_REQUEST['state2'];
$city1 = $_REQUEST['city1'];
$city2 = $_REQUEST['city2'];
$jobTitle1 = $_REQUEST['job1'];
$jobTitle2 = $_REQUEST['job2'];

function showerror() {
    die("Error " . mysql_errno() . " : " . mysql_error());
}

$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error()); 
mysql_select_db(DB_NAME, $connection) or die(mysql_error());

$query1 = "SELECT DISTINCT
    TOT_EMP,  
    JOBS_1000,
    A_MEAN,
    A_PCT90
    FROM Nurse_Local 
    WHERE PRIM_STATE='" . $state1 . "' 
    AND AREA_NAME='" . $city1 . "' 
    AND OCC_TITLE='" . $jobTitle1 . "'";

$query2 = "SELECT DISTINCT
    TOT_EMP,  
    JOBS_1000,
    A_MEAN,
    A_PCT90
    FROM Nurse_Local 
    WHERE PRIM_STATE='" . $state2 . "' 
    AND AREA_NAME='" . $city2 . "' 
    AND OCC_TITLE='" . $jobTitle2 . "'";

if (!($getPosts1 = mysql_query ($query1, $connection))) {
    showerror();
}

if (!($getPosts2 = mysql_query ($query2, $connection))) {
    showerror();
}


while($rows1 = mysql_fetch_array($getPosts1)) {     

    while($rows2 = mysql_fetch_array($getPosts2)) {

    //Retrieve array values
        for ($i1 = 0; $i1 < count($rows1); $i1++) {
            for ($i2 = 0; $i2 < count($rows2); $i2++) {
                //Assign array values
                $tot_EMP1 = $rows1['TOT_EMP'];
                $tot_EMP2 = $rows2['TOT_EMP'];
                $jobs_PER1 = $rows1['JOBS_1000'];
                $jobs_PER2 = $rows2['JOBS_1000'];
                $a_MEAN1 = $rows1['A_MEAN'];
                $a_MEAN2 = $rows2['A_MEAN'];
                $A_PCT901 = $rows1['A_PCT90'];
                $A_PCT902 = $rows2['A_PCT90'];

                //Convert array values to numbers
                $tot_EMP1 = 0 + $tot_EMP1;
                $tot_EMP2 = 0 + $tot_EMP2;

                //Functions for calculating differences
                /*
                function compareEMP1($diffEMP1) {
                    $diffEMP1 = $rows1['TOT_EMP'] - $rows2['TOT_EMP'];
                    return $diffEMP1;
                }

                function compareEMP2() {
                    $diffEMP2 = $rows2['TOT_EMP'] - $rows1['TOT_EMP'];
                    return $diffEMP2;
                }
            */
            }
        }

        if($tot_EMP1 > $tot_EMP2 || $tot_EMP2 < $tot_EMP1) {
            echo $tot_EMP1;//"In " . $state1 . " there are " . compareEMP1() . " more jobs than in " . $state2;
        }
        elseif ($tot_EMP2 > $tot_EMP1 || $tot_EMP1 < $tot_EMP2) {
            echo $tot_EMP2;//"In " . $state2 . " there are " . compareEMP2() . " more jobs than in " . $state1;
        }
        else {
            echo "<p>There was a problem comparing the employment numbers.</p>";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

找到答案,那是因为&#34;数字&#34;被输出为字符串,PHP并没有将它们识别为数字。我通过首先在$ tot_EMP1和$ tot_EMP2上执行str_replace来删除逗号然后对两个变量执行intval()来解决它。这会将它们转换为整数,逻辑会根据需要执行。现在我需要弄清楚如何添加逗号并保留结果的int状态。

进行转换的实际代码,但请记住,我的数据库将信息存储为字符串,因此部分问题是我自己无法正确存储数据:

                //Assign individual array values
                $tot_EMP1 = $rows1['TOT_EMP'];
                $tot_EMP2 = $rows2['TOT_EMP'];
                $jobs_PER1 = $rows1['JOBS_1000'];
                $jobs_PER2 = $rows2['JOBS_1000'];
                $a_MEAN1 = $rows1['A_MEAN'];
                $a_MEAN2 = $rows2['A_MEAN'];
                $a_PCT901 = $rows1['A_PCT90'];
                $a_PCT902 = $rows2['A_PCT90'];


                //Convert individual array values to numbers
                $tot_EMP1 = str_replace(",", "", $tot_EMP1);
                $tot_EMP2 = str_replace(",", "", $tot_EMP2);
                $tot_EMP1 = intval($tot_EMP1);
                $tot_EMP2 = intval($tot_EMP2);
                $jobs_PER1 = floatval($jobs_PER1);
                $jobs_PER2 = floatval($jobs_PER2);
                $a_MEAN1 = str_replace(",", "", $a_MEAN1);
                $a_MEAN2 = str_replace(",", "", $a_MEAN2);
                $a_MEAN1 = intval($a_MEAN1);
                $a_MEAN2 = intval($a_MEAN2);
                $a_PCT901 = str_replace(",", "", $a_PCT901);
                $a_PCT902 = str_replace(",", "", $a_PCT902);
                $a_PCT901 = intval($a_PCT901);
                $a_PCT902 = intval($a_PCT902);