je ziet overal in de voetbalbladen van die mooie draaitabellen, wie weet een script om van dit

datum | gespeeld |thuis |uit |score
zo 23-09-2012 12:30 Y 06 ADO AJA 1-1
za 10-11-2012 19:45 Y 12 ADO AZA 2-2
zo 05-05-2013 12:30 N 33 ADO FEY 0-0
zo 12-08-2012 16:30 Y 01 AJA AZA 2-2
zo 20-01-2013 14:30 Y 19 AJA FEY 3-0
zo 24-02-2013 14:30 Y 24 AJA ADO 1-1
zo 25-11-2012 14:30 Y 14 AZA FEY 0-2
za 09-03-2013 20:45 Y 26 AZA ADO 1-1
zo 17-03-2013 16:30 N 27 AZA AJA 0-0
zo 28-10-2012 12:30 Y 10 FEY AJA 2-2
zo 16-12-2012 14:30 Y 17 FEY ADO 3-2
zo 10-02-2013 14:30 Y 22 FEY AZA 3-1


een draaitabel /pivot tabel te maken zoals dit:

xxx|ado|aja|aza|fey|
___|___|___|___|___|_
ado| X |1-1|2-2| - |
___|___|___|___|___|_
aja|1-1| X |2-2|2-0|
___|___|___|___|___|_
aza|1-1| - | X |0-2|
___|___|___|___|___|_
fey|3-2|2-2|3-1| X |
___|___|___|___|___|_

Ik heb al lopen zoek maar kan het helaas geen gode oplossng vinden

