Page 1 of 3
brain spasm... formatting a text file into html columns
Posted: Tue Nov 18, 2003 10:38 am
by romeo
got a comman and line delimited file...
want it to read in 2 columns...
the way to do this would be read the entire file and COUNT the lines and then
<table><tr><td>
loop through the first half
</td><td>
loop through the second half
</td></tr>
</table>
Right?
Anyone know what the function to OCUNT the number of lines is?
Im have a brain freeze on simple crap and really pressed for time... thanks
Posted: Tue Nov 18, 2003 10:41 am
by JayBird
try this
Code: Select all
$linecount=count(file("path/to/file.txt"));
Mark
so im looking at this, and stuill having problems :)
Posted: Tue Nov 18, 2003 10:59 am
by romeo
DELETED THIS POST CAUSE IM AN IDIOT... NEW SOURCE STILL SCREWED UP BELOW
Posted: Tue Nov 18, 2003 11:03 am
by JayBird
....and whats that for? and could you please put [syntax=php][/syntax] tags around your code.
Thanks
mark
if yoru willing to help im mor eten willing to put php tags
Posted: Tue Nov 18, 2003 11:07 am
by romeo
DELETED THIS POST CAUSE IM AN IDIOT... NEW SOURCE STILL SCREWED UP BELOW
Posted: Tue Nov 18, 2003 11:09 am
by JayBird
i see you implemented the code i gave you, but what do you want now?
Mark
Posted: Tue Nov 18, 2003 11:12 am
by romeo
DELETED THIS POST CAUSE IM AN IDIOT... NEW SOURCE STILL SCREWED UP BELOW
Posted: Tue Nov 18, 2003 12:07 pm
by m3mn0n
how so? What error messages (if any) are you getting?
Posted: Tue Nov 18, 2003 12:54 pm
by romeo
not getting any, like its stuck in a loop...
hitESC and see that is echoing the first line over and over
Posted: Tue Nov 18, 2003 4:15 pm
by Quietus
You need to increase your counter within your while loop or else it will get stuck, exactly as you have seen.
i cnat believe how bad im screwing this up
Posted: Wed Nov 19, 2003 10:35 am
by romeo
Its looping the 1st line in the text file the entire time before making a new CELL in the table.
Code: Select all
<?php
$filename = 'websuppliers.txt';
$fp = fopen( $filename, 'r' );
$file_contents = fread( $fp, filesize( $filename ) );
fclose( $fp );
$lines = explode ( "\n", $file_contents );
foreach ($lines as $line) {
list( $member, $city, $state ) = explode( '|', $line );
$member = trim($member);
$city = trim($city);
$state = trim($state);
print("<table><tr><td>");
$linecount=count(file("$filename"));
$halfcount=$linecount/2;
$currentcount=0;
while($currentcount<$halfcount){
print("
<table width="250" border="1" cellspacing="1" cellpadding="2" bordercolor="#000000" align="center">
<tr>
<td>
<table width="250" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#ffffff"><font color="#993300" face="Verdana" size="1"><b>$member  </b></font></td>
</tr>
</table>
</td>
</tr>
</table>
");
$currentcount++;
}
print("</td><td>");
// this is the column 2
while($currentcount>$halfcount && $currentcount<$count){
print("
<td valign="top">
<table width="250" border="1" cellspacing="1" cellpadding="2" bordercolor="#000000" align="center">
<tr>
<td>
<table width="250" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#ffffff"><font color="#993300" face="Verdana" size="1"><b>$member  </b></font></td>
</tr>
</table>
</td>
</tr>
</table>
");
$currentcount++;
}
}//end foreach
print("</td></tr></table><br><br>\n");
?>
Posted: Wed Nov 19, 2003 10:46 am
by JayBird
I think the code you have written is flawed from the start. You itterate through each line of the file using th foreach function, but then within this you are using while loops to produce the 2 columns. This just isn't going to work.
I think you need a re-think.
Mark
Posted: Wed Nov 19, 2003 10:51 am
by romeo
thats what im thinking, but im honestly clueless, anyone here have any ideas?
if i did a while loop first and the foreach within, when i got to the second colum I woulndt be able to pick up from where i started...
Posted: Wed Nov 19, 2003 11:28 am
by romeo
ill give you a dollar
Im thinking i nee dto read the entir ething into an array and then split them in 2 and then display them...
Posted: Wed Nov 19, 2003 1:15 pm
by Weirdan
Code: Select all
$file=file($filename);
$file=array_chunk($file,(int)(count($file)/2));
$res=array();
foreach($file[0] as $key=>$line){
$res[$key][0]=$line;
$res[$key][1]=@$file[1][$key];
}