自定义PHP搜索功能

时间:2013-04-04 19:46:36

标签: php codeigniter

我希望我能解释一下让所有人理解。我创建了一个搜索功能表单,包含两个多选项和复选框。一个多选择具有性别,第二个具有从1900年到当年的年份,并且复选框具有一些代码和一些剂量的预定。搜索功能有效但我被要求稍微更改一下。我想要做的是,如果用户选择两个处方,例如我想要获得具有两个处方的行,而只选择已经选择的两个处方。目前我已经循环遍历复选框数组的循环,并且如果它具有与该搜索项匹配的任何处方,则获取id。我所拥有的代码不是最好看的代码,但这里是我所拥有的,当你看到代码时,你可以理解它。我会稍微修改一下。以下代码来自我的模型

    foreach ($this->input->post() as $key => $value) {
    switch ($key) {
        case 'gender':
            $who = $value;
            break;

        case 'custom_search':
            break;
         case 'study':
            $study_tables[] = $value;
            break;
         case 'birth_year':
            $birth_years[] = $value;
            break;
         case 'icd9_codes':
            $icd9_tables[] = $value;
            break;
         default:
            $doses[] = $value;
            break;
    }
}
if ($who == '*') {
    $q = $this->db->select('account_num')
            ->from('patient_info')
            ->get();
    foreach ($q->result_array() as $account_num) {
        $accounts[] = $account_num['account_num'];
    }
    if ($who && !$birth_years && !$icd9_tables && !$study_tables && !$doses) {
        $acc_nums = $accounts;
    }
    else {
       if ($birth_years) {
            $i = 0;
            foreach ($birth_years as $birth_year) {
                foreach ($accounts as $account) {
                    $q = $this->db->select('account_num')
                            ->from('patient_info')
                            ->where('account_num', $account)
                            ->like('dob', $birth_year[$i])
                            ->get();
                    foreach ($q->result_object() as $key) {
                        foreach ($key as $k => $v) {
                            $acc_nums[] = $v;
                        }
                    }
                }
                $i++;
            }
            unset($accounts);
            $accounts = $acc_nums;
            unset($acc_nums);
        }
        if ($study_tables) {
            $i = 0;
            foreach ($study_tables as $table) {
                foreach ($accounts as $account) {
                    $q = $this->db->select('account_num')
                            ->from(strtolower($table[$i]))
                            ->where('account_num', $account)
                            ->get();
                    foreach ($q->result_object() as $key) {
                        foreach ($key as $k => $v) {
                            $acc_nums[] = $v;
                        }
                   }
                }
               $i++;
            }
            unset($accounts);
            $accounts = $acc_nums;
            unset($acc_nums);
        }
        if ($icd9_tables) {
            $i = 0;
            foreach ($icd9_tables as $table) {
                foreach ($accounts as $account) {
                    $q = $this->db->select('account_num')
                            ->from(strtolower($table[$i]))
                            ->where('account_num', $account)
                            ->get();
                    foreach ($q->result_object() as $key) {
                       foreach ($key as $k => $v) {
                            $acc_nums[] = $v;
                        }
                    }
                }
                $i++;
            }
            unset($accounts);
            $accounts = $acc_nums;
            unset($acc_nums);
        }
        if ($doses) {
            foreach ($doses as $dose) {
                foreach ($accounts as $account) {
                    $q = $this->db->select('account_num')
                            ->from('prescription_history')
                            ->where('prescription', substr($dose, 0, strpos($dose, '-')))
                            ->where('dosage', end(explode('-', $dose)))
                            ->where('account_num', $account)
                            ->get();
                    foreach ($q->result_object() as $key) {
                        foreach ($key as $k => $v) {
                            $acc_nums[] = $v;
                        }
                    }
                }
           }
            unset($accounts);
            $accounts = $acc_nums;
            unset($acc_nums);
        }
        if ($accounts) {
            $acc_nums = $accounts;
        }
    }
}

以下代码是我的控制器

