如果不是soldout,则将Json值导入PHP事件日历

如果不是soldout,则将Json值导入PHP事件日历,php,html,arrays,json,compare,Php,Html,Arrays,Json,Compare,我已经被困了好几天,试图找出做我需要做的事情的最佳方法。我有一个json url,我正在从中提取数据,它位于如下所示的数组中。日期范围可以是一个月到一整年的数据。我需要检查一下车票是否为soldout。如果soldout等于true,则应在日历中的该日期显示soldout。如果soldout等于false,则应在日历中与start中的日期匹配的日期中显示$url变量 我正在尝试使用的数组格式 array(3) { [0]=> object(stdClass)#1 (4) {

我已经被困了好几天,试图找出做我需要做的事情的最佳方法。我有一个json url,我正在从中提取数据,它位于如下所示的数组中。日期范围可以是一个月到一整年的数据。我需要检查一下车票是否为soldout。如果
soldout
等于true,则应在日历中的该日期显示
soldout
。如果
soldout
等于false,则应在日历中与
start
中的日期匹配的日期中显示
$url
变量

我正在尝试使用的数组格式

array(3) { 
    [0]=> object(stdClass)#1 (4) { 
        ["id"]=> string(3) "165"  
        ["start"]=> string(18) "05/02/2020 1:00 PM" 
        ["title"]=> string(19) "Event 1:00 PM" 
        ["alldetails"]=> array(1) { 
            [0]=> object(stdClass)#2 (1) {  
                ["soldout"]=> bool(false) 
            } 
        } 
    } 
    [1]=> object(stdClass)#3 (4) { 
        ["id"]=> string(3) "166" 
        ["start"]=> string(18) "07/19/2020 5:00 PM"  
        ["title"]=> string(19) "Event 5:00 PM" 
        ["alldetails"]=> array(1) { 
            [0]=> object(stdClass)#4 (1) { 
                ["soldout"]=> bool(false) 
            } 
        } 
    } 
    [2]=> object(stdClass)#5 (4) { 
        ["id"]=> string(3) "167" 
        ["start"]=> string(18) "11/14/2020 9:00 PM" 
        ["title"]=> string(19) "Event 1:00 PM" 
        ["alldetails"]=> array(1) { 
            [0]=> object(stdClass)#6 (1) { 
                ["soldout"]=> bool(false)
            }
        } 
    } 
}
从json数组中提取数据

<?php
$array = 'https://URL/feed.json?start=2020-05-02&end=2020-11-14';
$obj = json_decode(file_get_contents($array));
foreach ($obj as $key => $value){
    $id = $value->id;
    $start = $value->start;
    $title = $value->title;
    $url = '<a href="https://url/'.$id.'">'.$title.'</a>';
    $details = $value->alldetails;
    foreach($details as $nested){
        $nested->soldout; //Outputs 1 or is blank
    }
}
?>

我一直在尝试向其他人的日历中插入数据。我不确定我是应该用这个还是试着自己动手建造。PHP中的日期格式不是很好,学习还有很长的路要走,需要很多复习

<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
}
?>
<table>
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr>
<td width="50%" align="left"><a href="<?php echo "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" cellpadding="2" cellspacing="2" border="1">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<th><strong>S</strong></th>
<th><strong>M</strong></th>
<th><strong>T</strong></th>
<th><strong>W</strong></th>
<th><strong>T</strong></th>
<th><strong>F</strong></th>
<th><strong>S</strong></th>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];

