Solved

Formatting

Posted on 2009-04-10
2
154 Views
Last Modified: 2012-05-06
Hi Experts

I have some code that pulls data (table) from a database and spits it out to the browser in a table.  See the code below.

My issue is the table looks a little bit naff.   Is there a way I can use CSS to format the table or output the CSS directly from the PHP?   I like the table way as it's appropriate for my situation where I don't know how many rows I have.

Thanks in advance.


// Execute the query

if ($result = $mysqli->query($query)) {

printf("<table border='1'>");

printf("<tr>");

// Output the headers

$fields = $result->fetch_fields();

foreach ($fields as $field)

printf("<th>%s</th>", $field->name);

printf("</tr>");

// Output the results

while ($table = $result->fetch_row()) {

$field1 = $table[0];

$field2 = $table[1];

$field3 = $table[2];

$field4 = $table[3];

$field5 = $table[4];

$field6 = $table[5];

//$field7 = $table[6];

//$field8 = $table[7];

//$field9 = $table[8];

//$field10 = $table[9];
 

printf("<tr>");

printf("<td>%s</td><td>%s</td>", $field1, $field2);

printf("<td>%s</td><td>%s</td>", $field3, $field4);

printf("<td>%s</td><td>%s</td>", $field5, $field6);

//printf("<td>%s</td><td>%s</td>", $field7, $field8);

//printf("<td>%s</td><td>%s</td>", $field9, $field10);
 

printf("</tr>");

}

printf("</TABLE>");
 

}

Open in new window

0
Comment
Question by:lee_jd
2 Comments
 
LVL 3

Accepted Solution

by:
FirestormX earned 500 total points
ID: 24116081
What kind of formatting do you want for the table?
Borders? Alternating row background colours? etc?

You can use CSS to make the cells look nice. You can set either a class, or set the style in the TD. Here's an example of each from line 24 and 25:
printf("<td style=\"border: solid 1px black;">%s</td><td style=\"border: solid 1px black;">%s</td>", $field1, $field2);

printf("<td class=\"black_border\">%s</td><td class=\"black_border\">%s</td>", $field3, $field4);

Open in new window

0
 
LVL 2

Author Comment

by:lee_jd
ID: 24116474
Hi,

I figured out to add styles via printf.  See the revised code below.  I have it how I want it, however, the table border on the left is missing.   Can you see anything wrong with the code?
// Execute the query

if ($result = $mysqli->query($query)) {
 
 

printf("<style type='text/css'>");

printf("table.sample {");

printf("border-collapse: collapse;");

printf("border-style: dotted dotted dotted dotted;");
 
 
 

printf("}");
 

printf("table.sample th {");

printf("border-width: 1px 1px 1px 1px;");	

printf("padding: 5px 5px 5px 5px;");

printf("border-style: dotted dotted dotted dotted;");
 

printf("}");
 

printf("table.sample td {");

printf("border-style: dotted dotted dotted dotted;");

printf("border-width: 1px 1px 1px 1px;");

printf("}");
 
 

printf("</style>");
 
 
 

printf("<table class='sample' border='1'>");

printf("<tr>");

// Output the headers

$fields = $result->fetch_fields();

foreach ($fields as $field)
 

printf("<th align='left'>%s</th>", $field->name);

printf("</tr>");

// Output the results

while ($table = $result->fetch_row()) {

$field1 = $table[0];

$field2 = $table[1];

$field3 = $table[2];

$field4 = $table[3];

$field5 = $table[4];

$field6 = $table[5];

//$field7 = $table[6];

//$field8 = $table[7];

//$field9 = $table[8];

//$field10 = $table[9];
 

printf("<tr>");

printf("<td>%s</td><td>%s</td>", $field1, $field2);

printf("<td>%s</td><td>%s</td>", $field3, $field4);

printf("<td>%s</td><td>%s</td>", $field5, $field6);

//printf("<td>%s</td><td>%s</td>", $field7, $field8);

//printf("<td>%s</td><td>%s</td>", $field9, $field10);
 

printf("</tr>");

}

printf("</TABLE>");

printf("<p>");
 
 

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now