$tables = '';
    $data['patients'] = $this->patients_model->custom_search();
    $prescriptions = '';
    $codes = '';
    $birth_years = '';
    $studies = '';

    $data['title'] = 'Patient Center: Search Results';

    //page does not exist
    if (empty($data['patients'])){
        $data['error'] = 'No results found';
        $data['heading'] = 'No results found';
    }
    else{
        foreach ($this->input->post() as $key => $value) {                
            switch ($key) {

                case 'gender':
                    $who = $value;
                    break;

                case 'custom_search':
                    break;

                case 'birth_year':
                    if (is_array($value)) {
                        foreach ($value as $val) {
                            $birth_years .= $val.', ';
                        }
                    }
                    else {
                        $studies .= $value.', ';
                    }
                    break;

                case 'study':
                    if (is_array($value)) {
                        foreach ($value as $val) {
                            $studies .= $val.', ';
                        }
                    }
                    else {
                        $studies .= $value.', ';
                    }
                    break;

                case 'icd9_codes':
                    if (is_array($value)) {
                        foreach ($value as $val) {
                            $codes .= $val.', ';
                        }
                    }
                    else {
                        $codes .= $value.', ';
                    }
                    break;

                default:
                    if (is_array($value)) {
                        foreach ($value as $val) {
                            $prescriptions .= $val.', ';
                        }
                    }
                    else {
                        $prescriptions .= $value.', ';
                    }
                    break;
            }
        }

        if ($who == '*') $title = ' for Everyone ';
        else $title = 'for all '.ucfirst($who).'s';
        if ($birth_years != '') { $title .= ' born on '.substr($birth_years,0,-2);}
        if ($studies != '') {$title .= ' in '.substr($studies,0,-2); }
        if ($prescriptions != '') {$title .= ' on '.substr($prescriptions,0,-2); }
        if ($codes != '') {$title .= ' with ICD9 Code '.substr($codes,0,-2); }          

        $data['heading'] = 'Search Results '.$title;
    }

    $this->load->view('templates/header',$data);
    $this->load->view('patients/search',$data);
    $this->load->view('templates/footer');

这里的代码是视图文件

<?php echo form_open('patients/custom_search'); ?>
                <table>                    
                    <tr>
                        <td>Gender</td>
                        <td>
                            <div id="gender">
                            <select name="gender">
                                <option value="*">All</option>
                                <option value="Male">Males</option>
                                <option value="Female">Females</option>
                            </select>
                            </div>
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td colspan="2">
                            <span style="font-size: 12px;">Hold down the Ctrl (Windows) / Command (Mac) button to select or unselect multiple options.</span>
                        </td>
                    </tr>
                    <tr>
                        <td>Birth Year</td>
                        <td>
                            <div id="birth_year" class="table_holder">
                            <select name="birth_year[]" multiple="multiple"  size="2">
                                <?php
                                for ($year = 1900; $year <= date("Y"); $year++) {
                                    echo '<option value="'.$year.'" />'.$year.'</option>'."\n"; 
                                }
                                ?>
                            </select>
                            </div>
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td>Study</td>
                        <td id="study_select">
                            <div class="table_holder">
                                <?php if (!empty($studies_sel)) { ?>
                                <?php $i=1;?>
                                <?php foreach ($studies_sel as $key):?>
                                <div class="grid_2">
                                <input id="study_check_<?php echo $i; ?>" type="checkbox" name="study[]" value="<?php echo preg_replace('/[\s\.\-\/]/', '', $key['study']); ?>" /> 
                                <?php echo ucfirst($key['study']); ?>
                                </div>
                                <?php $i++; ?>
                                <?php endforeach ?>
                                <?php } else { echo 'No studies on file'; } ?>
                            </div>                                
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td>Prescription</td>
                        <td id="script_select">
                            <div class="table_holder">
                            <?php 
                            if (!empty($prescriptions_sel)) {
                                foreach ($prescriptions_sel as $key => $value) {
                                    echo '<div class="grid_2">';
                                    foreach($value as $k => $val){
                                        echo '<input id="prescription_check_'.$i.'" type="checkbox" name="'.$val.'" value="'.$val.'" />'.$val;
                                    }
                                    echo '</div>';
                                    $i++;
                                }
                             }
                             else { echo 'No prescriptions on file'; } 
                             ?>
                            </div>                              
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td>ICD9 Code</td>
                        <td id="icd9_select">
                            <div class="table_holder">
                                <?php if (!empty($icd9_sel)) { ?>
                                <?php $i=1;?>
                                <?php foreach ($icd9_sel as $key):?>
                                <div class="grid_2">
                                <input id="icd9_check<?php echo $i; ?>" type="checkbox" name="icd9_codes[]" value="<?php echo preg_replace('/[\s\.\-\/]/', '', $key['icd9_code']); ?>" /> 
                                <?php echo ucfirst($key['icd9_code']).'('.$key['description'].')'; ?>
                                </div>
                                <?php $i++; ?>
                                <?php endforeach ?>
                                <?php } else { echo 'No ICD9 Codes on file'; } ?>
                            </div>
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr><td colspan="2"><input type="submit" name="custom_search" value="Search" /></td></tr>
                </table>                                    
            </form>

我想要做的是当检查多个复选框时,而不是获取与复选框中的任何一个匹配的所有行,我想获得与所有选中的复选框匹配的行。很抱歉,如果它难以理解和不幸,我无法向您显示页面,因为您需要登录。

0 个答案:

没有答案