Echo PHP多维数组下拉列表

时间:2014-11-18 05:20:47

标签: php select multidimensional-array echo

我有一个表单可以从下拉列表中选择月份和日期,但我不知道如何选择并打印出结果

<?php
$months = array(
"Jan" => array(
"01" => "Aquarius",
"02 => "Aquarius",
......
),
"Feb" => array(
"01" => "Gemini",
"02" => "Gemini",
......
),
);

我只是希望能够显示用户选择1月01日时所选择的内容他们会看到他们的标志是水瓶座

2 个答案:

答案 0 :(得分:1)

直接的PHP解决方案可能是使用strtotime()函数的东西,如:

// The date and month of person
$choosedate     =   strtotime("august 4");

// If the person has a date earlier than key 1
// then this is the key for after last date & before first
$start          =   'sign12';

// List of signs with start dates
$date['sign1']  =   strtotime("january 15");
$date['sign2']  =   strtotime("february 15");
$date['sign3']  =   strtotime("march 15");
$date['sign4']  =   strtotime("april 15");
$date['sign5']  =   strtotime("may 15");
$date['sign6']  =   strtotime("june 15");
$date['sign7']  =   strtotime("july 15");
$date['sign8']  =   strtotime("august 15");
$date['sign9']  =   strtotime("sept 15");
$date['sign10'] =   strtotime("october 15");
$date['sign11'] =   strtotime("november 15");
$date[$start]   =   strtotime("december 15");

// Loop through the dates separating out keys and times
foreach($date as $sign => $datestamp) {
        // If user month day is greater than current
        // assign current
        if($choosedate >= $datestamp)
            $currentSign    =   $sign;
        // If user month day is less than current, stop
        else
            break;
    }

// Echo current key if set, or else it will be the start sign.
// This would echo sign7
echo (!isset($currentSign))? $start:$currentSign;

答案 1 :(得分:0)

一个基本想法,如何做到,

PHP PART

$months = array(
                    "Jan" => array(
                                 "01" => "Aquarius",
                                 "02" => "Aquarius",
                                ),
                    "Feb" => array(
                                   "01" => "Gemini",
                                   "02" => "Gemini",
                                   )
                    );

JS PART

<script type="text/javascript">

$( document ).ready(function()
{

    var months_json = <?php echo json_encode($months);?>;

    console.log(months_json);

    for( m in months_json){
        //console.log(m);
        $('#m_sb').append('<option>'+m+'</option>');

    }


    $("#m_sb, #d_sb").on('change',function(){
        var month = $('#m_sb option:selected').text();
        var date  = $('#d_sb option:selected').text();

        //console.log(date+month);

        $.each(months_json, function(k,v){

            if(k == $('#m_sb option:selected').text()){
                $.each(v, function(i,j){
                     if(i == $('#d_sb option:selected').text())
                        alert(j);
                });
            }
        });

    });


});



</script>

<select name='months' id='m_sb'>

</select>

<select name='dates' id='d_sb'>
    <option>01</option>
    <option>02</option>
</select>