Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSS in PHP output (fgetcsv) 1

Status
Not open for further replies.

Geronantimo

Technical User
Apr 3, 2006
79
SE
I am successfully using "fgetcsv" to parse data from a flat file database into a webpage.

As I do not really have much knowledge of PHP, I am wondering whether the output can be formatted using CSS.

The database has only two fields and I would like the first field to have different formatting to the second.
Code:
<?php
$row = 1;
$handle = fopen("database.csv", "r");
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
   $num = count($data);
   
   $row++;
   for ($c=0; $c < $num; $c++) {
       echo $data[$c] . "<br />\n";
   }
}
fclose($handle);
?>

If it is even possible to format the output, can somebody point me to a resource where I can learn a little more about this?

.
 
Through print or echo statements you can output any css or html you want. Easiest would be to simply output a span element with applicable style to whichever element you want it applicable to.
 
Hi Vragabond,

Thanks for replying. A span element is precisely what I would like to add before the two rows of data, but I have tried and failed to find the right way to do this based on the code I posted earlier.

As my knowledge of PHP is so limited, I am rather stuck. Could you possibly show me an example, or point me to place where I can read up on this?
 
I'll take a Stab At it.

It ois really just a matter of placing the correct styling before you echo the data. in your piece code you have this:
Code:
...
 for ($c=0; $c < $num; $c++) {
       echo $data[$c] . "<br />\n";
   }
...

amongst other things, allk you would need to do is add some html tags, and css styling to the echo statement like so:

Code:
...
 for ($c=0; $c < $num; $c++) {
       echo [red]"<span style='color:#FFFFF; ...'" . [/red]$data[$c] . "[red]</span>[/red]<br />\n";
   }
...

You could also give the span a class instead of using inline styles.

Code:
...
       echo [red]"<span class='myclass'" . [/red]$data[$c] ...

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks vacunita,

You put me on the right track.

I entered a piece of CSS formatting similar to that which you showed me into the original PHP where there happened to be an empty line! It is as though it had been waiting for the input :)

Here is the code that is now working with the first field of the data formatted green and the second field in the deafult colour:

Code:
<?php
$row = 1;
$handle = fopen("database.csv", "r");
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
   $num = count($data);
[COLOR=red]   echo "<span style=color:#00ff00>";[/color]
   $row++;
   for ($c=0; $c < $num; $c++) {
       echo $data[$c] . "</span><br />\n";
   }
}
fclose($handle);
?>

Many thanks!
 
You're welcome.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top