Jump to content

sort and print array into tables


poe

Recommended Posts

i have an array of teams

[0] => Array
        (
            [id] => 18
            [city] => new jersey
            [divfull] => atlantic
            [conffull] => eastern
)

[1] => Array
        (
            [id] => 28
            [city] => toronto
            [divfull] => northeast
            [conffull] => eastern
)

[2] => Array
        (
            [id] => 4
            [city] => dallas
            [division] => pacific
            [conference] => western
)

etc...

there are 30 teams total
they are divided into 2 conferences (15teams each)
each conference has 3 divisions(5 teams each)

i am trying to get this to printo into 2 columns (1 for each conference, then within each conference print out into each division) at the top of each division print a header row.


[code]
echo '<center>';
echo '<table border=1 cellpadding=1 cellspacing=0>';
echo '<tr><td>';


foreach($confAry as $k => $v) {
list($confabv, $conffull) = split(":", $v);


$hdrow = "<table border=1>";
$hdrow .= "<tr>";
$hdrow .= "<td>team</td>\n";
$hdrow .= "<td>confernece</td>\n";
$hdrow .= "<td>division</td>\n";
$hdrow .= "</tr>\n";
echo $hdrow;

for ($x=0; $x<count($teamArray); $x++) {
if ($confabv == $teamArray[$x][confabv]) {
$tblrow = "<tr>";
$tblrow .= "<td>" . $teamArray[$x][city] . "</td>\n";
$tblrow .= <td>" . $teamArray[$x][conference] . "</td>\n";
$tblrow .= <td>" . $teamArray[$x][division] . "</td>\n";

$tblrow .= "</tr>\n";
echo $tblrow;
}
}
echo "</table>\n";
echo "<td width=20><br></td>\n";
echo "<td>\n";
}
echo '</td></tr>';
echo '</table>';
echo '</center>';
[/code]
Link to comment
https://forums.phpfreaks.com/topic/29368-sort-and-print-array-into-tables/
Share on other sites

I would restructure the table so it matches how you want to display the data.

[code=php:0]$data = array(
  'eastern' => array(
    'atlantic' => array(
      [0] => array(
        [id] => 18,
        [city] => new jersey,
      ),
    )
  'western' => array(
    'pacific' => array(
      [0] => array(
        [id] => 4,
        [city] => dallas,
      ),
  ),
);[/code]


Hmm.. I hope that is clear :)  Then you can just use foreach() loops to iterate through the conferences, then through the divisions, and then through the list of teams.
What kind of format are you looking to construct it in?  You can use something like list($item1,$item2,$item3) = $array.  This will store all of your array variables into different items, not quite sure what kind of reconstructin gyou want to achieve.
i was thinking of taking my format, which my array is constructed like:

Array
        (
            [id] => 28
            [city] => toronto
            [divfull] => northeast
            [conffull] => eastern
)


and manipulating it to:

array(
  'eastern' => array(
    'northeast' => array(
      [0] => array(
        [id] => 28,
        [city] => toronto,
      ),
    )
[quote author=btherl link=topic=117265.msg478291#msg478291 date=1165215896]
I would restructure the table so it matches how you want to display the data.

[code=php:0]$data = array(
  'eastern' => array(
    'atlantic' => array(
      [0] => array(
        [id] => 18,
        [city] => new jersey,
      ),
    )
  'western' => array(
    'pacific' => array(
      [0] => array(
        [id] => 4,
        [city] => dallas,
      ),
  ),
);[/code]


Hmm.. I hope that is clear :)  Then you can just use foreach() loops to iterate through the conferences, then through the divisions, and then through the list of teams.
[/quote]

Yes, do it like twas said above and you should be good.
if i have my arrays structured like:
$data = array(
  'eastern' => array(
    'atlantic' => array(
      [0] => array(
        [id] => 18,
        [city] => new jersey,
      ),
    )
  'western' => array(
    'pacific' => array(
      [0] => array(
        [id] => 4,
        [city] => dallas,
      ),
  ),
);
etc....

what is the best way to 'foreach loop' through the array and print out the list in a:
conference -> division -> team order
originally i have:
[code]
    0  => Array

        (
            [id] => 18
            [city] => new jersey
            [division] => atlantic
            [conference] => eastern
)

[1] => Array
        (
            [id] => 28
            [city] => toronto
            [division] => northeast
            [conference] => eastern
)

[2] => Array
        (
            [id] => 4
            [city] => dallas
            [division] => pacific
            [conference] => western
)
[/code]

then i was advised to change the structure on my array to:
[code]
$data = array(
  'eastern' => array(
    'atlantic' => array(
      [0] => array(
        [id] => 18,
        [city] => new jersey,
      ),
    )
  'western' => array(
    'pacific' => array(
      [0] => array(
        [id] => 4,
        [city] => dallas,
      ),
  ),
);
[/code]

which is what i did, now i am still stuck trying to get this to print out the way i want:
there are 30 teams total
they are divided into 2 conferences (15teams each)
each conference has 3 divisions(5 teams each)

i am trying to get this to print into 2 columns (1 for each conference, then within each conference print out into each division) at the top of each division print a header row.

like 'conference - division':

[table]
[tr][td]eastern - atlantic[/td][td] [/td][td]western - central[/td][/tr]
[tr][td]New Jersey[/td][td] [/td][td]Chicago[/td][/tr]
[tr][td]New York[/td][td] [/td][td]Columbus[/td][/tr]
[tr][td]New York[/td][td] [/td][td]Detroit[/td][/tr]
[tr][td]Philadelphia[/td][td] [/td][td]Nashville[/td][/tr]
[tr][td]Pittsburgh[/td][td] [/td][td]St. Louis[/td][/tr]
[tr][td]----[/td][td]----[/td][td]----[/td][/tr]
[tr][td]eastern - northeast[/td][td] [/td][td]western - northwest[/td][/tr]
[tr][td]Boston[/td][td] [/td][td]Calgary[/td][/tr]
[tr][td]Buffalo[/td][td] [/td][td]Colorado[/td][/tr]
[tr][td]Montreal[/td][td] [/td][td]Edmonton[/td][/tr]
[tr][td]Ottawa[/td][td] [/td][td]Minnesota[/td][/tr]
[tr][td]Toronto[/td][td] [/td][td]Vancouver[/td][/tr]
[tr][td]----[/td][td]----[/td][td]----[/td][/tr]
[tr][td]eastern - southeast[/td][td] [/td][td]western - pacific[/td][/tr]
[tr][td]Atlanta[/td][td] [/td][td]Anaheim[/td][/tr]
[tr][td]Carolina[/td][td] [/td][td]Dallas[/td][/tr]
[tr][td]Florida[/td][td] [/td][td]Los Angeles[/td][/tr]
[tr][td]Tampa Bay[/td][td] [/td][td]Phoenix[/td][/tr]
[tr][td]Washington[/td][td] [/td][td]San Jose[/td][/tr]
[/table]

any ideas on how to take my array and put it into a table format as shown above?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.