HTML on ONE line<

windows

Guest
I am tearing my hair out with this (seemingly) very simple problem. I use this part of PHP as part of a script:

$tab = "<table width=\"".
$twidth."\" cellpadding=\"2\" cellspacing=\"0\" style=\"font-family: ".
$fontfamily.";font-size: " .
$fontsize . "px;font-color: " .
$textcolor . ";border: 1px solid " .
$tableborder . "\">";

echo $tab;

(I wrote the PHP like that so that it is viewed better. In the .php file, however, it is all contained on one line, except the echo $tab; line.

When rendering the .php page, the HTML source shows this:

<table width="200
" cellpadding="2" cellspacing="0" style="font-family: verdana,tahoma,arial
;font-size: 10
px;font-color: black
;border: 1px solid black
">

The script takes values from a file and uses them to create a user defined table. When the HTML is displayed as above, the colors don't work out right.

When, however, I manually fix the HTML to be on one lines, all is fine, but this is not the solution. How can I don it in PHP so that ALL the HTML is written in ONE line, and not like above? :(Looks to me like there's a "\n" at the end of each variable.. If you read them from a file, chances are that you seperate each value with a new line.. When reading the file, you also get the new lines at the end of each value.. That would be my guess. So just try deleting the last char or two (in case it would be \r\n) of each var, and try again. That would be my guess. Future PHP questions go in the PHP forum, by the way. Hope it helps.

substr($var, 0, -1 or -2);umm unless your editor is adding breaks then it should be on one line, but you don't need the ".." stuff


$tab = "<table width=\"
$twidth\" cellpadding=\"2\" cellspacing=\"0\" style=\"font-family:
{$fontfamily};font-size:
{$fontsize}px ;font-color:
{$textcolor}; border: 1px solid
$tableborder \">";

but I don't see how that is easier to read. I just adding the {} so that the semicolons and the px can be next to the variable without causing problems.

but if you don't add \n to anything it should be one line. your editor is problably adding those when you upload them or you are not uploading in ascii mode.@Rydberg - sorry about posting in the wrong section, I'm kind of online on borrowed time :D

BUT I still can't get it to work.

This is how I write the settings to file:

$tableborder = $_POST["tableborder"];
$tableheading = $_POST["tableheading"];
$tablebgcol = $_POST["tablebgcol"];
$txtcol = $_POST["txtcol"];
$fontname = $_POST["fontname"];
$fontsize = $_POST["fontsize"];
$tablewidth = $_POST["tablewidth"];
$numshow = $_POST["numshow"];

$fileopen = fopen("config.cfg","w");

fwrite($fileopen, $tableborder . "\n");
fwrite($fileopen, $tableheading . "\n");
fwrite($fileopen, $tablebgcol . "\n");
fwrite($fileopen, $txtcol . "\n");
fwrite($fileopen, $fontname . "\n");
fwrite($fileopen, $fontsize . "\n");
fwrite($fileopen, $tablewidth . "\n");
fwrite($fileopen, $numshow . "\n");

fclose($fileopen);

And then this is how I read it from the file to create the table page:

$dt = "";

$conf = fopen("config.cfg", "r") or die("Couldn't open file");

$dt = fgets($conf, 1024);
$tableborder = $dt;

$dt = fgets($conf, 1024);
$tableheading = $dt;

$dt = fgets($conf, 1024);
$tablebg = $dt;

$dt = fgets($conf, 1024);
$textcolor = $dt;

$dt = fgets($conf, 1024);
$fontfamily = $dt;

$dt = fgets($conf, 1024);
$fontsize = $dt;

$dt = fgets($conf, 1024);
$twidth = $dt;

$dt = fgets($conf, 1024);
$shownum = $dt;

fclose($conf);

Then I use it in (the same page) like this:

$tab = "<table width=\"$twidth\" cellpadding=\"2\" cellspacing=\"0\" style=\"font-family: $fontfamily ;font-size: $fontsize px ;font-color: $textcolor; border: 1px solid $tableborder \">";

echo $tab;


And then the rest of the table, etc.

Please help.Tested it, it's working. Seems my first guess was correct. Here are the changes made for you:



$dt = fgets($conf, 1024);
$tableborder = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$tableheading = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$tablebg = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$textcolor = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$fontfamily = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$fontsize = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$twidth = substr($dt, 0, -1);

$dt = fgets($conf, 1024);
$shownum = substr($dt, 0, -1);



Mmmm. Alternatively you could read the whole file into a string, and use explode(), with use "\n" as separator.?br />
<!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.explode.php">http://www.php.net/manual/en/function.explode.php</a><!-- m -->

:) Good luck.right thee.

fwrite($fileopen, $tableborder . "\n");

you added the line break. take it outJust taking the "\n" out will result with everything on one line, won't it? Keep it, but optimize the code with explode(), and get all the values in an array.Thanks so much Rydberg, it works perfectly now. I must have messed up the variables when I tried your suggestion the first time.

@scoutt - I DID take it out, but then everything is written on one line and it doesn't work when reading the "variables" out of the config file.

Thanks for your help guys. :)Originally posted by Rydberg
Just taking the "\n" out will result with everything on one line, won't it? Keep it, but optimize the code with explode(), and get all the values in an array.
well yes that is correct. I was a step ahead of myself. good catch. I was thinking something different when I wrote it. you know how it is, think something and say something totally different :P besides that is the excuse I am using and I am sticking with it. :DHehe, no problem, I'm not doubting your abilities to solve this prob. You get tossed between tonnes of different problems in these forums each day, we all slip once in a while.
All for one, one for all :)

I feel unusually on top of my game today, I think I should get the hell out of here and do some coding on my own projects ;)
 
Back
Top