使用php数组生成选择选项

时间:2015-10-23 14:25:52

标签: javascript php jquery html mysql

我有一个“状态”类别选择,它根据所选的状态显示4个“载波”选择之一。我希望php用他们各自的选项填充“运营商”选项。我已经搜索了堆栈,我确信有几个答案,但由于我的新手php状态,我很难理解逻辑。话虽这么说,我现在的PHP是我指示使用/回收,并没有反映我的PHP技能。我真的需要帮助这个人!

这是一个小提琴(减去php):http://fiddle.jshell.net/prxmeo9z/

HTML(使用PHP):

    <div class="dropdown-box">
                        <select name="state_select" id="state_select">
                            <option selected disabled>Choose a state</option>
                            <option value="1">Connecticut</option>
                            <option value="2">New Hampshire</option>
                            <option value="3">New Jersey</option>
                            <option value="4">New York</option>
                        </select>
</div>
        <br />

<div id="select-div1" class="select-div">
        <select name="conn_select" id="carrier_select1"  class="carr-hide">
            <option selected disabled>Select a carrier - Conn</option>
            <option value="1">
             <? 
            $options = '';
foreach($company_arry as $company) {
    $options .= "<option value='" . $company['Id'] . "'>" . $company['Name'] . "</option>";
}
echo $options;
?>
            </option>
        </select>
</div>

<div id="select-div" class="select-div">                      
        <select name="nh_select" id="carrier_select2" class="carr-hide">
            <option selected disabled>Select a carrier - NH</option>
            <option value="2">
            <? 
            $options = '';
foreach($company_arryNH as $company) {
    $options .= "<option value='" . $company['Id'] . "'>" . $company['Name'] . "</option>";
}
echo $options;
?>
            </option>
        </select>
</div>

<div id="select-div" class="select-div">                       
        <select name="nj_select" id="carrier_select3" class="carr-hide">
            <option selected disabled>Select a carrier - NJ</option>
            <option value="3">
             <? 
            $options = '';
foreach($company_arryNJ as $company) {
    $options .= "<option value='" . $company['Id'] . "'>" . $company['Name'] . "</option>";
}
echo $options;
?>
            </option>
        </select>
</div>

<div id ="select-div" class="select-div">                        
        <select name="ny_select" id="carrier_select4" class="carr-hide">
            <option selected disabled>Select a carrier - NY</option>
            <option value="4">
            <? 
            $options = '';
foreach($company_arry as $company) {
    $options .= "<option value='" . $company['Id'] . "'>" . $company['Name'] . "</option>";
}
echo $options;
?>
            </option>
        </select>

</div>

JS / JQuery:

function optionCheck() {
    var i, len, optionVal, helpDiv,
        selectOptions = document.getElementById("state_select");

    // loop through the options in case there
    // are multiple selected values
    for (i = 0, len = selectOptions.options.length; i < len; i++) {

        // get the selected option value
        optionVal = selectOptions.options[i].value;

        // find the corresponding help div
        helpDiv = document.getElementById("carrier_select" + optionVal);
        //selectDiv = document.getElementsByClassName("select-div");

        // move on if we didn't find one
        if (!helpDiv) { continue; }

        // set CSS classes to show/hide help div
        if (selectOptions.options[i].selected) {
            helpDiv.className = "conn_select nh_select nj_select ny_select";
            $(helpDiv).addClass("dropdown-box");
        } else {
            helpDiv.className = "carr-hide";
            //$(".select-div").addClass("carr-hide");

        }
    }    
}

// alternative method of binding the onchange handler
document.getElementById("state_select").onchange = optionCheck;

我包含的PHP文件:

