dongxiao3694 2015-11-12 18:01
浏览 50

从api中分离数据

we are trying to make a website for our class, here you can see important news, the class schedule, homework schedule and a contact page for tips and bugs...

So, we want to have the schedule always up to date and a class mate made a api to get the schedule-data from our school site. but now we have an issue... he don't want to help us anymore!!! -,-

here is the question? The schedule is echo in a table but it stand next to eachother, because the api only separate the days including the lesson. but we want the lesson seperate from the day! and echo the table below each other..

Is there anyone who want to help me / Us out??? Link for a live previeuw how its now!

<?php

$apis = array('verandermij');

if (!isset($_GET['afdeling'])) //school sector
{
    echo "FOUTCODE 1"; //Wrongcode
    exit;
}

if (!isset($_GET['klas'])) // class
{
    echo "FOUTCODE 2"; //Wrongcode
    exit;
}

if (!isset($_GET['api']))
{
    echo "FOUTCODE 3"; //Wrongcode
    exit;
}

$apigeldig = false;
foreach ($apis as $api)
{ 
    if ($api == $_GET['api'])
    {
        $apigeldig = true;
    }
}

if ($apigeldig == false)
{
    echo "FOUTCODE 4"; //Wrongcode
    exit;
}

$afdeling = $_GET['afdeling'];
$klas = $_GET['klas'];

// get the class data
$url = "https://rooster.rocfriesepoort.nl/emmeloord.aspx?group=" . $afdeling . "&specs=" . $klas . "&a=1";
$content = file_get_contents($url);

// little bit cleaning
$content = explode("<div class=header>Rooster</div>", $content);
$content = $content[1];

// Splits dagen
$raw_dagen = explode("<td valign=top style='background-color: #e5e5e5'>", $content); //Raw_Days
$clean_dagen = array(); // Clean_Days

$dagen = array( //Days
    $raw_dagen[1],
    $raw_dagen[2],
    $raw_dagen[3],
    $raw_dagen[4],
    $raw_dagen[5]
);

// Beginnen met sorteren
$opgeschoond = array(); //Cleand

$stoploop = false;
foreach ($dagen as $dag) //Days as Day
{
    $tmp = explode("</div>", $dag);
    $i = 0;
    foreach ($tmp as $item)
    {
        if ($stoploop == true) { $i++; continue; }
        if ($tmp[$i] == null) { $i++; continue; }
        $tmp[$i] = str_replace("<div class='dagHeader'>", "", $tmp[$i]);
        $tmp[$i] = str_replace("<div class=tablecell3>", "VRIJ", $tmp[$i]);
        $tmp[$i] = str_replace("<div class=tablecell1>", "", $tmp[$i]);
        $tmp[$i] = str_replace("<div class=tablecell2>", "", $tmp[$i]);
        $tmp[$i] = str_replace('<br style="clear:both"></br><br><br><div id=footer style="width:95%;">&copy; ROC Friese Poort 2015 - ', "", $tmp[$i]);
        $tmp[$i] = str_replace("<a href='javascript:void(0)'". ' onclick="Disclaimer' . "('500','500','YES')" . '" title=' . "'Disclaimer'>", "", $tmp[$i]);

        if ($tmp[$i] == "</td>")
        {
            $tmp[$i] = "DAG EINDE";
        }
        if ($tmp[$i] == "</td></table>")
        {
            $tmp[$i] = "DAG EINDE";
            unset($tmp[$i + 1]);
            unset($tmp[$i + 2]);
            unset($tmp[$i + 3]);
            unset($tmp[$i + 4]);
            unset($tmp[$i + 5]);
            unset($tmp[$i + 6]);
            $stoploop = true;
        }
        $i++;
    }
    array_push($opgeschoond, $tmp);
}

print(json_encode($opgeschoond));

?>

this we use the echo the data!!!!!

