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
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.
Link to comment
Share on other sites

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,
      ),
    )
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?
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.