- implode: documentation ( source)
<?php
for ($x=1; $x<6; ++$x) {
$unions[] = "SELECT " . ($x==1 ? "$x AS challenge_id, 1 as week_id" : "$x, 1");
$unions[] = "SELECT $x, 6";
}
$derived_table = implode(' UNION ',$unions);
ECHO $derived_table;