Hey JimL one more question LOL

[php]
$url="http://www.nfl.com/liveupdate/scorestrip/ss.xml ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);

$find = array(
“ARI”,
“ATL”,
‘BAL’,
‘BUF’,
‘CAR’,
‘CHI’,
‘CIN’,
‘CLE’,
‘DAL’,
‘DEN’,
‘DET’,
‘GB’,
‘HOU’,
‘IND’,
‘JAC’,
‘KC’,
‘MIA’,
‘MIN’,
‘NE’,
‘NO’,
‘NYJ’,
‘NYG’,
‘OAK’,
‘PHI’,
‘PIT’,
‘STL’,
‘SD’,
‘SF’,
‘SEA’,
‘TAM’,
‘TEN’,
‘WAS’

);

$replace = array(
“Arizona Cardinals”,
“Atlanta Falcons”,
‘Baltimore Ravens’,
‘Buffalo Bills’,
‘Carolina Panthers’,
‘Chicago Bears’,
‘Cincinnati Bengals’,
‘Cleveland Browns’,
‘Dallas Cowboys’,
‘Denver Broncos’,
‘Detroit Lions’,
‘Green Bay Packers’,
‘Houston Texans’,
‘Indianapolis Colts’,
‘Jacksonville Jaguars’,
‘Kansas City Chiefs’,
‘Miami Dolphins’,
‘Minnesotta Vikings’,
‘New England Patriots’,
‘New Orleans Saints’,
‘New York Jets’,
‘New York Giants’,
‘Oakland Raiders’,
‘Philadelphia Eagles’,
‘Pittsburgh Steelers’,
‘Saint Louis Rams’,
‘San Diego Chargers’,
‘San Francisco 49ers’,
‘Seattle Seahawks’,
‘Tampa Bay Buccaneers’,
‘Tennessee Titans’,
‘Washington Redskins’
);

$games = array(
‘REG’ => array(),
‘WC’ => array(),
‘DIV’ => array(),
‘CON’ => array(),
‘PRO’ => array(),
‘SB’ => array()

);

