JSon / SQL项目 - (NHL得分和标识)仅缺少徽标

时间:2017-04-22 18:17:58

标签: php sql json postgresql

行。

方案: 我使用https://www.mysportsfeeds.com下载了一个NHL匹配的JSON文件。 JSON文件包含团队名称和结果。通过内容,我创建了一个看起来像这样的表。 Json文件如下所示:http://stephaniie.com/scoreboard.json

    scoreboard: {
lastUpdatedOn: "2017-04-17 9:23:44 AM",
gameScore: [
{
game: {
ID: "40295",
date: "2017-04-16",
time: "3:00PM",
awayTeam: {
ID: "25",
City: "Minnesota",
Name: "Wild",
Abbreviation: "MIN"
},
homeTeam: {
ID: "17",
City: "St. Louis",
Name: "Blues",
Abbreviation: "STL"
},
location: "Scottrade Center"
},
isUnplayed: "false",
isInProgress: "false",
isCompleted: "true",
awayScore: "1",
awayShots: "41",
homeScore: "3",
homeShots: "31",
periodSummary: {
period: [
{
@number: "1",
awayScore: "0",
awayShots: "9",
homeScore: "1",
homeShots: "15"
},
{
@number: "2",
awayScore: "1",
awayShots: "13",
homeScore: "1",
homeShots: "8"
},
{
@number: "3",
awayScore: "0",
awayShots: "19",
homeScore: "1",
homeShots: "8"
}
]
}
},
{
game: {
ID: "40287",
date: "2017-04-16",
time: "6:00PM",
awayTeam: {
ID: "10",
City: "Pittsburgh",
Name: "Penguins",
Abbreviation: "PIT"
},
homeTeam: {
ID: "19",
City: "Columbus",
Name: "Blue Jackets",
Abbreviation: "CBJ"
},
location: "Nationwide Arena"
},
isUnplayed: "false",
isInProgress: "false",
isCompleted: "true",
awayScore: "5",
awayShots: "47",
homeScore: "4",
homeShots: "37",
periodSummary: {
period: [
{
@number: "1",
awayScore: "1",
awayShots: "11",
homeScore: "3",
homeShots: "14"
},
{
@number: "2",
awayScore: "2",
awayShots: "19",
homeScore: "0",
homeShots: "8"
},
{
@number: "3",
awayScore: "1",
awayShots: "10",
homeScore: "1",
homeShots: "8"
},
{
@number: "4",
awayScore: "1",
awayShots: "7",
homeScore: "0",
homeShots: "7"
}
]
}
},
{
game: {
ID: "40275",
date: "2017-04-16",
time: "7:00PM",
awayTeam: {
ID: "14",
City: "Montreal",
Name: "Canadiens",
Abbreviation: "MTL"
},
homeTeam: {
ID: "9",
City: "New York",
Name: "Rangers",
Abbreviation: "NYR"
},
location: "Madison Square Gardens"
},
isUnplayed: "false",
isInProgress: "false",
isCompleted: "true",
awayScore: "3",
awayShots: "29",
homeScore: "1",
homeShots: "21",
periodSummary: {
period: [
{
@number: "1",
awayScore: "0",
awayShots: "10",
homeScore: "0",
homeShots: "6"
},
{
@number: "2",
awayScore: "1",
awayShots: "12",
homeScore: "0",
homeShots: "6"
},
{
@number: "3",
awayScore: "2",
awayShots: "7",
homeScore: "1",
homeShots: "9"
}
]
}
},
{
game: {
ID: "40303",
date: "2017-04-16",
time: "10:00PM",
awayTeam: {
ID: "24",
City: "Edmonton",
Name: "Oilers",
Abbreviation: "EDM"
},
homeTeam: {
ID: "26",
City: "San Jose",
Name: "Sharks",
Abbreviation: "SJS"
},
location: "SAP Center"
},
isUnplayed: "false",
isInProgress: "false",
isCompleted: "true",
awayScore: "1",
awayShots: "22",
homeScore: "0",
homeShots: "23",
periodSummary: {
period: [
{
@number: "1",
awayScore: "0",
awayShots: "6",
homeScore: "0",
homeShots: "13"
},
{
@number: "2",
awayScore: "0",
awayShots: "12",
homeScore: "0",
homeShots: "4"
},
{
@number: "3",
awayScore: "1",
awayShots: "4",
homeScore: "0",
homeShots: "6"
}
]
}
}
]
}
}

a busy cat

