function buildTableCompetitionsMedia()
{
    #----------------------------------------------------------------------
    #--- CompetitionsMedia table: Create it.
    reportAction("CompetitionsMedia", "Create");
    dbCommand("\n    CREATE TABLE CompetitionsMedia (\n      id                 INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\n      competitionId      VARCHAR(32)      NOT NULL,\n      type               VARCHAR(15)      NOT NULL,\n      text               VARCHAR(100)     NOT NULL,\n      uri                VARCHAR(500)     NOT NULL,\n      submitterName      VARCHAR(50)      NOT NULL,\n      submitterComment   VARCHAR(500)     NOT NULL,\n      submitterEmail     VARCHAR(45)      NOT NULL,\n      timestampSubmitted TIMESTAMP        NOT NULL DEFAULT CURRENT_TIMESTAMP,\n      timestampDecided   TIMESTAMP        NOT NULL,\n      status             VARCHAR(10)      NOT NULL,\n      PRIMARY KEY ( id )\n    )\n ");
    #--- Get the data from the Competitions table.
    $media = dbQuery("\n    SELECT id competitionId, 'report' type, reports data FROM Competitions\n    UNION\n    SELECT id competitionId, 'article' type, articles data FROM Competitions\n    UNION\n    SELECT id competitionId, 'multimedia' type, multimedia data FROM Competitions\n  ");
    #--- Fill the CompetitionsMedia table with the data.
    reportAction("CompetitionsMedia", "Fill with data from table Competitions");
    #  echo "<table>";
    foreach ($media as $data) {
        extract($data);
        # competitionId, type, data
        $competition = getFullCompetitionInfos($competitionId);
        $timestampComp = $competition['year'] . '-' . $competition['month'] . '-' . $competition['day'];
        //noticeBox( true, "One timestamp of comp $competitionId : $timestampComp" );
        preg_match_all('/\\[ \\{ ([^}]+) } \\{ ([^}]+) } ]/x', $data, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            list($all, $text, $uri) = $match;
            #--- Polish the data.
            $text = mysqlEscape($text);
            $uri = mysqlEscape($uri);
            #      echo "<tr><td>";
            #      echo implode( "</td><td>", array( $competitionId, $type, $text, $uri ));
            #      echo "</td></tr>";
            dbCommand("\n        INSERT INTO CompetitionsMedia\n          (competitionId, type, uri, text, submitterComment, submitterEmail, submitterName, timestampSubmitted, timestampDecided, status)\n        VALUES\n          ('{$competitionId}', '{$type}', '{$uri}', '{$text}', '{$comment}', '', '', '{$timestampComp}', '{$timestampComp}', 'accepted')\n      ");
        }
    }
    #  echo "</table>";
}
function showCompetitionInfos () {
#----------------------------------------------------------------------
  global $chosenCompetitionId;

  #--- Get the competition infos from the database.
  $competition = getFullCompetitionInfos( $chosenCompetitionId );
  extract( $competition );
  $delegates = getCompetitionDelegates( $chosenCompetitionId );
  $wcaDelegate = joinUsers( $delegates, true );
  $organizers = getCompetitionOrganizers( $chosenCompetitionId );
  // Only show organizer emails if there is no contact info given.
  $organizer = joinUsers( $organizers, !$contact );

  #--- Show the infos.
  echo "<h1>$name</h1>\n";

  #--- Start the table.
  echo "<div class='table-responsive'>\n";
  echo "<table width='100%' id='competitionDetails'><tr valign='top'>\n";

  #--- Left part.
  echo "<td style='width:70%'><table>";
  $country = getCountry($countryId);
  showItem( 'key', "Date",         array( competitionDate( $competition ), $year ));
  showItem( 'key', "City",         array( $cityName, $country['name'] ));
  showItem( 'key', "Venue",        array( $venue ));
  showItem( 'sub', "Address",      array( $venueAddress ));
  showItem( 'sub', "Details",      array( $venueDetails ));
  showItem( 'key', "Website",      array( $website ));
  showItem( 'key', "Organizer",    array( $organizer ));
  showItem( 'key', "WCA Delegate", array( $wcaDelegate ));
  showItem( 'key', "Contact",      array( $contact ));
  echo "</table></td>";

  #--- Right part.
  echo "<td style='width:30%'><table>";
  showItem( 'key', "Information",  array( $information ));
  showListItemNew( 'View results for', computeCompetitionEvents( $eventSpecs ));
  showListItemNew( 'Reports', computeMedia( 'report' ));
  showListItemNew( 'Articles', computeMedia( 'article' ));
  showListItemNew( 'Multimedia', computeMedia( 'multimedia' ));
  echo "</table></td>";

  #--- End table.
  echo "</tr></table></div>";
}
function showPreregList()
{
    #----------------------------------------------------------------------
    global $chosenCompetitionId;
    if (getBooleanParam('isPreregSubmit')) {
        savePreregForm();
    }
    $eventId = getNormalParam('eventId');
    if ($eventId) {
        showPsychSheet($eventId);
        return;
    }
    echo "<h1>Registered competitors</h1><br />";
    #--- Get the data.
    $preregs = dbQuery("SELECT * FROM Preregs WHERE competitionId = '{$chosenCompetitionId}' AND status='a' ORDER BY countryId, name");
    $competition = getFullCompetitionInfos($chosenCompetitionId);
    #--- Get all events of the competition.
    $eventList = getEventSpecsEventIds($competition['eventSpecs']);
    $headerEvent = "";
    $headerEventLink = "";
    foreach ($eventList as $event) {
        $headerEvent .= "|{$event}";
        $headerEventLink .= "|<a href='c.php?list=1&competitionId={$chosenCompetitionId}&eventId={$event}'>{$event}</a>";
    }
    for ($i = 3; $i < 3 + count($eventList); $i++) {
        $tableStyle[$i] = 'class="c"';
    }
    $tableStyle[3 + count($eventList)] = 'class="f"';
    tableBegin('results', 4 + count($eventList));
    $countPerson = 0;
    $countCountry = 0;
    foreach ($preregs as $prereg) {
        extract($prereg);
        if (!($countPerson % 20)) {
            if ($countPerson) {
                tableHeader(explode('|', "#|Person|Citizen of{$headerEvent}|"), $tableStyle);
            } else {
                if (isset($standAlone)) {
                    tableHeader(explode('|', "#|Person|Citizen of{$headerEvent}|"), $tableStyle);
                } else {
                    tableHeader(explode('|', "#|Person|Citizen of{$headerEventLink}|"), $tableStyle);
                }
            }
        }
        $countPerson += 1;
        #--- Compute the row.
        $row = array($countPerson);
        if ($personId) {
            if (preg_match('/competition_registration.php/', $_SERVER['PHP_SELF'])) {
                $row[] = "<a target='_blank' class='p' href='p.php?i={$personId}'>{$name}</a>";
            } else {
                $row[] = personLink($personId, $name);
            }
        } else {
            $row[] = $name;
        }
        $row[] = $countryId;
        if (!isset($listCountries[$countryId])) {
            $listCountries[$countryId] = 1;
            $countCountry += 1;
        }
        $personEvents = 0;
        $eventIdsList = array_flip(explode(' ', $eventIds));
        foreach ($eventList as $event) {
            if (isset($eventIdsList[$event])) {
                $row[] = 'X';
                $countEvents[$event] = isset($countEvents[$event]) ? $countEvents[$event] + 1 : 1;
                $personEvents += 1;
            } else {
                $row[] = '-';
            }
        }
        $row[] = $personEvents;
        tableRow($row);
    }
    $row = array('', 'Total', $countCountry);
    foreach ($eventList as $event) {
        if (isset($countEvents[$event])) {
            $row[] = $countEvents[$event];
        } else {
            $row[] = 0;
        }
    }
    $row[] = '';
    tableHeader($row, $tableStyle);
    tableEnd();
}
<?php

#----------------------------------------------------------------------
#   Initialization and page contents.
#----------------------------------------------------------------------
$currentSection = 'competitions';
require 'includes/_header.php';
#--- Get the parameters.
analyzeChoices();
#--- Get all competition infos.
$competition = getFullCompetitionInfos($chosenCompetitionId);
#--- If competition not found, say so and stop.
if (!$competition || !$competition['showAtAll']) {
    noticeBox(false, "Unknown competition ID \"{$chosenCompetitionId}\"");
    require 'includes/_footer.php';
    exit(0);
}
#--- Show competition infos.
require 'includes/competition_infos.php';
showCompetitionInfos();
if (wcaDate('Ymd') >= 10000 * $competition['year'] + 100 * $competition['month'] + $competition['day']) {
    #--- Try the cache
    # tryCache( 'competition', $chosenCompetitionId, $chosenByPerson, $chosenAllResults, $chosenTop3, $chosenWinners );
    #--- Show competition results...
    offerChoicesResults();
    require 'includes/competition_results.php';
    if ($chosenByPerson) {
        showCompetitionResultsByPerson();
    } else {
        showCompetitionResults();
    }