Solved

Formatting

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

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 tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

751 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