Jump to content

trying to learn but...


cowboysdude
Go to solution Solved by ginerjm,

Recommended Posts

Ok figured I'd go out and try to write me something for my site that I want and can't seem to be able to find what I want...sooooooooooo

 

I'm trying to create an NFL score 'ticker' and so far I've gotten as far as fetching the xml.. that part works LOL

 

I have been sitting here for a few hours trying to figure out how to do a foreach statement that works... I've hit a brick wall because I just can't seem to figure it out...

 

I have even tried a few I've found in other places but no go...

 

Here is what I have so far:

<?php

$url="http://www.nfl.com/liveupdate/scorestrip/postseason/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);
print_r($xml);  //just keeping this here so I can see it for now

?>

I know that part is working as you can see I left the print_r in it temp so I can still see I"m getting what I need...

 

Again the problem is I'm not getting a functional foreach that I can get to work.. I either get nothing back or an error of some kind and trust me I've tried as many as I could find LOL


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

            [g] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015010301
                                    [gsis] => 56492
                                    [d] => Sat
                                    [t] => 8:15
                                    [q] => P
                                    [htn] => Pittsburgh Steelers
                                    [hnn] => steelers
                                    [h] => PIT
                                    [hs] => 0
                                    [vtn] => Baltimore Ravens
                                    [vnn] => ravens
                                    [v] => BAL
                                    [vs] => 0
                                    [n] => NBC
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 1
                                    [gt] => WC
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015010400
                                    [gsis] => 56493
                                    [d] => Sun
                                    [t] => 1:05
                                    [q] => P
                                    [htn] => Indianapolis Colts
                                    [hnn] => colts
                                    [h] => IND
                                    [hs] => 0
                                    [vtn] => Cincinnati Bengals
                                    [vnn] => bengals
                                    [v] => CIN
                                    [vs] => 0
                                    [n] => CBS
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 2
                                    [gt] => WC
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015011000
                                    [gsis] => 0
                                    [d] => Jan 10
                                    [t] => 4:35
                                    [q] => P
                                    [htn] => New England Patriots
                                    [hnn] => patriots
                                    [h] => NE
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => NBC
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 3
                                    [gt] => DIV
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015011101
                                    [gsis] => 0
                                    [d] => Jan 11
                                    [t] => 4:40
                                    [q] => P
                                    [htn] => Denver Broncos
                                    [hnn] => broncos
                                    [h] => DEN
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => CBS
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 4
                                    [gt] => DIV
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015011801
                                    [gsis] => 0
                                    [d] => Jan 18
                                    [t] => 6:40
                                    [q] => P
                                    [hnn] => 
                                    [h] => TBD
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => CBS
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 5
                                    [gt] => CON
                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015011800
                                    [gsis] => 0
                                    [d] => Jan 18
                                    [t] => 3:05
                                    [q] => P
                                    [hnn] => 
                                    [h] => TBD
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => FOX
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 6
                                    [gt] => CON
                                )

                        )

                    [6] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015011100
                                    [gsis] => 0
                                    [d] => Jan 11
                                    [t] => 1:05
                                    [q] => P
                                    [htn] => Green Bay Packers
                                    [hnn] => packers
                                    [h] => GB
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => FOX
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 7
                                    [gt] => DIV
                                )

                        )

                    [7] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015011001
                                    [gsis] => 0
                                    [d] => Jan 10
                                    [t] => 8:15
                                    [q] => P
                                    [htn] => Seattle Seahawks
                                    [hnn] => seahawks
                                    [h] => SEA
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => FOX
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 8
                                    [gt] => DIV
                                )

                        )

                    [8] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015010401
                                    [gsis] => 56494
                                    [d] => Sun
                                    [t] => 4:40
                                    [q] => P
                                    [htn] => Dallas Cowboys
                                    [hnn] => cowboys
                                    [h] => DAL
                                    [hs] => 0
                                    [vtn] => Detroit Lions
                                    [vnn] => lions
                                    [v] => DET
                                    [vs] => 0
                                    [n] => FOX
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 9
                                    [gt] => WC
                                )

                        )

                    [9] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015010300
                                    [gsis] => 56491
                                    [d] => Sat
                                    [t] => 4:35
                                    [q] => P
                                    [htn] => Carolina Panthers
                                    [hnn] => panthers
                                    [h] => CAR
                                    [hs] => 0
                                    [vtn] => Arizona Cardinals
                                    [vnn] => cardinals
                                    [v] => ARI
                                    [vs] => 0
                                    [n] => ESPN
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 10
                                    [gt] => WC
                                )

                        )

                    [10] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015012500
                                    [gsis] => 0
                                    [d] => Jan 25
                                    [t] => 8:00
                                    [q] => P
                                    [htn] => Team Cris Carter
                                    [hnn] => team carter
                                    [h] => CRT
                                    [hs] => 0
                                    [vtn] => Team Michael Irvin
                                    [vnn] => team irvin
                                    [v] => IRV
                                    [vs] => 0
                                    [n] => ESPN
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 11
                                    [gt] => PRO
                                )

                        )

                    [11] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015020100
                                    [gsis] => 0
                                    [d] => Feb 1
                                    [t] => 6:30
                                    [q] => P
                                    [hnn] => 
                                    [h] => TBD
                                    [hs] => 0
                                    [vnn] => 
                                    [v] => TBD
                                    [vs] => 0
                                    [n] => NBC
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 12
                                    [gt] => SB
                                )

                        )

                )

        )

    [gds] => SimpleXMLElement Object
        (
        )

)