如您所见,遗憾的是,它不包含团队徽标。并且考虑到这是一个业余爱好项目(非商业性),以一种略微不同的方式进行。

理论值: 我创建了一个带有NHL团队名称的SQL表以及指向其徽标的超链接。 我还创建了一个数据库联系人并使用SELECT语句从SQL表中检索数据。

$ A_query = "SELECT * FROM nhl_team_logo ORDER BY ID" ;
$ A_result = pg_query ($ con, $ a_query) or the ( "Can not execute query: $ a_query \ n" );

然而,现在,我来到了一点上坡。

问题:
我该如何使用逻辑来配对这两个? (Mysports FEED和SQL表)? 显然,我的自然想法是在SQL数据库" Alt-Logo"之间创建一个链接。和饲料团队名称"客队名称"和#34; Home Team-Name"因为两个值都是相同的。但是,我有点不确定如何逻辑写这个。

现在(仅使用Mysport的Feed),HTML代码如下所示:

Table class = "table" >
<Tbody>
<? Php
Foreach ($ nhl ['scoreboard'] ['gameScore'] as $ game) {
Echo '<tr>';
Echo '<td> <img src = "images / used / sports / Untitled-3_13.png" alt = "W3Schools.com" style = "max-width: 80%" > </ td>';
Echo '<td>'. $ Game ['game'] ['away team'] ['name']. '</ Td>';
Echo '<td class = "bold" >'. $ Game ['awayScore']. '-'. $ Game ['homeScore']. '</ Td>';
Echo '<td>'. $ Game ['game'] ['homeTeam'] ['Name']. '</ Td>';
Echo '<td> <img src = "images / used / sports / Untitled-3_14.png" alt = "W3Schools.com" style = "max-width: 80%" > </ td>';
}?>
</ Tbody>
</ Table>
你能帮帮我吗?

编辑:这里是数据库的外观...... SQL Postgres:

a busy cat

答案..

我的一个朋友解决了这个问题。我提供了足够的代码。这是一个相当长的答案,我提供它的原因是.. 1.我想要一个很好的例子来寻找自己。无论哪种方式,我都会写这个副本。原因是,大多数教程,Stackoverflows等,他们提供的代码很少,但它只适用于一个函数。如果你读一本书,你需要整个部分,而不仅仅是scrabled words。这就是为什么一个完整的理解和代码片段对于理解代码是必不可少的,如果你没有,如果你这样做。嗯..如果您已经体验过这种方法和功能,这将不会有任何帮助。

所以我在这里提供Code以从Feeds(XML和JSON)创建新闻项目和体育项目。解析它们。创建数组并在网站上显示它们+包括数据库连接教程。还有如何使用SQL与JSON共同连接来创建两者之间的连接。你是如此欢迎!

结果将是这样的。

test

1。创建数据库连接。我在/config/db.php中使用它。也不要在乎你在我的本地主机上看到我的登录凭据。

db.php中     

$dbhost = 'localhost';
$dbport = 5432;
$dbname = 'acme';
$dbuser = 'devuser';
$dbpass = '123456';

