【发布时间】:2020-03-20 09:54:37
【问题描述】:
几天来,我一直在试图找出做我需要做的事情的最佳方法。我有一个 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>
所以要再次分解它,我需要将$url 插入日历中,其中start 日期与日历正确日期匹配,但前提是与soldout 不匹配。如果soldout 等于true,那么它可能应该只是echo "SoldOut"; 或留空,这就是那种性质。在正确的轨道上获取日历中的信息的任何帮助都会很棒。
【问题讨论】:
-
你的 php sn-ps 是否都在一个文件中?是否意味着您在构建日历表的脚本中提供了解码的 json?
-
两个 sn-ps 都在一个文件中,不能使用 2 个不同的文件,因为我正在尝试使用 php 插件将其实现到 Wordpress 中,我无法访问 wordpress 中的后端文件所以有使用插件向页面添加代码。
-
好的,所以在迭代构建日历单元时,您只需根据解码的 json 检查该日期。检查也将是一个简单的迭代,您将当前日期与记录中的日期进行比较,找到后返回链接或字符串“sold out”。
-
是的,这就是我遇到问题的地方,哈哈
-
那么您能否展示您尝试制作的实现此目的的功能,以便我们确定问题出在哪里?
标签: php html arrays json compare