My object is returning this and that's great... but my foreach skills need a TON of work apparently... 

 

I think I've looked at it too much and too long... some suggestions would be VERY much appreciated...  Probably a bit big of a project to try on my first real attempt but I just went through the foreach in my tutorial but it's just not working....

 

What I'm looking to do is output the d, t, htn, vtn, hs, vs... 

 

I was trying to just get it to show just one of those but no go...

 

Thanks everyone!!

Link to comment
Share on other sites

You should spend some time learning to read dumps like this.

 

SimpleXMLElement Object
(
$xml is a SimpleXMLElement object

[gms] => SimpleXMLElement Object
with a "gms" child

[g] => Array
which itself has a "g" child that is an array

[0] => SimpleXMLElement Object
of more SimpleXMLElement objects

[@attributes] => Array
and each element in the array has attributes

[eid] => 2015010301
such as "eid".

 

foreach ($xml->gms->g as $g) {
    echo (string)$g["eid"];
Edited by requinix
  • Like 1
Link to comment
Share on other sites

Thank you very much I will try that ... I was coming back to say this is what I have so far....


foreach($xml->gms->g as $games){
    echo "Matchup:".$games->htn->hs->vtn->vs->d->t;
}

But I'm not returning anything ... so I like your explanation Thank you for taking the time to tell me.  I will be going over today and buying a course book in addition to the online course I am taking... find this all VERY interesting and I like it!    SO on my first line I was on the right track... that makes me very happy!!!  

 

Again your explanation is VERY clear and I think I should be able to move forward again!  

 

Thank you so very much!!!  

Link to comment
Share on other sites

  • Solution

Horning in on requinex's very helpful reply I offer this in case she doesn't respond soon:

 

foreach($xml->gms->g as $games)
{
    echo "Matchup:".$games['htn']," ",$games['hs']," vaersus ",$games['vtn']]," ",$games['vs']," on ",$games['d']," at ",$games['t'],"<br>";
}
  • Like 2
Link to comment
Share on other sites

ginerjm is correct.

 

$games is itself the

                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [eid] => 2015010301
                                    [gsis] => 56492
                                    [d] => Sat
                                    [t] => 8:15
                                    [q] => P
                                    [htn] => Pittsburgh Steelers
                                    [hnn] => steelers
                                    [h] => PIT
                                    [hs] => 0
                                    [vtn] => Baltimore Ravens
                                    [vnn] => ravens
                                    [v] => BAL
                                    [vs] => 0
                                    [n] => NBC
                                    [rz] => 0
                                    [ga] => 
                                    [o] => 1
                                    [gt] => WC
                                )
 
                        )
part of the dump. htn and hs and vtn are all "attributes" of $games, and you access each one individually with array syntax ($games['htn'], etc).

<g htn="" hs="" vtn="" />
For ->htn->hs->vtn to work, htn would have to be a child of $games, hs a child of htn, vtn a child of hs, and so on.

<g>
    <htn>
        <hs>
            <vtn />
        </hs>
    </htn>
</g>
[edit] Casting to string, as with the (string)$g["eid"] in my earlier reply, is because those values are actually SimpleXMLElement objects. Not strings. They can become strings easily with a cast or when a function tries to treat the value as a string.

echo is the latter case. You can echo those values without problems not because they are strings but because each SimpleXMLElement object is being automatically converted to a string.

echo gettype($games["htn"]); // object
Keep that in mind because it trips people up when suddenly PHP complains that a value they thought was a string was actually an object the whole time. Edited by requinix
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.