Solved

Formatting

Posted on 2009-04-10
2
151 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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

11 Experts available now in Live!

Get 1:1 Help Now