brain spasm... formatting a text file into html columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

brain spasm... formatting a text file into html columns

Post 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
Last edited by romeo on Tue Nov 18, 2003 10:41 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this

Code: Select all

$linecount=count(file("path/to/file.txt"));
Mark
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

so im looking at this, and stuill having problems :)

Post by romeo »

DELETED THIS POST CAUSE IM AN IDIOT... NEW SOURCE STILL SCREWED UP BELOW
Last edited by romeo on Wed Nov 19, 2003 10:37 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

....and whats that for? and could you please put [syntax=php][/syntax] tags around your code.

Thanks

mark
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

if yoru willing to help im mor eten willing to put php tags

Post by romeo »

DELETED THIS POST CAUSE IM AN IDIOT... NEW SOURCE STILL SCREWED UP BELOW
Last edited by romeo on Wed Nov 19, 2003 10:37 am, edited 2 times in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i see you implemented the code i gave you, but what do you want now?

Mark
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

DELETED THIS POST CAUSE IM AN IDIOT... NEW SOURCE STILL SCREWED UP BELOW
Last edited by romeo on Wed Nov 19, 2003 10:36 am, edited 1 time in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

how so? What error messages (if any) are you getting?
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

not getting any, like its stuck in a loop...

hitESC and see that is echoing the first line over and over
Quietus
Forum Newbie
Posts: 16
Joined: Sat Nov 15, 2003 1:59 am

Post by Quietus »

You need to increase your counter within your while loop or else it will get stuck, exactly as you have seen.
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

i cnat believe how bad im screwing this up

Post 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 &nbsp</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 &nbsp</b></font></td> 
        </tr> 
        

      </table> 
    </td> 
  </tr> 
</table> 
"); 
$currentcount++; 
} 

}//end foreach 
print("</td></tr></table><br><br>\n"); 




?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post 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...
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post 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...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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];
}
Post Reply