for ($i=0; $i<($maxday+$startday); $i++) {
    if(($i % 7) == 0 ) echo "<tr>";
    if($i < $startday) echo "<td></td>";    
    else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>";
    if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>


S
M
T
W
T
F
S

试试看,如果这是你想要的,请告诉我

我已经创建了一个数组,用于保存指定日期范围内的所有事件。然后,当填充日历时,它会检查当前日期是否在数组中。如果是,它会显示它是否可用或已售罄

<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
}
?>
<table>
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr>
<td width="50%" align="left" onclick="changeDate(<?php echo $prev_month .','. $prev_year; ?>);" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right" onclick="changeDate(<?php echo $next_month .','. $next_year; ?>);" style="color:#FFFFFF">Next</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" cellpadding="2" cellspacing="2" border="1">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<th><strong>S</strong></th>
<th><strong>M</strong></th>
<th><strong>T</strong></th>
<th><strong>W</strong></th>
<th><strong>T</strong></th>
<th><strong>F</strong></th>
<th><strong>S</strong></th>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];

$eventsArray = array();

$array = 'https://URL/feed.json?start='.$cYear.'-'.$cMonth.'-'.$startday.'&end='.$cYear.'-'.$cMonth.'-'.$maxday;
$obj = json_decode(file_get_contents($array));
foreach ($obj as $key => $value) {
    $id = $value->id;
    $start = $value->start;
    $title = $value->title;
    $url = '<a href="https://url/'.$id.'">'.$title.'</a>';
    $details = $value->alldetails;
    $date = new DateTime($value->start);
    $day = $date->format('d');
    foreach($details as $nested){
        $soldOut = $nested->soldout;
        array_push($eventsArray, array($day, $soldOut));
    }
}

for ($i=0; $i<($maxday+$startday); $i++) {
    $currentDay = $i - $startday + 1;
    if($i % 7 == 0) {
        echo "<tr>";
    }
    if($i < $startday) {
        echo "<td></td>";
    }else{
        // Check if current day is in array $currentEvents
        foreach($eventsArray as $event){
            if($event[0] == $currentDay) {
                if($event[1] == 1) { // event is soldout
                    echo "<td align='center' valign='middle' height='20px'><p>".$currentDay."</p><p>Soldout</p></td>";
                }else{
                   echo "<td align='center' valign='middle' height='20px'><p>".$currentDay."</p><p>Available</p></td>"; 
                }
            }else{
                echo "<td align='center' valign='middle' height='20px'><p>".$currentDay."</p><p>No Event</p></td>"; 
            }
        }
    }
    if($i % 7 == 6) {
        echo "</tr>";
    }
}
?>
</table>
</td>
</tr>
</table>


@El_Vanja给了我很大的帮助,教会了我很多关于创建一个函数的知识,这个函数可以完全满足我的需求。似乎我做的事情是错误的,他引导我找到正确的答案,尽可能多地帮助我,同时还教我自己去做。如果@Il_Vanja获得荣誉,将成为一名优秀的PHP教师

这是正在工作的代码,仍然需要进行大量清理,包括用于显示事件的if语句,但as is按预期工作

