gregs
March 18, 2011, 2:16am
1
$pquery = <<<END
SELECT DISTINCT
ws_teams.id, ws_teams.state, ws_teams.sport,
ws_teams.school, ws_teams.mascot,
ws_sport.id, ws_sport.sport,
ws_state.id, ws_state.state
FROM ws_teams
INNER
JOIN ws_state
ON ws_state.id = ws_teams.state
INNER
JOIN ws_sport
ON ws_sport.id = ws_teams.sport
END;
$pquery .= "WHERE ws_teams.sport = ".$sport." ";
if ( $year != "ALL" ) { $pquery .= "AND ws_teams.year = ".$year." "; }
if ( $state != "ALL" ) { $pquery .= "AND ws_teams.state = ".$state." "; }
$pquery .="ORDER BY ws_state.state";
$query = doquery($pquery, "teams");
while ($result = mysql_fetch_array($query)) {
$page .= "<tr class=\\"alt\\"><td>".$result['state']." ".$result['school']." ".$result['mascot']."</td></tr>\
";
}
if (mysql_num_rows($query) == 0) { $page .= "<tr class=\\"alt\\"><td>No teams found.</td></tr>\
"; }
I have no idea how to use a WHILE or FOREACH to pick $RESULT apart and print it out as needed. Right now, it just prints out:
Kentucky Oak Ridge Timbers
Kentucky Valley View Chargers
...
Kentucky Mountainburg Rams
Apparently the data I want is in the array, I just dont know how to print it in this fashion
Kentucky
Oak Ridge Timbers
Valley View Chargers
...
Mountainburg Rams
Virginia
Gettysburg Minutemen
...
Any help would be appreciated.
gregs
March 18, 2011, 3:43am
2
I don’t understand how arrays work, I guess.
I use:
$result = mysql_fetch_array($query);
PRINT_R($result);
and it prints out this stuff.
Array ( [0] => 1 [id] => 4 [1] => 4 [state] => Arkansas [2] => 1 [sport] => Sr Football [3] => Bentonville [school] => Bentonville [4] => Tigers [mascot] => Tigers [5] => 1 [6] => Sr Football [7] => 4 [8] => Arkansas )
Is that the ONLY thing in that array? It can’t be. If I use the above post, it prints out the sixteen I know are in it. This is confusing.
It is the only thing in that array - because in your 1st post, the while statement creates a new array for each line returned from your sql query. If you’re not too familiar with php/mysql, take a look at MySQL Tutorial - Fetch Array - it’s a good website for beginners (I used it some years ago when I was getting started)
To get a result like you’re looking for in the first post, I would use something like this:
$state = "";
while ($result = mysql_fetch_array($query)) {
if($result['state'] !== $state)
{
$page .= "<tr class=\\"alt\\"><td>".$result['state']."</td>";
}
$page .= "<tr class=\\"alt\\"><td>".$result['school']." ".$result['mascot']."</td></tr>\
";
$state = $result['state'];
}
You may need to tweek the formatting to get it 100% the way you want it. Basically this will check if the current row’s state is the same as the last one, and if it is, it won’t re-print the state name.