Solved

Formatting

Posted on 2009-04-10
2
160 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

623 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