Order of things...

Well I’ve gotten this far and am learning like a sponge but this one is either too easy or too hard… not sure which LOL

Here is what I have and it works great but would like to be able to show ALL one type of game then the next and so on…

Right now it shows 2 Wild card games… then 2 Divisional games then both Conference games… then 2 Div games then the last 2 Wild card games… It’s just reading the order of the xml file …

Question is how can I make it show all one type, then the next and so on…

Thanks again!!

[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);
foreach($xml->gms->g as $games)
{
if(!empty($games[‘vtn’]))
{
if ((($games[‘gt’]) ==‘WC’) && (($games[‘q’])== ‘F’)) {
echo “

  • ”,$games[‘htn’]," “,$games[‘hs’],” “,$games[‘vtn’],” “,$games[‘vs’],”  Final
  • ";
    }
    if (($games[‘gt’]) ==‘DIV’) {
    echo “
  • ”,$games[‘htn’]," “,$games[‘hs’],” “,$games[‘vtn’],” “,$games[‘vs’],” “,$games[‘d’],” at “,$games[‘t’], “
  • ”;
    }
    if (($games[‘gt’]) == ‘CON’) {
    echo “
  • ”,$games[‘htn’],” “,$games[‘hs’],” “,$games[‘vtn’],” “,$games[‘vs’],” “,$games[‘d’],” at ",$games[‘t’], “
  • ”;
    }
    }
    }
    [/php]

    OH here’s the xml it’s following just in case my explanation is bad…

    <ss>
    <gms w="20" y="2014" t="POST" gd="0" bf="0" bph="0">
    <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:40" q="P" htn="New England Patriots" hnn="patriots" h="NE" hs="0" vtn="Indianapolis Colts" vnn="colts" v="IND" vs="0" n="CBS" rz="0" ga="" o="5" gt="CON"/>
    <g eid="2015011800" gsis="56499" d="Sun" t="3:05" q="P" htn="Seattle Seahawks" hnn="seahawks" h="SEA" hs="0" vtn="Green Bay Packers" vnn="packers" v="GB" vs="0" n="FOX" rz="0" ga="" o="6" gt="CON"/>
    <g eid="2015011100" gsis="56497" d="Sun" t="1:05" q="F" htn="Green Bay Packers" hnn="packers" h="GB" hs="26" vtn="Dallas Cowboys" vnn="cowboys" v="DAL" vs="21" n="FOX" rz="0" ga="" o="7" gt="DIV"/>
    <g eid="2015011001" gsis="56496" d="Sat" t="8:15" q="F" htn="Seattle Seahawks" hnn="seahawks" h="SEA" hs="31" vtn="Carolina Panthers" vnn="panthers" v="CAR" vs="17" n="FOX" rz="0" ga="" o="8" gt="DIV"/>
    <g eid="2015010401" gsis="56494" d="Sun" t="4:40" q="F" htn="Dallas Cowboys" hnn="cowboys" h="DAL" hs="24" vtn="Detroit Lions" vnn="lions" v="DET" vs="20" n="FOX" rz="0" ga="" o="9" gt="WC"/>
    <g eid="2015010300" gsis="56491" d="Sat" t="4:35" q="F" htn="Carolina Panthers" hnn="panthers" h="CAR" hs="27" vtn="Arizona Cardinals" vnn="cardinals" v="ARI" vs="16" n="ESPN" rz="0" ga="" o="10" gt="WC"/>
    <g 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"/>
    <g 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"/>
    </gms>
    <gds>
    <gd eid="2015010301" gsis="56492" h1q="3" h2q="6" h3q="0" h4q="8" hot="0" v1q="0" v2q="10" v3q="10" v4q="10" vot="0" htol="0" vtol="1"/>
    <gd eid="2015010400" gsis="56493" h1q="7" h2q="6" h3q="10" h4q="3" hot="0" v1q="7" v2q="3" v3q="0" v4q="0" vot="0" htol="2" vtol="0"/>
    <gd eid="2015011000" gsis="56495" h1q="7" h2q="7" h3q="14" h4q="7" hot="0" v1q="14" v2q="7" v3q="7" v4q="3" vot="0" htol="1" vtol="0"/>
    <gd eid="2015011101" gsis="56498" h1q="7" h2q="3" h3q="0" h4q="3" hot="0" v1q="0" v2q="14" v3q="7" v4q="3" vot="0" htol="0" vtol="2"/>
    <gd eid="2015011100" gsis="56497" h1q="7" h2q="3" h3q="10" h4q="6" hot="0" v1q="7" v2q="7" v3q="7" v4q="0" vot="0" htol="1" vtol="0"/>
    <gd eid="2015011001" gsis="56496" h1q="7" h2q="7" h3q="0" h4q="17" hot="0" v1q="0" v2q="10" v3q="0" v4q="7" vot="0" htol="2" vtol="0"/>
    <gd eid="2015010401" gsis="56494" h1q="0" h2q="7" h3q="7" h4q="10" hot="0" v1q="14" v2q="3" v3q="3" v4q="0" vot="0" htol="1" vtol="1"/>
    <gd eid="2015010300" gsis="56491" h1q="10" h2q="3" h3q="14" h4q="0" hot="0" v1q="0" v2q="14" v3q="0" v4q="2" vot="0" htol="0" vtol="0"/>
    </gds>
    </ss>

    Instead of outputting it directly you could just add it to an array in the first loop

    So for each game you do
    $games[$type][] = $game;

    Then you can add another loop (for output) where you will have them sorted

    Great idea!! Thank you!! I will now go read about that… it’s the only way I’m going to learn :slight_smile:

    But I have them all styled differently… will that effect that? Or do you mean output them as a much shorter string and keep the styling?

    Thanks!

    Then style/output them differently depending on which type it is. Since they are under the type in the output array you have that available :slight_smile:

    Ok, Thank you again!!! :slight_smile:

    Yeah for some reason it’s just not coming to me… it works now so until I can figure out what you’re telling me it’ll have to run like it is… :’(

    Thank you for all the help!!

    imagine the data you added, and this loop:

    [php]$games = array(‘CON’, ‘DIV’, ‘WC’);
    foreach($xml->gms->g as $game) { // it’s a single game
    $games[$game[‘gt’]][] = $game; // add the game to the games array
    }[/php]

    Let’s loop through it together :slight_smile:

    [hr]

    We enter the loop with this structure:
    [php]$games = array(
    ‘CON’,
    ‘DIV’
    ‘WC’
    )[/php]

    [hr]

    First iteration:

    $game =

    <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"/>

    [php]$games = array(
    ‘CON’,
    ‘DIV’,
    ‘WC’ => [
    0 => [
    ‘eid’ => 2015010301,
    ‘hnn’ => ‘steelers’,
    ‘vnn’ => ‘ravens’,
    etc…
    ]
    ]
    )[/php]

    [hr]

    Second iteration:

    $game =

    <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"/>

    [php]$games = array(
    ‘CON’,
    ‘DIV’,
    ‘WC’ => [
    0 => [
    ‘eid’ => 2015010301,
    ‘hnn’ => ‘steelers’,
    ‘vnn’ => ‘ravens’,
    etc…
    ],
    1 => [
    ‘eid’ => 2015010400,
    ‘hnn’ => ‘colts’,
    ‘vnn’ => ‘bengals’,
    etc…
    ]
    ]
    )[/php]

    [hr]

    Third iteration:

    $game =

    <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"/>

    [php]$games = array(
    ‘CON’,
    ‘DIV’ => [
    0 => [
    ‘eid’ => 2015011000,
    ‘hnn’ => ‘patriots’,
    ‘vnn’ => ‘ravens’,
    etc…
    ]
    ],
    ‘WC’ => [
    0 => [
    ‘eid’ => 2015010301,
    ‘hnn’ => ‘steelers’,
    ‘vnn’ => ‘ravens’,
    etc…
    ],
    1 => [
    ‘eid’ => 2015010400,
    ‘hnn’ => ‘colts’,
    ‘vnn’ => ‘bengals’,
    etc…
    ]
    ]
    )[/php]

    etc…

    [hr]

    So now, you can loop through it like this:

    [php]foreach ($games as $gametype) {
    echo $gametype . ‘
    ’;
    foreach ($gametype as $game) {
    echo $gamestuff;
    }
    }[/php]

    And it will go through the array as it’s written.

    First CON
    Then the games under CON (in order of insertion)

    Then DIV
    Then the games under DIV (in order of insertion)

    Then WC
    Then the games under WC (in order of insertion)

    Very good explanation and I appreciate it… this is what I have:

    [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);

    $games = array(‘WC’, ‘DIV’, ‘CON’);
    foreach($xml->gms->g as $game) { // it’s a single game
    $games[$game[‘gt’]][] = $game; // add the game to the games array
    }

    foreach ($games as $gametype) {
    echo $gametype . ‘
    ’;
    foreach ($gametype as $game) {
    echo $gamestuff;
    }
    }[/php]

    When I run that I am getting this:

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11

    Warning: Illegal offset type in nfl.php on line 11
    WC

    Warning: Invalid argument supplied for foreach() in nfl.php on line 16
    DIV

    Warning: Invalid argument supplied for foreach() in nfl.php on line 16
    CON

    Warning: Invalid argument supplied for foreach() in nfl.php on line 16

    Should I be using the [“DIV”] or [DIV] etc?

    I see that it is loading the xml and reading through it looking for specific [‘gt’] but it’s not liking what it’s finding for some reason…

    in this line:
    [php]$games[$game[‘gt’]][] = $game;[/php]

    Why the blank []?

    Yeah this learning stuff is hard sometimes LOL

    Thanks for helping!

    is $game[‘gt’] set? (try to var dump it or something to see the value)

    with quotes, or it will try to use a constant named DIV

    To insert the game into the $game[‘type’] array. The difference is that without it you will set $game[‘type’] equal to the game, then that variable can hold only one game. Inserting it into $game[‘type’] as an array gives you the possibility to have multiple games per type

    Btw, I messed up the second loop, too early over here ^^

    [php] foreach ($games as $gametype => $subgames) {
    print_r($gametype);
    foreach ($subgames as $game) {
    print_r($game);
    }
    }[/php]

    LOL it was early here too… or should I say very late at night. I still appreciate all the help, it does help me to learn… I’m reading a book right now but before that I had some knowledge just not enough… the loops and foreach get me into trouble…

    DIV used as one of the xml expressions to show a game… not as in Css Div…

    Ahhhhhhhhhhh OK I see :slight_smile: Thank you!

    Honestly still lost… still getting errors and right now it’s bloated but it works… I guess until I can really understand the loop and foreach I’m not going to get it…

    So it’s back to reading online more…

    Add the code you have now and I can try to run it with the xml data and see what’s going on :slight_smile:

    [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);

    echo "<div class='smarticker9'>";
    echo "<ul>";
    

    foreach($xml->gms->g as $games)

    {
    if(!empty($games['vtn']))
    {
     if (($games['gt'])== 'REG') {
     	echo "<li data-subcategory='Regular Season' data-category='Week", $games['w'],"' data-color='1979ab'><a href='#'>",$games['htn']," ",$games['hs'],"  ",$games['vtn'],"  ",$games['vs'],"&nbsp;&nbsp;Final</a></li>";	 	
     }
     if (($games['gt']) =='WC') {
     	echo "<li data-subcategory='Round 1' data-category='Wild Card Games' data-color='1979ab'><a href='#'>",$games['htn']," ",$games['hs'],"  ",$games['vtn'],"  ",$games['vs'],"&nbsp;&nbsp;Final</a></li>";
     		 }
     if (($games['gt']) =='DIV') {
    	echo "<li data-subcategory='Round 2' data-category='Divisional Games' data-color='14ab05'><a href='#'>",$games['htn']," ",$games['hs'],"  ",$games['vtn'],"  ",$games['vs'],"  ",$games['d']," at ",$games['t'], "</a></li>";
     }
     if (($games['gt']) == 'CON') {
    

    echo “

  • ”,$games[‘htn’]," “,$games[‘hs’],” “,$games[‘vtn’],” “,$games[‘vs’],” “,$games[‘d’],” at “,$games[‘t’], “
  • ”;
    }
    if (($games[‘gt’]) == ‘SB’) {
    echo “
  • ”,$games[‘htn’],” “,$games[‘hs’],” “,$games[‘vtn’],” “,$games[‘vs’],” “,$games[‘d’],” at ",$games[‘t’], “
  • ”;
    }
    }
    }
    echo “”;

    [/php]

    That’s the entire script…

    I did this: if(!empty($games[‘vtn’])) so if that score is empty it displays nothing… because there are things in the XML I don’t want to show… there are games that have not been played and games that aren’t really games like the one labled ‘PRO’ isn’t really a game so I don’t want to display that…

    I also did it this way so that I could style each game type individually… so they are all different colors, etc.

    At any rate I REALLY appreciate your help and honestly right now I’m reading about foreach to try to get a better handle on that…

    I am trying to set it up so I don’t have to modify the code in anyway so it’ll work from Regular Season right on through to the SB…

    It’s a pretty cool project so far and I like it and it will be exclusive to my own site!! :slight_smile: BUT if anyone else is interested I would share… no charge because sharing is just awesome!! just can’t share the smarticker part of it because that guy is selling it… :frowning: but I’m sure people can figure out something to display it!! :slight_smile: I do this for fun not for profit…

    I know you guys helping here are doing it to help and you people are really appreciated more then you know for people sincere about learning AND understanding!!

    [php]

    <?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); $games = array('WC', 'DIV', 'CON'); foreach($xml->gms->g as $game) { // it's a single game $games=array($game); // add the game to the games array } foreach ($games as $gametype => $gametypes) { echo $gametype . '
    '; foreach ($gametype as $game) { echo $gamestuff; } } ?>

    [/php]

    I’m sorry I forgot to post this too… it’s the one I’ve been playing with… from your suggestions…

    This is the error I’m getting from this:

    0

    Warning: Invalid argument supplied for foreach() on line 17

    Actually your problem isn’t the loops, it’s not making sure what data set you are working with (common error).

    [hr]

    As you have it now line 13 actually overwrites the $games array for every iteration in the loop. So every time you run through the loop you set $games equal to [$current game] and loose the wc/div/con structure.

    Also when we tried to do $games[$game[‘gt’]][] = $game; we got an “Illegal offset type”. If you tried to var_dump $game[‘gt’] you would get

    object(SimpleXMLElement)#4 (1) { [0]=> string(2) "WC" }

    So what we basically was asking for was to insert that object as a key (similar to WC, DIV and CON) in our array. Which it didn’t really like.

    Simple solution: just cast it to a string, then we have this:

    [php]var_dump((string) $game[‘gt’]);

    // “WC”[/php]

    [hr]

    [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);

    $games = array(
    ‘WC’ => array(),
    ‘DIV’ => array(),
    ‘CON’ => array()
    );

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

    [/php]

    Ok honestly I am more lost now then I was LOL

    I do appreciate you trying but I am not that far ahead enough to even know what to do with any of this…

    [php]
    var_dump((string) $game[‘gt’]);

    $games = array(
    ‘WC’ => array(),
    ‘DIV’ => array(),
    ‘CON’ => array()
    );
    [/php]

    I know what I have is totally bloated but I understand it and can follow it… you know baby steps… but I think I’m trying to run here and not step…

    It’s ok to give up on this LOL I think perhaps I bit off a little more then I can chew … I’m happy it works now, just would like it tidier and by not understanding this I’m making it way harder then it has to be…

    But like I said I have NO idea what to do with that… or what it would even replace… or where it would go… I know me… once I get frustrated my brain stops working :wink:

    The last chunk of code replaces your entire code. I have tried to run it and I think it does what you want it to.

    OH I apologize and I should NEVER look at this stuff while I’m tired I totally missed what you did… I see it now… and yes it works…

    Just a couple of silly questions:
    [php]
    foreach($xml->gms->g as $game) I know what this is…
    { // it’s a single game
    if (!empty($game[‘vtn’])) This I know…

    {
    $games[(string) $game[‘gt’]][] = $game; <- This taking the ‘gt’ element and setting it to a variable correct?
    }

    foreach ($games as $gametype => $subgames) <-- This is where I get lost… not understanding why after using the above it is setting to $subgames… then…

    {
    foreach ($subgames as $game) back into $game… OR is this to load all elements so they can be used?
    [/php]

    Really, Thank you. What you did makes MUCH more sense as far as the use of php and html… MUCH cleaner and easier to make changes to…

    I did make one change so the script works from Week 1 until the SB…

    I will add Pre season and Pro Bowl later when I find out what the ‘gt’ value is…

    Thanks…

    I’ve been meaning to ask you… did you write a book on php?

    Sponsor our Newsletter | Privacy Policy | Terms of Service