Jump to content

help on splitting and grouping array values


blackhawx

Recommended Posts

I'm using smarty and php to collect parameters from a user defined tag in cmsms that looks like this... 

 

{schedule date="2010-11-13" time="7:05 p.m." location="Evansville" home="Oakland City" vs="Evansville" winner="University of Evansville" results="82-42"}

{schedule date="2010-11-15" time="7:00 p.m." location="Oakland City" home="Oakland City" vs="OSU-Lima" winner="OSU-Lima" results="68-67 2OT"}
etc...

 

As you can see each {schedule tag above has the same home="" value.  Thats because its schedule dates just for THAT team (i.e. Oakland City).  So I may have 10 of these tags on the same page.  But I also have other places where the schedule tag is used with a different $home name for another team.  So basically I'm calling ALL these schedule tags into one for loop like this...

 

 

for ($i=0; $i < count($params['date']); $i++) {
    $date= $params['date']; 
    $time=$params['time'];
    $location= $params['location'];
    $home= $params['home'];
    $vs= $params['vs'];
    $winner= $params['winner'];
    $results= $params['results'];
    $gameinfo = array("date", "time","location","home","vs","winner","results");
    $game_info = compact($gameinfo);   

}

 

All I want to do is spit back out the total wins for each $home team.  I am pulling out my hair out trying to get this thing to work but no luck yet, even though I see a little light. This is what I got so far...

 

for ($i=0; $i < count($params['date']); $i++) {
    $date= $params['date']; 
    $time=$params['time'];
    $location= $params['location'];
    $home= $params['home'];
    $vs= $params['vs'];
    $winner= $params['winner'];
    $results= $params['results'];
    $gameinfo = array("date", "time","location","home","vs","winner","results");
    $game_info = compact($gameinfo);   

  if($home == $winner) {
    $wins = array('team' => $winner, 'win' => count($winner));
  }

}

$smarty->append('mwins',$wins);

print '<pre>';
print_r($wins);
print '</pre>';

 

and I get this...

 

Array
(
    [0] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [1] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [2] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [3] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [4] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [5] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [6] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [7] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [8] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [9] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [10] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [11] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [12] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [13] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [14] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [15] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [16] => Array
        (
            [team] => Oakland City
            [win] => 1
        )

    [17] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [18] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [19] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [20] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [21] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [22] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [23] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [24] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [25] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [26] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [27] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [28] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [29] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [30] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [31] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [32] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [33] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [34] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [35] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [36] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

    [37] => Array
        (
            [team] => Southern Indiana
            [win] => 1
        )

etc...

 

I want to take all the [win] values for each [team] and add them up this way my final array looks like this...

 


Array
(
    [0] => Array
        (
            [team] => Oakland City
            [win] => 18
        )

    [1] => Array
        (
            [team] => Southern Indiana
            [win] => 24
        )

 

I know I'm so close but don't know how to solve the condition I need. Any suggestions would be great!!!

 

bh

 

Link to comment
Share on other sites

Try this:

 

$wins = array();
for ($i=0; $i < count($params['date']); $i++) {
    $date= $params['date']; 
    $time=$params['time'];
    $location= $params['location'];
    $home= $params['home'];
    $vs= $params['vs'];
    $winner= $params['winner'];
    $results= $params['results'];
    $gameinfo = array("date", "time","location","home","vs","winner","results");
    $game_info = compact($gameinfo);   

  if($home == $winner) {
    $wins[$winner] += 1;
  }
}

foreach ($wins as $team => $win_count) {
  print "$team won $win_count games\n";
}

Link to comment
Share on other sites

i get 1's listed all the way down with team names like this

 

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

OaklandCity 1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

Southern Indiana University1

 

etc...

 

Link to comment
Share on other sites

Did you change this code inside the loop as well:

 

  if($home == $winner) {
    $wins[$winner] += 1;
  }

 

That's different from the code you posted.

 

Edit: And did you put the loop that prints the results outside the for loop?

Link to comment
Share on other sites

this code is on my UDT (user defined tag)...

for ($i=0; $i < count($params['date']); $i++) {
    $date= $params['date']; 
    $time=$params['time'];
    $location= $params['location'];
    $home= $params['home'];
    $vs= $params['vs'];
    $winner= $params['winner'];
    $results= $params['results'];
    $gameinfo = array("date", "time","location","home","vs","winner","results");
    $game_info = compact($gameinfo);   

if($home == $winner) {
    $record = array('team' => $winner, 'wins' => count($winner));
  }

}
     $smarty->append('mwins',$record);

 

As you can see, I'm using the smarty append tag to merge my array because my {schedule ...} tag gets used multiple times on each page, per team.

 

And on my smarty page I'm simplying testing how the array looks...

 

<div style="display:block; clear:both;">
<pre>
{$mwins|@print_r}
</pre>
</div>

 

 

and I get this...

 

1
Array
(
    [0] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [1] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [2] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [3] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [4] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [5] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [6] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [7] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [8] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [9] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [10] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [11] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [12] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [13] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [14] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [15] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [16] => Array
        (
            [team] => Oakland City
            [wins] => 1
        )

    [17] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [18] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [19] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [20] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [21] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [22] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [23] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [24] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [25] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [26] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [27] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [28] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [29] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [30] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [31] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [32] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [33] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [34] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [35] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [36] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

    [37] => Array
        (
            [team] => Southern Indiana
            [wins] => 1
        )

)
1

 

If I can get my array to load the total win values per team, I'm a winner! haha

 

Link to comment
Share on other sites

You mean how often I call the schedule tag on a page for a specific team? Like this...

 

{schedule date="2010-11-13" time="7:05 p.m." location="Evansville" home="Oakland City" vs="Evansville" winner="University of Evansville" results="82-42"}

{schedule date="2010-11-15" time="7:00 p.m." location="Oakland City" home="Oakland City" vs="OSU-Lima" winner="OSU-Lima" results="68-67 2OT"}

{schedule date="2010-11-18" time="7:00 p.m." location="Logan" home="Oakland City" vs="Logan" winner="Logan" results="70-63"}

{schedule date="2010-11-20" time="3:00 p.m." location="Oakland City" home="Oakland City" vs="OSU-Mansfield" winner="Oakland City" results="124-59"}

{schedule date="2010-11-26" time="7:30 p.m." location="BYU-Hawaii" home="Oakland City" vs="BYU-Hawaii" winner="BYU-Hawaii" results="81-50"}

{schedule date="2010-11-27" time="7:30 p.m." location="BYU-Hawaii" home="Oakland City" vs="BYU-Hawaii" winner="BYU-Hawaii" results="84-56"}

{schedule date="2010-12-02" time="7:00 p.m." location="Oakland City" home="Oakland City" vs="Principia" winner= "Principia" results="86-42"}

{schedule date="2010-12-04" time="3:00 p.m." location="Oakland City" home="Oakland City" vs="Concordia" winner= "Oakland City" results="74-42"}

{schedule date="2010-12-11" time="3:00 p.m." location="Oakland City" home="Oakland City" vs="OU-Lancaster" winner= "Oakland City" results="81-56"}

{schedule date="2010-12-12" time="3:30 p.m." location="Indiana State" home="Oakland City" vs="Indiana State" winner= "Oakland City" results="85-52"}

{schedule date="2010-12-16" time="7:00 p.m." location="St. Louis" home="Oakland City" vs="St. Louis" winner="Oakland City" results="91-38"}

{schedule date="2010-12-18" time="3:00 p.m." location="Oakland City" home="Oakland City" vs="Heritage Baptist" winner= "Oakland City" results="91-38"}

 

 

Link to comment
Share on other sites

Hmm.. so is it like this:  The schedule tag appears multiple times on the page, and each time it appears, your code gets called.  So your code is called once for each schedule tag?

 

Are you able to execute some more php code after all the schedule tags, but before you need to display the total wins per team?

Link to comment
Share on other sites

Yes, every time I call a schedule tag, the udt code gets called each time and appends all the values into a large array called  $game_info.  Then I display $game_info back out into my smarty page with all the schedule values.  I'm just trying to extend this UDT so that I can add up all the wins per $home team, as a separate array.  It is challenging.  But I do thank you very much for the help!

 

I don't have any other code beyond what I have mentioned on his post...

 

Let me say this...if there was a way in smarty that we can sum up the [wins] values where team name ="...", i think that would also be a possible solution...

 

 

 

 

Link to comment
Share on other sites

Yes you can do it in smarty, though it's not really the right tool for the job.  But it's dead simple in php.  If you can get this php code to run after all the schedule tags then you're set:

 

$mwins = $smarty->get_template_vars('mwins');
$agg_wins = array();
foreach ($mwins as $m) {
  $agg_wins[$m['team']] += $m['wins'];
}
$smarty->assign('agg_wins', $agg_wins);

 

Then your smarty code will look like:

 

{foreach from=$agg_wins item=wincount key=teamname}{$teamname} won {$wincount} games<br>{/foreach}

 

If you're allowed you can put the code in smarty's {php} tags, after the {schedule} tags and before the {foreach} that displays the results.

 

BTW this is bad program design, and I would never recommend using {php} tags normally, but I can't suggest any other way without understanding your program's structure better.

Link to comment
Share on other sites

That worked!  I learned something new for me through this post, accessing a variable with get_template_vars, AFTER an array has appened all data.  Never done that before but happy I did now!  In addition to the script you posted I extended a step further.  Not only can you call an associative array with get_template_vars but you can combine associative arrays to make a mega array like this...

 


//here I am calling mtotalgames array and storing it into rtotalgames
$rtotalgames = $smarty->get_template_vars('mtotalgames');

//this is to test out the value...
/*
print '<pre>';
print 'yes! we can call the mtotalgames array like this:';
print_r($rtotalgames);
print '</pre>';
*/


//you can also sort it
sort($rtotalgames);

//btherl's addition...
$agg_total = array();
foreach ($rtotalgames as $n) {
  $agg_total[$n['team']] += $n['loss'];
}
$smarty->assign('agg_total', $agg_total);

//now load another array that has matching keys called mwins...
$rstats = $smarty->get_template_vars('mwins');

/* test it out...
print '<pre>';
print 'yes! we can call the mwins array like this:';
print_r($rstats);
print '</pre>';
*/

sort($rstats);
$agg_wins = array();
foreach ($rstats as $m) {
  $agg_wins[$m['team']] += $m['wins'];
}
$smarty->assign('agg_wins', $agg_wins);


//Now do like voltron and combine them...
$agg_grp = array_merge_recursive($agg_wins,$agg_total);


//Now access parts of the the mega array with array_splice
$array_div2 = array_slice($agg_grp, 0, 4);
$array_div3 = array_slice($agg_grp, 4, 9);
$array_naia = array_slice($agg_grp, 13);


//And send the array portions back to smarty pages...
$smarty->assign('agg_grp', $agg_grp);
$smarty->assign('marray_div2',$array_div2);
$smarty->assign('marray_div3',$array_div3);
$smarty->assign('marray_naia',$array_naia);

 

Thanks again btherl!

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.