Solved

Formatting

Posted on 2009-04-10
2
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to count occurrences of each item in an array.

756 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