I am trying to make my table 'build' only when data is in the database.
For example, there are 10 events over the year but do not want them to be listed until they have taken place. I also do not want to have to add the line manually by downloading the page and re-uploading it.
Below is my attempt but it will not work. I think I am just putting it the wrong way.
<?php
$ct_results = mysql_query("SELECT *, (ct1 + ct2) AS total FROM resultsopen WHERE ct ='1' ORDER BY total DESC");
$title = mysql_query("SELECT * FROM openevents");
if(mysql_num_rows($ct_results) == '') { echo "<p>No Results Available."; } else {
$title = mysql_fetch_array($title);
echo "<table width=\"1000\" cellpadding=\"5\" cellspacing=\"2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<th>Overall</th>
<th>Competitor</th>
<th>"(!empty($title['cta']) ? "<th>" . $title['cta'] . " : "")</th>
<th>" . $title['ctb'] . "</th>
<th>" . $title['ctc'] . "</th>
<th>" . $title['ctd'] . "</th>
<th>" . $title['cte'] . "</th>
<th>" . $title['ctf'] . "</th>
<th>" . $title['ctg'] . "</th>
<th>" . $title['cth'] . "</th>
<th>Total</th>
</tr>";
//set counter
$counter = 1;
$x=1;
while($results_row = mysql_fetch_array($ct_results))
{
if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#D3D3D3"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $counter . "</td>";
echo "<td>" . $results_row['competitor'] . "</td>";
echo (!empty($results_row['ct1']) ? "<td>" . $results_row['ct1'] . "</td>" : "");
echo (!empty($results_row['ct2']) ? "<td>" . $results_row['ct2'] . "</td>" : "");
echo (!empty($results_row['ct3']) ? "<td>" . $results_row['ct3'] . "</td>" : "");
echo (!empty($results_row['ct4']) ? "<td>" . $results_row['ct4'] . "</td>" : "");
echo (!empty($results_row['ct5']) ? "<td>" . $results_row['ct5'] . "</td>" : "");
echo (!empty($results_row['ct6']) ? "<td>" . $results_row['ct6'] . "</td>" : "");
echo (!empty($results_row['ct7']) ? "<td>" . $results_row['ct7'] . "</td>" : "");
echo (!empty($results_row['ct8']) ? "<td>" . $results_row['ct8'] . "</td>" : "");
echo "<td>" . $results_row['total'] . "</td>";
echo "</tr>";
$counter++; //increment count by 1
$x++;
}
echo "</table>";
}
?>
The bottom part where it echo's is working as I want it to.
The bit I am trying to make work is the Header on top of the table.
There is Overall (permanent), Competitor (permanent), Then event names (appear once its added to database, Total (permanent).
EDIT If there is no event name in the database then I do not want it to show that column. If there is a name in the database to show that column.
<th>Overall</th>
<th>Competitor</th>
<th>"(!empty($title['cta']) ? "<th>" . $title['cta'] . " : "")</th>
<th>" . $title['ctb'] . "</th>
<th>" . $title['ctc'] . "</th>
<th>" . $title['ctd'] . "</th>
<th>" . $title['cte'] . "</th>
<th>" . $title['ctf'] . "</th>
<th>" . $title['ctg'] . "</th>
<th>" . $title['cth'] . "</th>
<th>Total</th>
For example 'cta' in the database will say "Event 1" so it shows Event 1 on the webpage. But 'ctb' has nothing in the database so it just ignores that column until a title is added.