我有这个JSON数据:
"slots": [
{
"start": "2017-02-09 08:00:00",
"end": "2017-02-09 08:20:00"
},
{
"start": "2017-02-09 08:20:00",
"end": "2017-02-09 08:40:00"
},
{
"start": "2017-02-09 08:40:00",
"end": "2017-02-09 09:00:00"
},
{
"start": "2017-02-10 08:40:00",
"end": "2017-02-10 09:00:00"
},
{
"start": "2017-02-10 09:00:00",
"end": "2017-02-10 09:20:00"
},
{
"start": "2017-02-10 09:20:00",
"end": "2017-02-10 09:40:00"
}
我想将dateTime“start”拆分为$ date和$ time。它适用于此:
echo '<select name="reservationDate" id="test">';
foreach($slots_start as $option){
$dateTime = $option->start;
list($date, $time) = explode(" ", $dateTime);
echo '<option value=' . $date. '>' . $date . '</option>';
}
echo '</select>';
使用上面的代码,我会看到下拉列表,其中所有值都被$ date拆分。
现在我想按$ date分组,然后在另一个选择下拉菜单中显示$ time。
见Scheme: image scheme
答案 0 :(得分:0)
您可以先对数据进行分组,然后填充两个下拉列表。
<?php
// group data
$grouped = [];
foreach($slots_start as $option){
$dateTime = $option->start;
list($date, $time) = explode(" ", $dateTime);
if (isset($grouped[$date])) {
$grouped[$date][] = $time;
} else {
$grouped[$date] = [$time];
}
}
然后输出两个下拉列表。
您可以使用
获取日期数组$dates = array_keys($grouped);
每次都与该日期有关:
$times = $grouped[$date];
这样你将获得更多可持续的代码。 希望它有所帮助。
答案 1 :(得分:0)
php with:
<select name="company" id="select_company">
<option>Select...</option>
<option value="1">Italy</option>
</select>
<select name="reservationDate" id="firstSelect">
<option>Seleziona date</option>
</select>
//ajax
$('#select_company').on('change', function(e) {
e.preventDefault(); // Prevent Default Submission
$.ajax({
url: 'my_page.php',
type: 'POST',
data: $(this).serialize()
})
.done(function(data){
$('#firstSelect').hide(function(){
$('#firstSelect').show().html(data);
});
})
.fail(function(){
alert('Errore nel caricamento');
});
});
my_page.php with:
<?php
if(isset($_POST))
{
...//my code for get json array
$times = array();
foreach($slots_start as $option){
list($date, $time) = explode(" ", $option->start);
if (!array_key_exists($date, $times)){
$times[$date] = array();
}
if (!in_array($time, $times[$date])){
$times[$date][] = $time;
}
}
echo '<select name="reservationDate" id="firstSelect" onchange="fillSecond()">';
foreach (array_keys($times) as $date){
echo '
<option value="' . $date. '">' . $date . '</option>';
}
echo '</select>';
echo '<select name="reservationTime" id="secondSelect"></select>';
?>
<script language="javascript">
function fillSecond(){
firstSelectElement = document.getElementById('firstSelect');
secondSelectElement = document.getElementById('secondSelect');
times = date_options[firstSelectElement.selectedIndex];
while (secondSelectElement.options.length > 0) {
secondSelectElement.remove(0);
}
for(var i = 0; i < times.length; i++) {
var new_option = document.createElement('option');
new_option.innerHTML = times[i];
new_option.value = times[i];
secondSelectElement.appendChild(new_option);
}
}
date_options = [];
<?php
foreach($times as $date => $times){
echo "date_options.push([]);";
foreach ($times as $time){
echo "date_options[date_options.length - 1].push('$time');";
}
}
?>
fillSecond();
</script>
<?php
}?>
答案 2 :(得分:-1)
你需要用php创建javascript数组。
https://jsfiddle.net/bruvygjf/
<?php
$json_str =<<<EOF
[
{
"start": "2017-02-09 08:00:00",
"end": "2017-02-09 08:20:00"
},
{
"start": "2017-02-09 08:20:00",
"end": "2017-02-09 08:40:00"
},
{
"start": "2017-02-09 08:40:00",
"end": "2017-02-09 09:00:00"
},
{
"start": "2017-02-10 08:40:00",
"end": "2017-02-10 09:00:00"
},
{
"start": "2017-02-10 09:00:00",
"end": "2017-02-10 09:20:00"
},
{
"start": "2017-02-10 09:20:00",
"end": "2017-02-10 09:40:00"
}
]
EOF;
$slots = json_decode($json_str);
$times = array();
foreach($slots as $option){
list($date, $time) = explode(" ", $option->start);
if (!array_key_exists($date, $times)){
$times[$date] = array();
}
if (!in_array($time, $times[$date])){
$times[$date][] = $time;
}
}
echo '<select name="reservationDate" id="firstSelect" onchange="fillSecond()">';
foreach (array_keys($times) as $date){
echo '
<option value="' . $date. '">' . $date . '</option>';
}
echo '</select>';
echo '<select name="reservationTime" id="secondSelect"></select>';
?>
<script language="javascript">
function fillSecond(){
firstSelectElement = document.getElementById('firstSelect');
secondSelectElement = document.getElementById('secondSelect');
times = date_options[firstSelectElement.selectedIndex];
while (secondSelectElement.options.length > 0) {
secondSelectElement.remove(0);
}
for(var i = 0; i < times.length; i++) {
var new_option = document.createElement('option');
new_option.innerHTML = times[i];
new_option.value = times[i];
secondSelectElement.appendChild(new_option);
}
}
date_options = [];
<?php
foreach($times as $date => $times){
echo "date_options.push([]);";
foreach ($times as $time){
echo "date_options[date_options.length - 1].push('$time');";
}
}
?>
fillSecond();
</script>