Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Dynamic Table Data 2

Status
Not open for further replies.

ArtWerk

Programmer
Jul 31, 2006
66
US
I am dynamically creating a table based on an uploaded CSV file, and I'd like allow the user to see this information. My query is simply SELECT * FROM `temp`. How do I go about displaying the content of this table onto the page, since the number of columns could vary?
 
You don't know how many rows will be in the created table? My, that sounds pedagogical...

Anyway, in my code I might:

connect to the database server;
select the appropriate db;
issue the SELECT query;
while (fetching rows from the query into a numerical array)
{
foreach (elements from the array)
{
print the element's value
{
}





Want the best answers? Ask the best questions! TANSTAAFL!
 
it's not so much the problem of not knowing how many rows. That's no problem. The problem is I don't know how many columns are in my dynamically created table. I mean, well i could figure it out when it's created, but I'm trying to view the data in the table well after it's been created.

Since there are functions that find how many rows are in a table, that's not the issue i'm facing.

My table is created based on a CSV file uploaded by the user, so it could contain 5 columns or 25 columns.

So besides recording in some other table the number of columns (or fields) in the uploaded file, i'm wondering if there's a way to find that out (as well as the field names) to help me output this data into a table.
 
an easy way (without a separate sql call) is this
Code:
$result = mysql_query("Select * from `temp`");
while ($result = mysql_fetch_array($result)){
  if (!isset($done)){
     $number_columns = count($row);
     $done = true;
  }
  //process the recordset as usual
}

another way to retrieve column information from a database is
Code:
$result = mysql_query ("Show Columns from table");
$num_cols = mysql_num_rows($result);
 
Would the names of the columns also be somewhere in that query?

like
Code:
column 1 | column 2 | column 3 | column 4
------------------------------------------
row1data | row1data | row1data | row1data
row2data | row2data | row2data | row2data
...
 
column names are available in mysql_fetch_assoc() as the key of each array element.
 
Excellent! Thank you so much!! array_keys() is what I was looking for.

Thanks for all the help.
 
You could use mysql_fetch_field to get the column names. amongst other information.
PHP online Manual said:
mysql_fetch_field

(PHP 3, PHP 4, PHP 5)
mysql_fetch_field -- Get column information from a result and return as an object
Description
object mysql_fetch_field ( resource result [, int field_offset] )

Returns an object containing field information. This function can be used to obtain information about fields in the provided query result.
Parameters

result

The result resource that is being evaluated. This result comes from a call to mysql_query().
field_offset

The numerical field offset. If the field offset is not specified, the next field that was not yet retrieved by this function is retrieved. The field_offset starts at 0.
Return Values

Returns an object containing field information. The properties of the object are:

*

name - column name
*

table - name of the table the column belongs to
*

def - default value of the column
*

max_length - maximum length of the column
*

not_null - 1 if the column cannot be NULL
*

primary_key - 1 if the column is a primary key
*

unique_key - 1 if the column is a unique key
*

multiple_key - 1 if the column is a non-unique key
*

numeric - 1 if the column is numeric
*

blob - 1 if the column is a BLOB
*

type - the type of the column
*

unsigned - 1 if the column is unsigned
*

zerofill - 1 if the column is zero-filled

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top