foreach($xml->gms->g as $game) { // it’s a single game
// if (!empty($game[‘vtn’]))
{

 $games[(string) $game['gt']][] = $game;

}
}
?>

 <?php
 
 foreach ($games as $gametype => $subgames) {
   foreach ($subgames as $game) {
      if ($game['gt'] == 'REG') {
     	 $game = (str_replace($find, $replace, $game));
   ?>

[/php]

I added this and it returns nothing LOL

I believe that I have populated the $game variable then once I added this:

[php]
$game = (str_replace($find, $replace, $game)); [/php]

I believe I destroy that variable. What I’m trying to do is during the regular season the xml outputs team names like this DAL for Dallas Cowboys, NYG for New York Giants, etc…

SO when I’m showing the regular season games it just shows [example] - DAL 21 NYG 30

So I am trying to use the str_replace to replace the DAL with Dallas Cowboys…

Suggestions as to where I went wrong?

Thanks again.

[php]$game[‘gt’] = (str_replace($find, $replace, $game[‘gt’]));[/php]

I also tried this… I have a feeling I may be putting it in the wrong order…

str_replace expects the subject to be a string or an array.

In the first example you are sending in the entire xml object for the game

ie:

[code]SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014122801
[gsis] => 56411
[d] => Sun
[t] => 1:00
[q] => F
[h] => BAL
[hnn] => ravens
[hs] => 20
[v] => CLE
[vnn] => browns
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)

)[/code]use print_r($variable); to print out the variable to see what you are working with. Prepend with a echo ‘

’; to get it pretty printed.

[hr]

In the other example you are sending in the string stored in $game[‘gt’], which in the example above is “REG”. I expect you want this to be $game[‘v’] or $game[‘h’]

[php]foreach ($games as $gametype => $subgames) {
foreach ($subgames as $game) {
if ($game[‘gt’] == ‘REG’) {
$homeTeam = str_replace($find, $replace, $game[‘h’]);
$visitingTeam = str_replace($find, $replace, $game[‘v’]);
echo $homeTeam . ’ vs ’ . $visitingTeam . ‘
’;
}
}[/php]

Baltimore Ravens vs Cleveland Browns Houston Texans vs Jacksonville Jaguars Kansas City Chiefs vs San Diego Chargers Miami Dolphins vs New York Jets Minnesotta Vikings vs Chicago Bears New England Patriots vs Buffalo Bills New York Giants vs Philadelphia Eagles TB vs New Orleans Saints Tennessee Titans vs Indianapolis Colts Washington Redskins vs Dallas Cowboys Atlanta Falcons vs Carolina Panthers Denver Broncos vs Oakland Raiders Green Bay Packers vs Detroit Lions Seattle Seahawks vs Saint Louis Rams San Francisco 49ers vs Arizona Cardinals Pittsburgh Steelers vs Cincinnati Bengals

also by doing $game = // something… you are overwriting the game variable, removing all the other content in it.

[hr]

Also, please note that atm your script will hit nfl.com for every page view you get. You should cache the result locally. It could be as simple as storing the values to a file (with a timestamp), then before making the request, check if the file is newer than x minutes/hours/days, and if so use that instead. Then you can have thousands of visits without bothering nfl.com

Great idea on file storage! Thank you!

Yeah I figured I was overwriting what was in the $game variable and wiping it all out and thus no return… that is what I said earlier ;D

Always a problem… I thought I was on the right track and tried many times to figure it out… but I see what you did and that makes MUCH more sense to me!

Thanks!!!

This is where I’m at… all working great!! I was on the right track just trying to manipulate the wrong element…

[php]
foreach($xml->gms->g as $game) {
// if (!empty($game[‘vtn’]))
{
$games[(string) $game[‘gt’]][] = $game;
}
}
?>

 <?php
 foreach ($games as $gametype => $subgames) {
   foreach ($subgames as $game) {
     if ($game['gt'] == 'REG') {
     	$homeTeam = str_replace($find, $replace, $game['h']);
            $visitingTeam = str_replace($find, $replace, $game['v']);
   ?>
       <li data-subcategory='Regular Season' data-category='Week <?= $game['w'] ?>' data-color='1979ab'>
         <a href='#'><b><?= $homeTeam ?> <?= $game['hs'] ?> &nbsp;&nbsp;  <?= $visitingTeam ?>  <?= $game['vs'] ?></b></a>
       </li>

[/php]

Just one more question that I tried to figure out but am having a bit of problems…

[php]
'Week <?= $game['w'] ?>’
[/php]

I know the problem is here…

It’s not loading the ‘gms’ just the ‘g’… this line :

isn’t loading because the [‘w’] returns empty.

I have tried
It is this: [php]foreach($xml->gms->g as $game)[/php]
and I have tried to change it to this: [php]foreach($xml->gms as $game)[/php]

But that is a no go… it returns nothing at all… just blank. That is a bit confusing to me because it would seem that it would start loading all the data from gms on down like it does for the g but it’s not… but if you use the gms->g as $game it populates that ok. Why is that?

since you only have one of these result sets you don’t need to loop over anything.

[php]echo ‘

’;
print_r($xml->gms);[/php]

[code]SimpleXMLElement Object
(
[@attributes] => Array
(
[w] => 21
[y] => 2014
[t] => POST
[gd] => 1
[bf] => 0
[bph] => 0
)

[g] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [eid] => 2015010301
                        ...[/code]

So…

[php]echo ‘

’;
print_r($xml->gms[‘w’]);[/php]

SimpleXMLElement Object ( [0] => 17 )

And just to get what we really want we cast it to an integer

[php]$someInt = (int) $xml->gms[‘w’];
print_r($someInt);[/php]

17

Ok that was a really explanation and I appreciate it!! It worked like a charm and I even understand it…

What I’m learning is that there is more then one way to do something and today I just learned how to get one piece of info, namely a number, and set it to (int) from an xml such as going to the 1st ‘tag’ as I call it and getting the info I need by pulling it from the $xml variable that it’s assigned to.

I see so assign the info to a variable such as $xml then from that use various methods depending on what you want to get results…

In this case one bit of information in the loop reading multiple elements and putting those out in either HTML or PHP form.

I think I got it!! Thank you so much!!!

Yeah that “w” param is shared across all the results, so no need to fetch it in a loop. We can just fetch it outside the loop and then either show it before/after the results, or show it for every result in the loop that outputs the games :slight_smile:

Good convention: do as few things as possible inside loops.

I agree I can see where loops can get quite confusing not to mention if I don’t have to use one I’m not going to :smiley:

I think you might be overcomplicating it, loops are actually extremely simple. What I meant was that one should limit the amount of work done in loops, as everything inside the loop is done n times. Like the assignment we did for the variable w, there is absolutely no reason to do that for every game ^^

I suggest you play around with some arrays and looping over them. If you look at the array structure before setting up the loop there is absolutely no magic behind it.

ex:

[code]Array
(
[0] => Array
(
[id] => 1
[title] => the first one
)

[1] => Array
    (
        [id] => 2
        [title] => the second one
    )

[2] => Array
    (
        [id] => 3
        [title] => the third one
    )

)[/code]

[php]$foreach ($array as $item) {[/php]

Will do this:

[code]Array
(
[0] => // first iteration, $item = Array
(
[id] => 1
[title] => the first one
)

[1] => // second iteration, $item = Array
    (
        [id] => 2
        [title] => the second one
    )

[2] => // third iteration, $item = Array
    (
        [id] => 3
        [title] => the third one
    )

)[/code]

Oh Ok I get you. Yes I see that if you plot out what you’re looking for and how to get it then it does make it much easier.

What I started out doing what printing out the array like your example and that’s how I got to understand the [0], [1], [2] etc… Just wasn’t sure how to get that info to show correctly, I do now!! :slight_smile:

I really do appreciate you taking the time to explain them all to me. It helps greatly!!! This stuff is fun to learn… I don’t get too frustrated I just think at times I’m trying to make something harder then it has to be… the ole ‘it can’t be that easy, can it?’ When in truth at times it can be just that easy!!!

That xml is unique as it has different headings and to me seemed more complicated but Thanks to you I understand how to get the information out of it!!

So now what I’ve done is looked at it in total and finding ways to tweak it here and there…learning more.

I’ve made a couple of changes and in the backend I have the choice now of either “Regular Season” or “Post Season” and it’s in list format so I pick one and that’s what it displays… it’s all really awesome! Well I think so anyway LOL It’s one of a kind… I made some graphics for it and it’s just about there… One more thing I want to do…well actually two more things I want to do to it then I believe it’s complete.

One is taking your suggestion and downloading the actual xml so I don’t have to make so many requests to the actual file on their server… so in doing that I need to get and keep it with my files then every so often go back and recheck it [score changes, etc]. I’m playing with that now.

And the other is getting the [g eid] info and putting that in a date format that works. Right now it shows up as g eid=“2015010301” and from that I have figured out that the first 8 digits are the actual date of the game… 01/03/2015 :slight_smile:

But again you have been a huge help and have a ton of patience and I really do appreciate that!!

I am just not catching onto this at all… :’(

This is what I did to get the date [‘eid’] and I have it reading and outputting the first one with this:

[php]
$gdate = $xml->gms->g[‘eid’];
$date = str_split($gdate,1);
foreach (
$fdates= $date[5] . “-” . $date[7] . “-” . $date[0] . $date[1] . $date[2] . $date[3] as $fdate){
}
print_r($fdates);
[/php]

I have tried to loop this thing for 3 days now LOL I KNOW this is totally wrong looping attempt …

<g eid="2015010301" gsis="56492" d="Sat" t="8:15" q="F" htn="Pittsburgh Steelers" hnn="steelers" h="PIT" hs="17" vtn="Baltimore Ravens" vnn="ravens" v="BAL" vs="30" n="NBC" rz="0" ga="" o="1" gt="WC"/>
<g eid="2015010400" gsis="56493" d="Sun" t="1:05" q="F" htn="Indianapolis Colts" hnn="colts" h="IND" hs="26" vtn="Cincinnati Bengals" vnn="bengals" v="CIN" vs="10" n="CBS" rz="0" ga="" o="2" gt="WC"/>
<g eid="2015011000" gsis="56495" d="Sat" t="4:35" q="F" htn="New England Patriots" hnn="patriots" h="NE" hs="35" vtn="Baltimore Ravens" vnn="ravens" v="BAL" vs="31" n="NBC" rz="0" ga="" o="3" gt="DIV"/>
<g eid="2015011101" gsis="56498" d="Sun" t="4:40" q="F" htn="Denver Broncos" hnn="broncos" h="DEN" hs="13" vtn="Indianapolis Colts" vnn="colts" v="IND" vs="24" n="CBS" rz="0" ga="" o="4" gt="DIV"/>
<g eid="2015011801" gsis="56500" d="Sun" t="6:50" q="F" htn="New England Patriots" hnn="patriots" h="NE" hs="45" vtn="Indianapolis Colts" vnn="colts" v="IND" vs="7" n="CBS" rz="0" ga="" o="5" gt="CON"/>

I just can’t imagine why I’m making this so hard… I knew I had the first one so I tried to loop with foreach to get them all… but of course it’s not looping. I am making this so hard for myself that it’s funny… yes I am laughing…

I know in the foreach I’m making it equal to something and there in is the problem but if I take out the foreach or make any changes to that line it fails…

Im not sure what you are trying to do. If you want the date for each game then you have it. Just add an echo for it in the loop where yoh output game info

I knew you’d say that!! Honestly I really did LOL

But I have to put in the correct format… it does output the $game[‘eid’] but like this: 2015010301

I’m trying to put it like this: 01/03/2015 :wink:

So I need to grab it, and print the 5th, 7th, 0, 1st, 2nd and 3rd values from that key…

Thanks Jim!

This is what I did and the result:

added this:
[php]
$gdate = str_split($game[‘eid’],2);
$gdates = ($gdate[2]."/".$gdate[3]."/".$gdate[0].$gdate[1]);
[/php]

To the ‘loop’ :

[php]
foreach ($games as $gametype => $subgames) {
foreach ($subgames as $game) {
if ($game[‘gt’] == ‘REG’) {
$homeTeam = str_replace($find, $replace, $game[‘h’]);
$visitingTeam = str_replace($find, $replace, $game[‘v’]);

[/php]

Result:

[php]
foreach ($games as $gametype => $subgames) {
foreach ($subgames as $game) {
$gdate = str_split($game[‘eid’],2);
$gdates = ($gdate[2]."/".$gdate[3]."/".$gdate[0].$gdate[1]);
if ($game[‘gt’] == ‘REG’) {
$homeTeam = str_replace($find, $replace, $game[‘h’]);
$visitingTeam = str_replace($find, $replace, $game[‘v’]);

[/php]

and I call it like this:

[php]

  • <?= $gdates?> <?= $game['htn'] ?> <?= $game['hs'] ?> <?= $game['vtn'] ?> <?= $game['vs'] ?> <?= $game['d'] ?> at <?= $game['t'] ?> [/php]

    So I end up with what I want like this:
    01/18/2015 New England Patriots 45 Indianapolis Colts 7 Sun at 6:50

    :slight_smile:

    Well I’m starting to figure some things out so I’m quite happy about that… I’m sure there was a shorter way but hey it works and just that alone makes me very happy that I got it!!

  • Compare with what I linked you to

    [php]$date = DateTime::createFromFormat(‘Ymd’, substr($game[‘eid’], 0, 8));
    echo $date->format(‘d/m/Y’);[/php]

    Gotcha! Thank you!! Yes that looks much better… and more to the point!!!

    I was just happy to figure out something that worked! I understand what you’re saying though.

    There it is :slight_smile: In all it’s glory!

    [php]
    foreach ($subgames as $game) {
    $date = DateTime::createFromFormat(‘Ymd’, substr($game[‘eid’], 0, 8));
    $gdates = $date->format(‘n/j/Y’);
    if ($game[‘gt’] == ‘REG’) {
    $homeTeam = str_replace($find, $replace, $game[‘h’]);
    $visitingTeam = str_replace($find, $replace, $game[‘v’]);

    [/php]

    Thanks Jim… it is better form!!!

    Sponsor our Newsletter | Privacy Policy | Terms of Service