0

我正在尝试为数据库构建下拉过滤器搜索,它似乎与我现有的升序/降序逻辑语句相冲突。

当我尝试将下拉菜单过滤器的 if 语句移动到升序/降序语句上方时,过滤器无法正常工作,即当我尝试过滤“已分析”时,它也给了我另一个状态。

另一方面,当我尝试将下拉过滤器的 if 语句移动到排序语句下方时,过滤器搜索工作正常,但在尝试对表排序时,我得到 $sql 未定义错误。

    print("<form action='patients.php' method='get'>");
    // cs = case status
    print("<select name='cs'> 
                <option selected='selected' value=''>--Select Case Status--</option>
                <option value='1'>Pass</option>
                <option value='2'>Failed</option>
                <option value='3'>Pass With Error</option>
                <option value='4'>Indeterminate</option>
          </select>");
    // sf = status flagged
    print("<select name='sf'>
                <option selected='selected' value=''>--Select Flagged Status--</option>
                <option value='Analyzed'>Analyzed Case</option>
                <option value='New Case'>New Case</option>
                <option value='Updated Case'>Updated Case (New Images)</option>
                <option value='None'>No Status Flag</option>
          </select>");

    print("<input type='submit' name='search' value='Search'></form>");
    //print("<a href = 'patients.php?case=failed'>View Failed Cases</a><br><br>");



    }
    printf('<table border=1><tr><th><a href ="patients.php?pID=%s">Patient ID</a>',empty($_GET['pID']) || $_GET['pID'] == 'asc' ? 'desc' : 'asc');
    printf('</th><th><a href ="patients.php?caseStatus=%s">Case Status</a>',
       empty($_GET['caseStatus']) || $_GET['caseStatus'] == 'asc' ? 'desc' : 'asc');
    printf('</th><th>Sub Status</th><th>Number of Cases</th><th>Status Reason</th><th><a href ="patients.php?dateReviewed=%s">Date Reviewed</a>',
       empty($_GET['dateReviewed']) || $_GET['dateReviewed'] == 'asc' ? 'desc' : 'asc');
    printf('</th><th><a href ="patients.php?statusFlag=%s">Status Flag</a>',
       empty($_GET['statusFlag']) || $_GET['statusFlag'] == 'asc' ? 'desc' : 'asc');
    printf('</th></tr>');




// search filter, takes the case status (cs) and status flag (sf) and filter according to selection
        if (isset($_GET['cs']) && isset($_GET['sf']) ) {
            $cs= $_GET['cs'];
            $sf= $_GET['sf'];

            if (($sf==NULL) && ($cs!==NULL)) {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = '".$cs."' 
                    Group By patientid, cases.status";      

            } else if (($sf!==NULL) && ($cs==NULL)) {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status_flag = '".$sf."'
                    Group By patientid, cases.status";      

            } else if (($cs==NULL)&&($sf==NULL)){

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status";  
            } else {
                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = '".$cs."' and cases.status_flag = '".$sf."'
                    Group By patientid, cases.status";      


            }

            $orderBy ="Order By patientid";
        }

else  {
        $orderBy = "Order By patientid";
        $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;

                    }

//takes the patient id, status description, count of cases then groups them by patient id ordering them by their status

if (isset($_GET['pID'])){

                    switch($_GET['pID']){

                      case "asc":
                        $orderBy = " ORDER BY patientid ASC";
                        break;

                      case "desc":
                        $orderBy = " ORDER BY patientid DESC";
                        break;

                      default:
                        $orderBy = " ORDER BY patientid ASC";
                        break;

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;



                        }
    } else if (isset($_GET['caseStatus'])){                 

                    switch($_GET['caseStatus']){

                      case "asc":
                        $orderBy = " ORDER BY sub_status_lookup.CASE_STATUS ASC";
                        break;

                      case "desc":
                        $orderBy = " ORDER BY sub_status_lookup.CASE_STATUS DESC";
                        break;

                      default:
                        $orderBy = " ORDER BY sub_status_lookup.CASE_STATUS ASC";
                        break;

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;



                    }

    } else if (isset($_GET['dateReviewed'])){                           

                    switch($_GET['dateReviewed']){

                      case "asc":
                        $orderBy = " ORDER BY date_mod ASC";
                        break;

                      case "desc":
                        $orderBy = " ORDER BY date_mod DESC";
                        break;

                      default:
                        $orderBy = " ORDER BY date_mod ASC";
                        break;

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;




                        }


    // Updated to work, statusFlag now stored in DB as status_flag 6/4/2013 - Allen
    } else if (isset($_GET['statusFlag'])){ 

                    switch($_GET['statusFlag']){

                      case "asc":
                        $orderBy = " ORDER BY status_flag ASC";
                        break;

                      case "desc":
                        $orderBy = " ORDER BY status_flag DESC";
                        break;

                      default:
                        $orderBy = " ORDER BY status_flag ASC";
                        break;

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;


                    }

    } else if (isset($_GET['lastUser'])){   

                    switch($_GET['lastUser']){

                      case "asc":
                        $orderBy = " ORDER BY user_mod ASC";
                        break;

                      case "desc":
                        $orderBy = " ORDER BY user_mod DESC";
                        break;

                      default:
                        $orderBy = " ORDER BY user_mod ASC";
                        break;

                    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;


                    }                   

    } else  { $orderBy = "Order By patientid";
    $sql = "SELECT patientid, sub_status_lookup.CASE_STATUS, sub_status_lookup.SUB_STATUS, count(caseid) as cases_count, cases.comments, date_mod, caseid, user_mod, status_flag  FROM cases, sub_status_lookup
                    Where cases.status = sub_status_lookup.SUB_ID
                    Group By patientid, cases.status ".$orderBy;


    }   




                    //commented 5.10.2013 - allen
                    /*while (isset($_GET['sort'])) {
                        $sortOrder = ASC;
                        if ($_GET['sort'] == 'patientID') { $sql .= "Order by patientid ".$sortOrder;}
                        else if ($_GET['sort'] == 'caseStatus') { $sql .= "Order By sub_status_lookup.CASE_STATUS ".$sortOrder;}
                        else if ($_GET['sort'] == 'dateReviewed') { $sql .= "Order by date_mod ".$sortOrder; }
                        else if ($_GET['sort'] == 'statusFlag') { $sql .= "Order by date_mod ".$sortOrder; }
                    }
                    if (!isset($_GET['sort'])) { $sql .= "Order By patientid"; }*/



    $result = mysql_query($sql, $connection);
    $count = 1;
    $prvPatId = "";
    if($result !== FALSE) {
        while ($record = mysql_fetch_row($result)) {

            $patient = $record[0];
            $statusD = $record[1];
            $substatusD = $record[2];
            $caseCount = $record[3];
            $statusReason = $record[4];
            $dateReviewed = $record[5];
            $caseID = $record[6];
            $lastUser =$record[7];
            statusFlagged($patient,$caseID,$dateReviewed);
            $statusFlagged = $record[8];
            //StatusFlag should be working as of 5/30/2013. Please double check



            $patLink = "<a href='case.php?dirName=".$rootDirectory.$patient."&patient=".$patient."' >".$patient."</a>";

            if($prvPatId == $patient) {
                $patLink = "&nbsp;";
            } else {
                $count ++;
                $prvPatId = $patient;
            }
            print("<tr><td>".$patLink."</td><td>".$statusD."</td><td>".$substatusD."</td><td>".$caseCount."</td><td>".$statusReason."</td><td>".$dateReviewed."</td><td>".$statusFlagged."</td></tr>");
        }
    }


    mysql_free_result($result);
//at the end prints the total number of patients (patient ids- unique)
    print("<tr><th colspan=3>Total Patients: ".$count."</th></tr>");
    print("</table>"
4

0 回答 0