While looping an Array

Hi I’m trying to complete a task where I have produce a table with stats about past super bowl winners and losers. I am retrieving data from a table I have created

Example of data:

1 task is to produce a table to show how many time each team have won.

To do this I wanted to create a loop where I keep adding to an array. I will then output the array into a table


       $aid = 10;
			//below are the exact columns in posts table
        $query = "SELECT * FROM codetable WHERE codeid = 3"; 
		$select_products = mysqli_query($connection, $query);

		while($row = mysqli_fetch_array($select_products)){
				$year = $row['data1'];
				$team_won = $row['data2'];
				$team_lost = $row['data3'];
				$win_points = $row['data4'];
				$lose_points = $row['data5'];
			    $a1[] =  array( $year, $team_won, $team_lost, $win_points, $lose_points );
			    $team_win[] = $team_won;
			    $team_lose[] = $team_lost;
$winners = array_values(array_unique($team_win));  //make winners list unique
$losers = array_values(array_unique($team_lose)); // make losers list unique
$joint = array_merge($team_win, $team_lose);  // merge winners and losers list
$unq_joint = array_values(array_unique($joint)); // make the merged list unique
	$x= 0;
		// Create new array to place distinct values in
		   while($x < count($unq_joint)){
					$a2[] = $unq_joint[$x];

In the array $a2 I’d like to create an array with multiple columns in which I can populate in a while loop. The first column will be the name of the distinct teams, and I’d like the next column to be a count for the how many time a team has won a final.

Is it possible for me to do this.

I’m sure there’s a way you could count the number of times a team name appears in the “data2” column within a short query, rather than the relatively complex code you have. Something like

select team_win, count(team_win) as wins from games group by team_win

If there’s a need to do it with arrays, perhaps you could display the contents of some of the arrays you created to make it a bit clearer.

(Also, surround your source code with a line containing only three back-tick characters to make it easier to read, or I think you can select it and hit the </> button above the text editor. Makes it easier to read here).

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.