How to count columns of a database table and output via PHP?

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
moontear
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sun Feb 11, 2007 10:40 pm

How to count columns of a database table and output via PHP?

Post by moontear » Wed Jan 09, 2008 10:57 pm

Hey there,

beginner question. I count rows like this (and it's working like a charm):

Code: Select all

global $database;
$sql = "SELECT * FROM #__table_2";
$database->setQuery($sql);
$rows = $database->loadObjectList();
$cntrows = count($rows);

echo "<p>There are currently <strong>".$cntrows."</strong> rows</p>


How do i count columns now? My table currently looks like this:
ID
Name
Name2
Name3
Name4

the thing is that the user is able to enter only a name or additional names, so code should take care of that too. The end result should be something like "There are currently x rows with y additional names."

Something like IF column "Name2" NOT NULL count++ AND IF COLUMN "Name3" NOT NULL count++... - I just have no clue how to write that in PHP.

Thanks in advance
moontear

PS: I know there is the "mysql_numfields" function, but I don't know how to implement it with the Joomla special coding (e.g. $database->setQuery($sql); etc)

Post Reply