<?
$sql = mysql_query("SELECT gia_survey_allyears_company.Name, gia_survey_allyears_matrix.* FROM `gia_survey_allyears_matrix` 
                    left join gia_survey_allyears_company on gia_survey_allyears_matrix.CompanyID = gia_survey_allyears_company.ID
                    WHERE gia_survey_allyears_matrix.CompanyID=".mysql_real_escape_string(trim($_GET['id']))."");

$row = mysql_fetch_assoc($sql);

$qu_arry = array(   'Q1'=>'N/A','Q2'=>'N/A','Q3'=>'N/A','Q4'=>'N/A',
                    'Q5'=>'N/A','Q6'=>'N/A','Q7'=>'N/A','Q8'=>'N/A',
                    'Q9'=>'N/A','Q10'=>'N/A','Q11'=>'N/A','Q12'=>'N/A',
                    'Q13'=>'N/A','Q14'=>'N/A','Q15'=>'N/A','Q16'=>'N/A');

$quavg_arry = array(    'Q1'=>'N/A','Q2'=>'N/A','Q3'=>'N/A','Q4'=>'N/A',
                    'Q5'=>'N/A','Q6'=>'N/A','Q7'=>'N/A','Q8'=>'N/A',
                    'Q9'=>'N/A','Q10'=>'N/A','Q11'=>'N/A','Q12'=>'N/A',
                    'Q13'=>'N/A','Q14'=>'N/A','Q15'=>'N/A','Q16'=>'N/A');

$holdarry = array(  'Agents'=>'N/A','RankOutOf'=>'N/A','Rank'=>'N/A','Score'=>'N/A',
                    'Avg'=>'N/A','AvgAllScore'=>'N/A','AvgAllQuestion'=>'N/A',
                    'ComAvg'=>'N/A','Q1-16'=>$qu_arry,'Q1-16Avg'=>$quavg_arry);

$company['2003'] = $holdarry;
$company['2004'] = $holdarry;
$company['2005'] = $holdarry;
$company['2006'] = $holdarry;
$company['2007'] = $holdarry;
$company['2008'] = $holdarry;
$company['Name']= $row['Name'];
$company['State']= $row['State'];

do {
// Show Scores for question
$qu_arry = array(   'Q1'=>round($row['Q2-0'],1),'Q2'=>round($row['Q2-1'],1),'Q3'=>round($row['Q3-0'],1),'Q4'=>round($row['Q3-1'],1),
                    'Q5'=>round($row['Q3-2'],1),'Q6'=>round($row['Q4-0'],1),'Q7'=>round($row['Q4-1'],1),'Q8'=>round($row['Q5-0'],1),
                    'Q9'=>round($row['Q5-1'],1),'Q10'=>round($row['Q5-2'],1),'Q11'=>round($row['Q6-0'],1),'Q12'=>round($row['Q6-1'],1),
                    'Q13'=>round($row['Q7-0'],1),'Q14'=>round($row['Q7-1'],1),'Q15'=>round($row['Q8-0'],1),'Q16'=>round($row['Q8-1'],1));

    $company[$row['Year']]= array(  'Agents'=>$row['total'],
                                    'RankOutOf'=>'',
                                    'Rank'=>$row['Rank'],
                                    'Score'=>round($row['QAvg'],1),
                                    'Q1-16'=>$qu_arry,
                                    'Q1-16Avg'=>'');

    $sqlStateYear = mysql_query("SELECT * FROM `gia_survey_allyears_matrix` WHERE CompanyID=0 and `State`='".$row['State']."' and `Year`='".$row['Year']."'");
    $rowStateYear = mysql_fetch_assoc($sqlStateYear);

    do {

$company[$row['Year']]['Q1-16Avg'] = 
              array('Q1'=>round($rowStateYear['Q2-0'],1),'Q2'=>round($rowStateYear['Q2-1'],1),
                    'Q3'=>round($rowStateYear['Q3-0'],1),'Q4'=>round($rowStateYear['Q3-1'],1),
                    'Q5'=>round($rowStateYear['Q3-2'],1),'Q6'=>round($rowStateYear['Q4-0'],1),
                    'Q7'=>round($rowStateYear['Q4-1'],1),'Q8'=>round($rowStateYear['Q5-0'],1),
                    'Q9'=>round($rowStateYear['Q5-1'],1),'Q10'=>round($rowStateYear['Q5-2'],1),
                    'Q11'=>round($rowStateYear['Q6-0'],1),'Q12'=>round($rowStateYear['Q6-1'],1),
                    'Q13'=>round($rowStateYear['Q7-0'],1),'Q14'=>round($rowStateYear['Q7-1'],1),
                    'Q15'=>round($rowStateYear['Q8-0'],1),'Q16'=>round($rowStateYear['Q8-1'],1));
$company[$row['Year']]['AvgAllScore'] = round($rowStateYear['QAvg'],1);
$company[$row['Year']]['RankOutOf'] = $rowStateYear['total'];
    } while ($rowStateYear = mysql_fetch_assoc($sqlStateYear));

// END
} while ($row = mysql_fetch_assoc($sql));
function fnRake($rake) {
    if ($rake==11 || $rake==12 || $rake==13) {
        $i=$rake;
    }else {
        $i = substr($rake,strlen($rake)-1,1);
    }
    switch ($i) {
        case 1:
            $add =  "st";
        break;
        case 2:
            $add =  "nd";
        break;
        case 3:
            $add =  "rd";
        break;
        case 11:
            $add =  "th";
        break;
        case 12:
            $add =  "th";
        break;
        case 13:
            $add =  "th";
        break;
        default:
            $add =  "th";
        break;
    }
    if ($rake=="N/A") {
        return $rake;
    }else{
        return $rake."<sup>$add</sup>";
    }
}
?>

我的html开头有一些额外的PHP:

    $state = new state;
$state_arry = $state->state_options("NY");

//For Connecticut
$sqlCT = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='CT' and `show`='Y' order by Name");
//For New York
$sql = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='NY' and `show`='Y' order by Name");
//For New Jersey
$sqlNJ = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='NJ' and `show`='Y' order by Name");
//For New Hampshire
$sqlNH = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='NH' and `show`='Y' order by Name");

$row = mysql_fetch_assoc($sql);

//For New York
do {
    $company_arry[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sql));

//For New Jersey
do {
    $company_arryNJ[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sqlNJ));

//For New Jersey
do {
    $company_arryNH[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sqlNH));

//For Connecticut
do {
    $company_arryCT[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sqlCT));

$total = count($company_arry);
$col1 = round($total/2);
$col2 = $total-1

这个PHP生成了我在NY CARRIER SELECT中所需的列表(例如):

 <? 
            for ($i1=0; $i1<= $col1; $i1++) { 
                echo "<a href=\"company.php?id=".$company_arry[$i1]['Id']."\">".$company_arry[$i1]['Name']."</a><br>";
            } 
            ?>

1 个答案:

答案 0 :(得分:1)

也许这会解决它

$options = '';
foreach($company_arry as $company) {
    $options .= "<option value='" . $company['Id'] . "'>" . $company['Name'] . "</option>";
}
echo $options;