$con = pg_connect("host=".$dbhost." port=".$dbport.". 
    dbname=".$dbname." user=".$dbuser." password=".$dbpass);

2. 为您的XML RSS FEED连接创建一个文件(我使用NYT RSS Feed)。我称之为rss-feeds.php

RSS-FEEDS.PHP

<?php

    //Gets RSS FEEDs
    $url = "http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"; // xmld.xml contains above data
    $feeds = file_get_contents($url);
    $rss = simplexml_load_string($feeds);

    //Creates Array
    $items = [];

    //Loop Feed Objects (images, description) into Array  as they are Media: tags and harder to get.
        foreach($rss->channel->item as $entry) {
        $image = '';
        $image = 'N/A';
        foreach ($entry->children('media', true) as $k => $v) {
            $attributes = $v->attributes();

            if (count($attributes) == 0) {
                continue;
            } else {
                $image = $attributes->url;
            }
        $content_data = (string)$entry->children("media", true)->description;
        $credit = (string)$entry->children("media", true)->credit;
        $creator = (string)$entry->children("dc", true)->creator;
        $pubDate = (string)$entry->pubDate;
        $pubDate = preg_split('/[\s,]+/', $pubDate);


        }

        //Not sure.. guess it find the last two rss feed obects - Title and Link. 
        $items[] = [
            'link' => $entry->link,
            'title' => $entry->title,
            'image' => $image,
            'creator' =>$creator,
            'pubDate' =>$pubDate,
        ];

    }

?>

3. 为您的体育JSON连接创建一个文件(我使用https://www.mysportsfeeds.com/)。我称之为sports-json.php。

SPORTS-JSON.PHP

<?php
error_reporting(E_ALL);

$url = 'https://www.mysportsfeeds.com/api/feed/pull/nhl/2017-playoff/scoreboard.json?fordate=20170416';

$username = 'username';
$password = 'password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_REFERER, $url);


curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);

$result = curl_exec($ch);
$info = curl_getinfo($ch);
$nhl=json_decode($result,true);
curl_close($ch);


?>

4. 在Index.php中 - (网站) - 包括数据库,运动信息源(json)和RSS FEED(XML)为SQL DB表格选择函数..我正在使用Postgres。

<?php
include 'rss-feeds.php';
include 'sports-json.php'; 
include 'config/db.php'; //This is the database connection. 

 function getTeamLogos(){
    global $con;
    $logos = [];
    $a_query = "SELECT logo, team FROM nhl_team_logo;";
    $a_result = pg_query($con, $a_query) or die("Cannot execute");
    $placeholder_logo = 'placeholder.jpg';
    if(pg_num_rows($a_result ) > 0) {
        while ($row = pg_fetch_assoc($a_result )) {
            $logos[$row['team']] = ($row['logo'] ? $row['logo'] : $placeholder_logo);
        }
    }
    return $logos;
}
?>

5. 最后,在(index.php)HTML代码中使用json,xml和sql。我提供了一种演示如何使用这三者中的每一种。

JSON和SQL:

<?php $logos = getTeamLogos();?>
<table class="table">
<tbody>
<?php
   foreach ($nhl['scoreboard']['gameScore'] as $game) {
       echo '<tr>';
       echo '<td><img src="'.$logos[$game['game']['homeTeam']['Name']].'" alt="'.$game['game']['homeTeam']['Name'].'" style="max-width:80%"></td>'; 
      echo '<td>' . $game['game']['awayTeam']['Name'] . '</td>';
      echo '<td class="bold"> ' . $game['awayScore'] . ' - ' . $game['homeScore'] . ' </td>';
      echo '<td>' . $game['game']['homeTeam']['Name'] . '</td>';
      echo '<td><img src="images/used/sports/Untitled-3_14.png" alt="W3Schools.com" style="max-width:80%"></td>';
   } ?>
</tbody>
</table>

XML:

<div class="col-sm-3" >
    <h2 class="rightnow-title ">Right Now</h2><hr>
    <?php $i2=0; 
    foreach ($items2 as $item2) {
        if ($i2 < 4) {
            printf('<div class="right-now-box">');
            printf('<img src="%s" class="width-100">', $item2['image2']);
            printf('<a target="_blank" class="rightnow-text" href="%s">%s</a>', $item2['link2'], $item2['title2']);
            printf('</div>');
            $i2++;
        }
    } ?>
</div> 

还可以使用XML代码构建IF语句以使用不同的设计。 通过以下方式:使用另一个XML项目编号,只需将foreach语句更改为此 - foreach(range(6, 15) as $i) { //将Item6更改为15。

或使用不同的设计 - 不要忘记使用&#34;休息&#34;最后的语句结束if循环。

<?php
   $i=0; 
   foreach ($items as $item) {
    if ($i <= 0) {
      printf('<div class="top-news-flex-parent">');
      printf('<div class="top-news-item">');
      printf('<img class="sidebar-first-img" src="%s">', $item['image']);
      printf('<a target ="_blank" href="%s">%s</a><br>', $item ['link'], $item['title']);
      printf('<span class="reporter">%s</span><span class="date"></span>', $item['creator']);?> <span class="date"> <?php 
      echo  $item['pubDate'][1]; ?> <?php echo $item['pubDate'][2]; ?> <?php echo $item['pubDate'][3];
      printf('</div>');
      printf('</div>');
      $i++;
    } elseif ($i < 5) {
    if ($item['image'] == "N/A") {
        printf('<div class="top-news-flex-parent">');
        printf('<div class="top-news-item">');
        printf('<a href="%s">%s</a><br>', $item ['link'], $item['title']);
        ?> <span class="date"> <?php 
        printf('<span class="reporter">%s</span><span class="date"></span>', $item['creator']);
        echo $item['pubDate'][1]; ?> <?php echo $item['pubDate'][2]; ?> <?php echo $item['pubDate'][3];
        printf('</div>');
        printf('</div>');   
        $i++;
    } else {
        printf('<div class="top-news-flex-parent">');
        printf('<div class="top-news-imgtext" >');
        printf('<a href="%s">%s<a/><br>', $item ['link'], $item['title']);
        printf('<span class="reporter">%s</span><span class="date"></span>', $item['creator']);
        ?> <br><span class="date"> <?php 
        echo $item['pubDate'][1]; ?> <?php echo $item['pubDate'][2]; ?> <?php echo $item['pubDate'][3];
        printf('</div>');
        printf('<div class="top-news-img"><img src="%s"></div>', $item['image']);
        printf('</div>');
        $i++;
    }                           
    } else {
        break;
    }
} ?>

2 个答案:

答案 0 :(得分:0)

JSON文件是否为每个团队都有任何ID?您可以使用此ID与您在表中定义的ID相关联。

另一种选择是从团队名称生成唯一ID。

例如,您可以通过将所有字母转换为大写并使用下划线替换空格来转换团队名称。或者你制作团队名称的MD5。无论如何,您可以使用此ID来匹配这两个。

答案 1 :(得分:0)

我的一个朋友解决了这个问题。我提供了足够的代码。这是一个相当长的答案,我提供它的原因是.. 1.我想要一个很好的例子来寻找自己。所以无论哪种方式,我都要复制这个副本。原因是,大多数教程,Stackoverflows等,他们提供的代码很少,但它只适用于一个函数。如果你读一本书,你需要整个部分,而不仅仅是scrabled words。这就是为什么一个完整的理解和代码片段对于理解代码是必不可少的,如果你不这样做,如果你这样做。当这对你没有任何帮助时,无论如何。

所以我在这里提供Code以从Feeds(XML和JSON)创建新闻项目和体育项目。解析它们。创建数组并在网站上显示它们+包括数据库连接教程。还有如何使用SQL与JSON共同连接来创建两者之间的连接。你是如此欢迎!

结果将是这样的。

test

1。创建数据库连接。我在/config/db.php中使用它。也不要在乎你在我的本地主机上看到我的登录凭据。

db.php中     

$dbhost = 'localhost';
$dbport = 5432;
$dbname = 'acme';
$dbuser = 'devuser';
$dbpass = '123456';

$con = pg_connect("host=".$dbhost." port=".$dbport.". 
    dbname=".$dbname." user=".$dbuser." password=".$dbpass);

2. 为您的XML RSS FEED连接创建一个文件(我使用NYT RSS Feed)。我称之为rss-feeds.php

RSS-FEEDS.PHP

<?php

    //Gets RSS FEEDs
    $url = "http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"; // xmld.xml contains above data
    $feeds = file_get_contents($url);
    $rss = simplexml_load_string($feeds);

    //Creates Array
    $items = [];

    //Loop Feed Objects (images, description) into Array  as they are Media: tags and harder to get.
        foreach($rss->channel->item as $entry) {
        $image = '';
        $image = 'N/A';
        foreach ($entry->children('media', true) as $k => $v) {
            $attributes = $v->attributes();

            if (count($attributes) == 0) {
                continue;
            } else {
                $image = $attributes->url;
            }
        $content_data = (string)$entry->children("media", true)->description;
        $credit = (string)$entry->children("media", true)->credit;
        $creator = (string)$entry->children("dc", true)->creator;
        $pubDate = (string)$entry->pubDate;
        $pubDate = preg_split('/[\s,]+/', $pubDate);


        }

        //Not sure.. guess it find the last two rss feed obects - Title and Link. 
        $items[] = [
            'link' => $entry->link,
            'title' => $entry->title,
            'image' => $image,
            'creator' =>$creator,
            'pubDate' =>$pubDate,
        ];

    }

?>

3. 为您的体育JSON连接创建一个文件(我使用https://www.mysportsfeeds.com/)。我称之为sports-json.php。

SPORTS-JSON.PHP

<?php
error_reporting(E_ALL);

$url = 'https://www.mysportsfeeds.com/api/feed/pull/nhl/2017-playoff/scoreboard.json?fordate=20170416';

$username = 'username';
$password = 'password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_REFERER, $url);


curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);

$result = curl_exec($ch);
$info = curl_getinfo($ch);
$nhl=json_decode($result,true);
curl_close($ch);


?>

4. 在Index.php中 - (网站) - 包括数据库,运动信息源(json)和RSS FEED(XML)为SQL DB表格选择函数..我正在使用Postgres。

<?php
include 'rss-feeds.php';
include 'sports-json.php'; 
include 'config/db.php'; //This is the database connection. 

 function getTeamLogos(){
    global $con;
    $logos = [];
    $a_query = "SELECT logo, team FROM nhl_team_logo;";
    $a_result = pg_query($con, $a_query) or die("Cannot execute");
    $placeholder_logo = 'placeholder.jpg';
    if(pg_num_rows($a_result ) > 0) {
        while ($row = pg_fetch_assoc($a_result )) {
            $logos[$row['team']] = ($row['logo'] ? $row['logo'] : $placeholder_logo);
        }
    }
    return $logos;
}
?>

5. 最后,在(index.php)HTML代码中使用json,xml和sql。我提供了一种演示如何使用这三者中的每一种。

JSON和SQL:

<?php $logos = getTeamLogos();?>
<table class="table">
<tbody>
<?php
   foreach ($nhl['scoreboard']['gameScore'] as $game) {
       echo '<tr>';
       echo '<td><img src="'.$logos[$game['game']['homeTeam']['Name']].'" alt="'.$game['game']['homeTeam']['Name'].'" style="max-width:80%"></td>'; 
      echo '<td>' . $game['game']['awayTeam']['Name'] . '</td>';
      echo '<td class="bold"> ' . $game['awayScore'] . ' - ' . $game['homeScore'] . ' </td>';
      echo '<td>' . $game['game']['homeTeam']['Name'] . '</td>';
      echo '<td><img src="images/used/sports/Untitled-3_14.png" alt="W3Schools.com" style="max-width:80%"></td>';
   } ?>
</tbody>
</table>

<强> XML:

<div class="col-sm-3" >
    <h2 class="rightnow-title ">Right Now</h2><hr>
    <?php $i2=0; 
    foreach ($items2 as $item2) {
        if ($i2 < 4) {
            printf('<div class="right-now-box">');
            printf('<img src="%s" class="width-100">', $item2['image2']);
            printf('<a target="_blank" class="rightnow-text" href="%s">%s</a>', $item2['link2'], $item2['title2']);
            printf('</div>');
            $i2++;
        }
    } ?>
</div> 

还可以使用XML代码构建IF语句以使用不同的设计。 通过以下方式:使用另一个XML项目编号,只需将foreach语句更改为this - foreach(范围(6,15)为$ i){//将Item6更改为15。

或使用不同的设计 - 只是不要忘记在结尾处使用“break”语句来结束if循环。

<?php
   $i=0; 
   foreach ($items as $item) {
    if ($i <= 0) {
      printf('<div class="top-news-flex-parent">');
      printf('<div class="top-news-item">');
      printf('<img class="sidebar-first-img" src="%s">', $item['image']);
      printf('<a target ="_blank" href="%s">%s</a><br>', $item ['link'], $item['title']);
      printf('<span class="reporter">%s</span><span class="date"></span>', $item['creator']);?> <span class="date"> <?php 
      echo  $item['pubDate'][1]; ?> <?php echo $item['pubDate'][2]; ?> <?php echo $item['pubDate'][3];
      printf('</div>');
      printf('</div>');
      $i++;
    } elseif ($i < 5) {
    if ($item['image'] == "N/A") {
        printf('<div class="top-news-flex-parent">');
        printf('<div class="top-news-item">');
        printf('<a href="%s">%s</a><br>', $item ['link'], $item['title']);
        ?> <span class="date"> <?php 
        printf('<span class="reporter">%s</span><span class="date"></span>', $item['creator']);
        echo $item['pubDate'][1]; ?> <?php echo $item['pubDate'][2]; ?> <?php echo $item['pubDate'][3];
        printf('</div>');
        printf('</div>');   
        $i++;
    } else {
        printf('<div class="top-news-flex-parent">');
        printf('<div class="top-news-imgtext" >');
        printf('<a href="%s">%s<a/><br>', $item ['link'], $item['title']);
        printf('<span class="reporter">%s</span><span class="date"></span>', $item['creator']);
        ?> <br><span class="date"> <?php 
        echo $item['pubDate'][1]; ?> <?php echo $item['pubDate'][2]; ?> <?php echo $item['pubDate'][3];
        printf('</div>');
        printf('<div class="top-news-img"><img src="%s"></div>', $item['image']);
        printf('</div>');
        $i++;
    }                           
    } else {
        break;
    }
} ?>