<?php
$array = 'https://feed.json?start=2020-01-01&end=2100-12-31';
$obj = json_decode(file_get_contents($array,true));
$monthNames = Array("January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
}
?>
<table>
<tr align="center">
<td>
<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr>
<td width="50%" align="left"><div class="m_buttons"><a href="<?php echo "?month=". $prev_month . "&year=" . $prev_year; ?>" ><strong>Previous Month</strong></a></div></td>
<td width="50%" align="right"><div class="m_buttons"><a href="<?php echo "?month=". $next_month . "&year=" . $next_year; ?>" ><strong>Next Month</strong></a></div></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" cellpadding="2" cellspacing="2" border="1">
<tr align="center">
<td colspan="7"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<th><strong>Sun</strong></th>
<th><strong>Mon</strong></th>
<th><strong>Tues</strong></th>
<th><strong>Wed</strong></th>
<th><strong>Thurs</strong></th>
<th><strong>Fri</strong></th>
<th><strong>Sat</strong></th>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
function getDateCellContent(array $obj, $d) {
    $f_newdate = DateTime::createFromFormat('Y-m-j', $d);
    $f_newdate = $f_newdate->format('m/d/Y');
    $cellLink = '';
    $cellSoldout = '';
    foreach ($obj as $key => $value) {
        $start = $value->start;
        $a_newdate = DateTime::createFromFormat('m/d/Y g:i A', $start);
        $a_date = $a_newdate->format('m/d/Y');
        $a_time = $a_newdate->format('g:i A');
        $id = $value->id;
        $title = $value->title;
        if($a_date === $f_newdate) {
            foreach ($value->alldetails as $details) {
                $name = $details->name;
                $title = $value->title;
                $link1 = '<div class="link1"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link2 = '<div class="link2"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link3 = '<div class="link3"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link4 = '<div class="link4"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link5 = '<div class="link5"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link6 = '<div class="link6"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link7 = '<div class="link7"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link8 = '<div class="link8"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link9 = '<div class="link9"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link10 = '<div class="link10"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                $link11 = '<div class="link11"><a href="./setup/'.$id.'" target="_blank"><strong>'.$name.'<br />'.$a_time.'</strong></a></div>';
                if (!$details->soldout && $title === "Event 1:00 PM") {
                    $cellLink .=  $link1;
                }
                if (!$details->soldout && $title === "Event 10:00 AM") {
                    $cellLink .=  $link2;
                }
                if (!$details->soldout && $title === "Event 2:30 PM") {
                    $cellLink .=  $link3;
                }
                if (!$details->soldout && $title === "Mother's Day Event 1:00 PM") {
                    $cellLink .=  $link4;
                }
                if (!$details->soldout && $title === "Sunset Event 6:00 PM") {
                    $cellLink .=  $link5;
                }
                if (!$details->soldout && $title === "Spring Event 10:00 AM") {
                    $cellLink .=  $link6;
                }
                if (!$details->soldout && $title === "All Day Event 10:00 AM") {
                    $cellLink .=  $link7;
                }
                if (!$details->soldout && $title === "Winter Event 3:00 PM") {
                    $cellLink .=  $link8;
                }
                if (!$details->soldout && $title === "Winter Event 5:00 PM") {
                    $cellLink .=  $link9;
                }
                if (!$details->soldout && $title === "Winter Event 7:00 PM") {
                    $cellLink .=  $link10;
                }
                if (!$details->soldout && $title === "Father's Day Event 1:00 PM") {
                    $cellLink .=  $link11;
                }else{
                }
            }
        }
    }
    return $cellLink . $cellSoldout;
}
for ($i=0; $i<($maxday+$startday); $i++) {
    if(($i % 7) == 0 ) echo "<tr>";
    if($i < $startday) echo "<td></td>";    
    else echo "<td align='center' valign='middle' height='20px'>".($i - $startday + 1) ."<br />". getDateCellContent($obj, $cYear.'-'.$cMonth.'-'.($i - $startday + 1)) ."</td>";
    if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>


太阳
Mon
周二
Wed
周四
Fri
Sat

两个php代码段都在一个文件中吗?这意味着你在构建日历表的脚本中有解码的json?这两个代码段都在一个文件中,不能使用两个不同的文件,因为我正试图使用php插件将其实现到Wordpress中,我无法访问Wordpress中的后端文件,因此必须使用插件向页面添加代码。好的,因此,在迭代构建日历单元格时,只需根据解码的json检查该日期。检查也将是一个简单的迭代,您将当前日期与记录中的日期进行比较,找到后,返回链接或字符串“售罄”。是的,这就是我的问题所在。那么,你能展示一下你试图实现这一点的功能吗?这样我们就可以确定问题所在?已经得到了我正在寻找的答案,我只是忘了将其发布给其他人。无论如何,我会给你这笔赏金,只是为了完成它和你的努力P、 顺便说一句,我不想创建一个新的JSON数组,因为我已经从某个地方得到了一个JSON数组。看起来您正在创建一个新的。从这个角度看,似乎我将从自身而不是从正确的源中提取数据。