Forum Moderators: open

Message Too Old, No Replies

Count entries from each field

         

lordevil

2:16 am on Jun 27, 2008 (gmt 0)

10+ Year Member



I have no idea where to start on this but I have a database that has lots of tables. Each table has a name of a NFL position, QB, RB, WR, anyways people enter a players name and information. When I am in the database I can see these. What I was hoping to do is on my html page (The index page) I would like it to show how many entries are in each table.
My html page looks like so:
QBs
RBs
WRs
TEs

I would like it to show something like so
QBs (15)
RBs (22)
WRs (9)
TEs (18)

and so on. Thanks to anyone with help.

rocknbil

5:46 pm on Jun 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard lordevil! You can do this with a static SSI page or a PHP page. If you use SSI, a small script can be included. With PHP, you'd do all the work when the page loads. Logic would be as follows:

Using the SSI option, put the following in your .shtml page. It's called an SSI directive and basically means include the output from this file or program in this page at this location:

<!--#include virtual="path_to/summary_script.cgi" -->

Where "summary_script.cgi" is a server-side program that gets the totals and outputs a formated block to be included in the page. It can be in any server side language you're familiar with.

The following is for logic only, roughly what summary_script.cgi would do. I'd do something like this:


$output_div = '<div id="summary"><ul>';
for each $table ('QB','RB','WR','TE') {
$select = "select count(*) from $table";
$cnt = mysql_query_output($select);
$output_div .= "<li>$table ($cnt)</li>\n";
}
$output_div .= '</ul></div>';
print "content-type: text/html\n\n";
print "$output_div";

It will vary based on the programming language you use, but the logic is the same. The SSI (include) calls this program to be included in the page. The program runs, starts a div for your stats, then as you read through the database, add to that div with the results. It finishes by printing out the div, which appears in the space where the SSI directive is. You can remove the bullets on the <li> and style the rest of the block with CSS.

If you do it with PHP, the concept is all still the same, you'd just read in the database at the point in the page where you need it.

lordevil

11:33 pm on Jun 27, 2008 (gmt 0)

10+ Year Member



Thanks for the welcome
How do I point the code to the right database since I have so many? I tried renaming $table to the actual table, I keep getting a parse error.

I didnt use the script, I just put teh code into the html file and changed the html file to a php ending.

rocknbil

6:08 pm on Jun 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do I point the code to the right database since I have so many?

Database, or table? The "code" I posted is not valid code for PHP. it is for example only, My PHP is a bit rusty. :-) It's to show the logic of how you'd loop through your tables. When you loop through the list, "$table" becomes the value for each item in the list.

for each $table ('QB','RB','WR','TE') { ....

So what it should do, if you add echo

for each $table ('QB','RB','WR','TE') {
$select = "select count(*) from $table";
echo "$select <br>\n";

is this:

$select = "select count(*) from QB";
$select = "select count(*) from RB";
$select = "select count(*) from WR";
$select = "select count(*) from TE";

Review loops in PHP for the proper language syntax.