Misschien moet ik iets met een array doen, waarmee ik per thuisteam een regel maak. Maar ik kom er dus niet uit :-(
Ger

Dat met de $pdo ($pdo = new PDO(....)) snap ik even niet. zou je dat wat meer kunnen uitleggen=


De seizoen_id (109) staat inderdaad niet in de teams, want teams kunnen namelijk in meerdere seizoenen aanwezig zijn, maar in de cs_wedstrijden want een wedstrijd zal altijd in één seizoen voorkomen.
<?php
$pdo = new PDO('mysql:host=localhost;dbname=xxxxxx;charset=utf8', 'wortel', 'willie');
?>

Ik begrijp dat je het seizoen niet in de teams heb staan, maar hoe bepaal je dan de indeling per seizoen?.
Zoals gezegd, ik zelf heb daar een tabel voor gemaakt (teams_by_season).
Ger

de indeling per siezoen wordt bepaald door de volgende gegevens

cs_wedstrijden_id (oplopend uniek volgnummer, heeft denk ik verder geen verklaring nodig)
cs_wedstrijd_gespeeld (Y of N om te bepalen al gespeeld is of nog gespeeld moet worden)

per seizoen zijn er 306 wedstrijd (18 teams die tegen elkaar spelen behalbve tegen zichzelf)

cs_season_id ((geeft aan in welk seizoen de wedstrijd gespeeld is)
cs_wedstrijden_speelronde (geeft aan welk in speelronde de onderlinge ontmoeting was)

cs_wedstrijden_datum (speekt voor zich)
cs_team_id_thuis (bevat de ID van de teams uit de table teams)
cs_team_id_uit (bevat de ID van de teams uit de table teams)
cs_doelthuis (speekt voor zich)
cs_doeluit(speekt voor zich)

dit is wat ik nu als resultaat krijg

cs_wedstrijden_id mDate team1_id team1_name team2_id team2_name uitslag
1352 30/11 3 ado 4 aja 30/11
1288 27/09 3 ado 5 aza 2 - 3
1386 17/01 3 ado 8 cam 17/01
1478 22/03 4 aja 3 ado 22/03
1418 05/02 4 aja 5 aza 05/02
1523 10/05 4 aja 8 cam 10/05
1501 18/04 5 aza 3 ado 18/04
1244 17/08 5 aza 4 aja 1 - 3
1477 21/03 5 aza 8 cam 21/03
1258 30/08 8 cam 3 ado 3 - 2
1333 09/11 8 cam 4 aja 09/11
1351 29/11 8 cam 5 aza 29/11


ado aja aza cam
ado X 30/11 2-3 17/01
aja 22/03 X 05/02 10/05
aza 18/04 1-3 X 21/03
cam 3-2 09/11 29/11 X

Nb.

Ik heb nu even een beperkte zet data geselecteerd
Nou.. binnen een uurtje :-)


<?php
// even een array zoals die uit de database zou kunnen komen om te testen
$records = array(
	array(
		'cs_wedstrijden_id' => 1,
		'mDate' => '1-10-2014',
		'team1_id' => 1,
		'team1_name' => 'FC Knudde',
		'team2_id' => 2,
		'team2_name' => 'FC Losers',
		'uitslag' => '1-2',
	),
	array(
		'cs_wedstrijden_id' => 2,
		'mDate' => '2-10-2014',
		'team1_id' => 3,
		'team1_name' => 'Ajax',
		'team2_id' => 4,
		'team2_name' => 'Feyenoord',
		'uitslag' => '2-3',
	),
	array(
		'cs_wedstrijden_id' => 3,
		'mDate' => '3-10-2014',
		'team1_id' => 1,
		'team1_name' => 'FC Knudde',
		'team2_id' => 4,
		'team2_name' => 'Feyenoord',
		'uitslag' => '3-4',
	),
	array(
		'cs_wedstrijden_id' => 4,
		'mDate' => '4-10-2014',
		'team1_id' => 3,
		'team1_name' => 'Ajax',
		'team2_id' => 2,
		'team2_name' => 'FC Losers',
		'uitslag' => '5-6',
	),
);

class kruistabel
{
	private $records;
	private $teams;
	private $table;
	
	// constructor, wordt automatisch uitgevoerd.
	function __construct($records)
	{
		$this->records = $records;
		$this->makeTeamsArray();
		$this->makeTableArray();
	}
	
	// verkrijg een array met de namen van de teams
	public function getTeamNames()
	{
		return array_values($this->teams);
	}
	
	// verkrijg de complete kruistabel
	public function renderTable()
	{
		$html = '<table>';
		
		foreach($this->table as $row)
		{
			$html .= '<tr>';
			
			foreach($row as $field)
			{
				if(is_int($field))
					$html .= '<td>' . $this->records[$field]['uitslag'] . '</td>';
				else
					$html .= '<td>'.$field.'</td>';
			}
			
			$html .= '</tr>';
		}
			
		return $html . '</table>';
	}

	// filter alle teams uit de wedstrijden en stop ze in een array
	// hierbij komt elk team maar één keer in de array
	private function makeTeamsArray()
	{
		$this->teams = array();
		
		foreach($this->records as $match)
		{
			echo $match['team1_id'] . '-' . $match['team2_id'] . '-';
			$this->teams[$match['team1_id']] = $match['team1_name'];
			$this->teams[$match['team2_id']] = $match['team2_name'];
		}
		
		// sorteer de teams op alphabetische volgorde (optioneel)
		asort($this->teams);
		
		// echo '<pre>'; print_r($this->teams); echo '</pre>';
	}
	
	// maak een 2D array met de inhoud voor de tabel
	private function makeTableArray()
	{
		$this->table = array();
		$this->table[] = array_merge(array(''), $this->teams);

		foreach($this->teams as $id1 => $team1)
		{
			$row = array();
			
			$row[] = $team1;
			
			foreach($this->teams as $id2 => $team2)
			{
				if($id1 == $id2)
					$row[] = 'X';
				else
					$row[] = $this->findMatch($id1, $id2);
			}
			
			$this->table[] = $row;
		}
		
		// echo '<pre>'; print_r($this->table); echo '</pre>';		
	}
	
	// deze functie zoekt naar wedstrijden tussen team A en team B
	// hierbij maakt het niet uit of de volgorde A-B of B-A is
	private function findMatch($id1, $id2)
	{
		foreach($this->records as $key => $match)
		{
			if(($match['team1_id'] == $id1 && $match['team2_id'] == $id2) || ($match['team1_id'] == $id2 && $match['team2_id'] == $id1))
			{
				return $key;
			}
		}

		return NULL;
	}
}

$kruistabel = new kruistabel($records);
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Untitled Document</title>
		<style>
			table {
				border-collapse:collapse;
			}
			table td, table th {
				border:1px solid #CCC;
				text-align:center;
			}
        </style>
    </head>
    
    <body>
        <h2>Teams:</h2>
    	<ul>
        <?php
			foreach($kruistabel->getTeamNames() as $name)
				echo '<li>'.$name.'</li>';
		?>
        </ul>
        <h2>Kruistabel:</h2>
		<?php echo $kruistabel->renderTable(); ?>
    </body>
</html>

HELEMAAL TOP!!

nu moet ik alleen nog het resultaat van mij query naar de Array

<?php
SELECT
m1.cs_wedstrijden_id,
date_format(m1.cs_wedstrijden_datum, '%d/%m') as mDate,
m1.cs_team_id_thuis AS team1_id,
t1.TeamName_Short AS team1_name,
m1.cs_team_id_uit AS team2_id,
t2.TeamName_Short AS team2_name,
CASE m1.cs_wedstrijd_gespeeld
WHEN 'Y' THEN CONCAT(m1.cs_doelthuis, ' - ', m1.cs_doeluit)
WHEN 'N' THEN date_format(m1.cs_wedstrijden_datum, '%d/%m')
ELSE 'X' END uitslag
FROM
cs_wedstrijden m1, af_teams t1, af_teams t2
WHERE
cs_season_id =110
AND
t1.Team_ID = cs_team_id_thuis
AND
t2.Team_ID = cs_team_id_uit
ORDER by t1.TeamName_Short , t2.TeamName_Short
?>
<?php
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
:-(

dat van die PDO snap ik even niet

Met onderstaand code krijg ik nu de errors

PHP Notice: Undefined variable: kruistabel in new1.php on line 153
PHP Fatal error: Call to a member function renderTable() on a non-object in new1.php on line 153

<?php


$pdo = new PDO("mysql:host=localhost;dbname=database;charset=utf8;user='ikke';password='blabla'");

$stmt = $pdo->query("SELECT
m1.cs_wedstrijden_id,
date_format(m1.cs_wedstrijden_datum, '%d/%m') as mDate,
m1.cs_team_id_thuis AS team1_id,
t1.TeamName_Short AS team1_name,
m1.cs_team_id_uit AS team2_id,
t2.TeamName_Short AS team2_name,
CASE m1.cs_wedstrijd_gespeeld
WHEN 'Y' THEN CONCAT(m1.cs_doelthuis, ' - ', m1.cs_doeluit)
WHEN 'N' THEN date_format(m1.cs_wedstrijden_datum, '%d/%m')
ELSE 'X' END uitslag
FROM
cs_wedstrijden m1, af_teams t1, af_teams t2
WHERE
cs_season_id =110
AND
t1.Team_ID = cs_team_id_thuis
AND
t2.Team_ID = cs_team_id_uit
ORDER by t1.TeamName_Short , t2.TeamName_Short") or die(mysql_error());

while ($records = $stmt->fetchAll(PDO::FETCH_ASSOC))
{
class kruistabel
{
private $records;
private $teams;
private $table;

// constructor, wordt automatisch uitgevoerd.
function __construct($records)
{
$this->records = $records;
$this->makeTeamsArray();
$this->makeTableArray();
}

// verkrijg een array met de namen van de teams
public function getTeamNames()
{
return array_values($this->teams);
}

// verkrijg de complete kruistabel
public function renderTable()
{
$html = '<table>';

foreach($this->table as $row)
{
$html .= '<tr>';

foreach($row as $field)
{
if(is_int($field))
$html .= '<td>' . $this->records[$field]['uitslag'] . '</td>';
else
$html .= '<td>'.$field.'</td>';
}

$html .= '</tr>';
}

return $html . '</table>';
}

// filter alle teams uit de wedstrijden en stop ze in een array
// hierbij komt elk team maar één keer in de array
private function makeTeamsArray()
{
$this->teams = array();

foreach($this->records as $match)
{
echo $match['team1_id'] . '-' . $match['team2_id'] . '-';
$this->teams[$match['team1_id']] = $match['team1_name'];
$this->teams[$match['team2_id']] = $match['team2_name'];
}

// sorteer de teams op alphabetische volgorde (optioneel)
asort($this->teams);

// echo '<pre>'; print_r($this->teams); echo '</pre>';
}

// maak een 2D array met de inhoud voor de tabel
private function makeTableArray()
{
$this->table = array();
$this->table[] = array_merge(array(''), $this->teams);

foreach($this->teams as $id1 => $team1)
{
$row = array();

$row[] = $team1;

foreach($this->teams as $id2 => $team2)
{
if($id1 == $id2)
$row[] = 'X';
else
$row[] = $this->findMatch($id1, $id2);
}

$this->table[] = $row;
}

}

// deze functie zoekt naar wedstrijden tussen team A en team B
// hierbij maakt het niet uit of de volgorde A-B of B-A is
private function findMatch($id1, $id2)
{
foreach($this->records as $key => $match)
{
if(($match['team1_id'] == $id1 && $match['team2_id'] == $id2) || ($match['team1_id'] == $id2 && $match['team2_id'] == $id1))
{
return $key;
}
}
return NULL;
}
}

$kruistabel = new kruistabel($records);

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>kruistable</title>
<style>
table {
border-collapse:collapse;
}
table td, table th {
border:1px solid #CCC;
text-align:center;
}
</style>
</head>

<body>
<?php echo $kruistabel->renderTable(); ?>
</body>
</html>
<?php
}
?>
een fetchAll hoeft niet in een while lus. deze pakt in één keer alle records :-)

<?php


class kruistabel
{
// HIER DIE CLASS VAN MIJ
}

$pdo = new PDO("mysql:host=localhost;dbname=adostatsTEST;charset=utf8;user='ikke';password='blabla'");

$stmt = $pdo->query("HIER DIE MOOIE QUERY VAN JOU :-)") or die(mysql_error());
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);

$kruistabel = new kruistabel($records);
echo $kruistabel->renderTable();
?>
YEP!!!Dat is hem!!

hartstikke bedankt!!

Reageren