1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 04 '15 at 20:29
  • Thank-you, I am very new to php. I want to get the entire site converted to sqli or pdo but I really do not understand enough at the moment. I have been reading into it and can not get my head around it. I have attempted to build the website from scratch in another folder page by page but its not working. Just to keep the current one working I am still using mysql – Joseph Thomas Mallinson Jun 04 '15 at 20:35
  • different table. the results are from openresults and the headers come from openevents – Joseph Thomas Mallinson Jun 04 '15 at 21:18
  • You can stick with mysql - just [switch the mysql_* functions to their mysqli equivalents](http://stackoverflow.com/a/1390625/2403513) :) – Andy Hoffner Jun 04 '15 at 22:55

2 Answers2

0

I would highly recommend normalizing your tables. However, if wish to keep the current table structure that you have, you can use:

SELECT COUNT(ct1) as ct1_count, COUNT(ct2) as ct2_count, COUNT(ct3) as ct3_count FROM openevents;

This will tell you the number of non-empty entries in each column. Then you can simply check to see if each of the columns is 0. If it is, do not display the associated header for your table.

kojow7
  • 10,308
  • 17
  • 80
  • 135
  • 1
    I am just putting code in now to see if it works. Thanks for helping me. – Joseph Thomas Mallinson Jun 04 '15 at 20:59
  • Many thanks for your help, I really appreciate it. Its not working as I want it but I think that maybe because the name of the event is stored in a different database to the one where the results are held? am I allowed to post a webpage link to show you the page and explain what I want to happen? – Joseph Thomas Mallinson Jun 04 '15 at 21:14
  • You said they're stored in a different database, but I assume you mean different table? – kojow7 Jun 04 '15 at 21:29
  • Yes sorry posted on wrong thread. It is in a different table yes not database sorry. – Joseph Thomas Mallinson Jun 04 '15 at 21:36
  • I'm not sure what the rules are about posting links, but since you have most of your trouble code here, I think it is fine. – kojow7 Jun 04 '15 at 21:48
  • www.ilkleymotorclub.org.uk/index.php?p=opencartrialnew is the link. I am wanting the columns between Bala and Total to not be there as there is no title yet as these events have not taken place. – Joseph Thomas Mallinson Jun 04 '15 at 21:56
  • @JosephThomasMallinson I am still pretty sure that my code will work for you. Maybe you didn't try it correctly. – kojow7 Jun 04 '15 at 23:00
  • I also notice in your code that you only check to see if cta is empty, but you don't check to see if ctb, ctc, etc. are empty. Is there a reason for this? – kojow7 Jun 05 '15 at 02:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79768/discussion-between-joseph-thomas-mallinson-and-kojow7). – Joseph Thomas Mallinson Jun 05 '15 at 11:18
0

mysql_num_rows($myVar) will return a number. Your if statement should check the value of the call if(mysql_num_rows($ct_results) == 0)

Kirk Powell
  • 908
  • 9
  • 14
  • This will not do what the OP is wanting. This will only tell if any rows have been returned or not and he is already using this function in his program. The OP is wanting to know which columns are empty not the number of rows returned. – kojow7 Jun 04 '15 at 20:52