<table>
        <tr>
            <?php
                foreach ($maandag as $lesuur)
                {
                    print("<td>");
                        print($lesuur);
                    print("</td>");
                }

            ?>
        </tr>
        <tr>
            <?php
                foreach ($dinsdag as $lesuur)
                {
                    print("<td id='oneven'>");
                        print($lesuur);
                    print("</td>");
                }

            ?>
        </tr>
        <tr>
            <?php
                foreach ($woensdag as $lesuur)
                {
                    print("<td>");
                        print($lesuur);
                    print("</td>");
                }

            ?>
        </tr>
        <tr>
            <?php
                foreach ($donderdag as $lesuur)
                {
                    print("<td id='oneven'>");
                        print($lesuur);
                    print("</td>");
                }

            ?>
        </tr>
        <tr>
            <?php
                foreach ($vrijdag as $lesuur)
                {
                    print("<td>");
                        print($lesuur);
                    print("</td>");
                }

            ?>
        </tr>
    </table>

link for the raw data: enter link description here

[["Maandag 09-11-2015","MTM

Nederlands C2.02 ","MTM Nederlands A2.08 ","VRIJ","KAS Wiskunde B2.02 ","KSR SLB A2.07 ","DAG EINDE"],["Dinsdag 10-11-2015","VRIJ","CRL Database techn. A2.07 ","CRL Database techn. A2.07 ","CRL CMS A2.07 ","VRIJ","CRL CMS A2.07 ","CRL Webservers A2.07 ","CRL Webservers A2.07 ","DAG EINDE"],["Woensdag 11-11-2015","VRIJ","VRIJ","KSR Java Script A2.07 ","KSR Java Script A2.07 ","VRIJ","VRIJ","VSS Routeplanner B2.04 ","VSS LevO B2.04 ","VSS LLB B2.04 ","DAG EINDE"],["Donderdag 12-11-2015","VRIJ","KRJ Project A2.07 ","KRJ Project A2.07 ","KRJ Project A2.07 ","VRIJ","KSR Scrum A2.07 ","GFO Rekenen B2.01 ","KSR Scrum A2.07 ","DAG EINDE"],["Vrijdag 13-11-2015","ZAA Engels B2.03 ","ZAA Engels B2.06a ","KRJ Project A2.07 ","KRJ Project A2.07 ","VRIJ","KSR SLB A2.07 ","GFO Rekenen B2.06 ","DAG EINDE"]]

thanks for helping me, a beginner (a)

  • 写回答

1条回答 默认 最新

  • dpv50040 2015-11-12 19:09
    关注

    The data returned from the 'api' is a two dimensional array, with the second dimension variable width, but the first entry is the day. data[0][0] = "Maandag 09-11-2015";.

    If I understand correctly, what you want is something like this:

    $apidata = file_get_contents($apiurl);
    $data = json_decode($apidata, true);
    
    $maandag = array_slice($data[0],1)
    $dinsdag = array_slice($data[1],1)
    $woensdag = array_slice($data[2],1)
    $donderdag = array_slice($data[3],1)
    $vrijdag = array_slice($data[4],1)
    

    Then your echo from above will work, the day names removed from the arrays (though also the dates).

    Once in this form, building a table for the days is relatively simple (though I'm sure others will have a cleaner way to do it):

    <?php
    $longest = 0;
    for($i=0;$i<5;$i++) {
      if($data[$i].count()>$longest) $longest=$data[$i].count();
    }
    $longest--; // we removed one entry already
    ?>
    <table>
    <tr><th>Maandag</th><th>Dinsdag</th><th>Woensdag</th><th>Donderdag</th><th>Vrijdag</th></tr>
    <?php
    for($i=0;$i<$longest;$i++) {
      print "<tr><td>{$maandag[$i]}</td><td>{$dinsdag[$i]}</td><td>{$woensdag[$i]}</td><td>{$donderdag[$i]}</td><td>{$vrijdag[$i]}</td></tr>
    ";
    }
    ?>
    </table>
    

    That should have you set. There may be runtime errors attempting to access the array entries of the shorter days before the longest day is finished, but you can fix that with another loop adding empty strings.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法