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

Post by romeo »

can you ellabroate a little, im having trouble reading that
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);  //read the file contents into $file array
$file=array_chunk($file,ceil(count($file)/2));  //split the array into chunks.
//each chunk should be [i]ceil(count($file)/2)[/i] in size.
//count($file) gives you the count of the lines in file
//count($file)/2 is half (you want to split text in two cols, right?)
//count($file) could be odd, so ceil function is used to round the quotient up to the next integer
$res=array(); 
foreach($file[0] as $key=>$line){ 
  $res[$key][0]=$line; 
  $res[$key][1]=@$file[1][$key]; //if the count($file) is odd the
 // second column will have ceil(count($file)/2)-1 rows, so errors are 
//supressed.
}
//after the loop we have the array with two columns.

echo "<table>";
foreach($result as $row)
   echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td></tr>";
echo "</table>";
// the above piece of code will display the table with two columns
[php_man]count[/php_man]
[php_man]ceil[/php_man]
[php_man]array_chunk[/php_man]
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

okay, each line in the file has 3 values, | delimited, would i explode that in the foreach?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

If you want the table to be 1 row in size you can modify the above code:

Code: Select all

$file=file($filename);  //read the file contents into $file array 
$file=array_chunk($file,ceil(count($file)/2));  //split the array into chunks.
//each chunk should be ceil(count($file)/2) in size. 
//count($file) gives you the count of the lines in file 
//count($file)/2 is half (you want to split text in two cols, right?) 
//count($file) could be odd, so ceil function is used to round the quotient up to the next integer
echo "<table><tr><td>";
echo implode("\n",$file[0]); //first column
echo "</td><td>";
echo implode("\n",$file[1]); //second column
echo "</td></tr></table>";
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

after the line:
$file=array_chunk($file,ceil(count($file)/2));
is executed you have the two-element array with the first half of the file in $file[0] and the second one in $file[1]. It's up to you to decide what you want to do with it. ;)
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

im sorry man, I am easily confused...

the file is a CSV, i want to display 1 or 2 of thsoe values in each line... your saying take file[0] and exlplode it at the /n and then into value (at the |) right?
(wanna make sur ethsi is right ebfor ei kill myself :)
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

so i tried...

Code: Select all

<?php

$filename = 'websuppliers.txt'; 
$file=file($filename);  //read the file contents into $file array 
$file=array_chunk($file,ceil(count($file)/2));  //split the array into chunks. 
//each chunk should be ceil(count($file)/2) in size. 
//count($file) gives you the count of the lines in file 
//count($file)/2 is half (you want to split text in two cols, right?) 
//count($file) could be odd, so ceil function is used to round the quotient up to the next integer 
$res=array(); 
foreach($file[0] as $key=>$line){ 
  $res[$key][0]=$line; 
  $res[$key][1]=@$file[1][$key]; //if the count($file) is odd the 
// second column will have ceil(count($file)/2)-1 rows, so errors are 
//supressed. 
} 
//after the loop we have the array with two columns. 

echo "<table><tr><td>"; 
foreach($result as $row) {

$lines = explode ( "\n", $row[0] );
list( $member, $city, $state ) = explode( '|', $line ); 
$member = trim($member); 
$city = trim($city); 
$state = trim($state); 

echo "$member<br>";
echo "</td><td>";

$lines1 = explode ( "\n", $row[1] );
list( $member1, $city1, $state1 ) = explode( '|', $line1 ); 
$member1 = trim($member); 
$city1 = trim($city); 
$state1 = trim($state); 

echo "$member1<br>";



}
echo "</td><tr></table>";

?>
and I am getting this error...

Fatal error: Call to undefined function: array_chunk() in /home/httpd/vhosts/localsupport.com/httpdocs/vpcpages/sup_listing2.php on line 437
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

PHP manual wrote: array_chunk
Split an array into chunks (PHP 4 >= 4.2.0)
If you have ancient PHP you may try a replacement:

Code: Select all

function array_chunk ($a, $s, $p=false) {
  $r = Array();
  $ak = array_keys($a);
  $i = 0;
  $sc = 0;
  for ($x=0;$x<count($ak);$x++) {
     if ($i == $s){$i = 0;$sc++;}
     $k = ($p) ? $ak[$x] : $i;
     $r[$sc][$k] = $a[$ak[$x]];
     $i++;
  }
  return $r;
}
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

S.O.B

Post by romeo »

Thank you for all yoru help.. but its still not wokring...

I will seriously paypal you a dollar is you fix it :)

Code: Select all

<?php
function array_chunk ($a, $s, $p=false) { 
  $r = Array(); 
  $ak = array_keys($a); 
  $i = 0; 
  $sc = 0; 
  for ($x=0;$x<count($ak);$x++) { 
     if ($i == $s){$i = 0;$sc++;} 
     $k = ($p) ? $ak[$x] : $i; 
     $r[$sc][$k] = $a[$ak[$x]]; 
     $i++; 
  } 
  return $r; 
} 





$filename = 'websuppliers.txt'; 
$file=file($filename);  //read the file contents into $file array 
$file=array_chunk($file,ceil(count($file)/2));  //split the array into chunks. 
//each chunk should be ceil(count($file)/2) in size. 
//count($file) gives you the count of the lines in file 
//count($file)/2 is half (you want to split text in two cols, right?) 
//count($file) could be odd, so ceil function is used to round the quotient up to the next integer 
$res=array(); 
foreach($file[0] as $key=>$line){ 
  $res[$key][0]=$line; 
  $res[$key][1]=@$file[1][$key]; //if the count($file) is odd the 
// second column will have ceil(count($file)/2)-1 rows, so errors are 
//supressed. 
} 
//after the loop we have the array with two columns. 

echo "<table><tr><td>"; 
foreach($result as $row) {

$lines = explode ( "\n", $row[0] );
list( $member, $city, $state ) = explode( '|', $lines ); 
$member = trim($member); 
$city = trim($city); 
$state = trim($state); 

echo "$member<br>";
echo "</td><td>";

$lines1 = explode ( "\n", $row[1] );
list( $member1, $city1, $state1 ) = explode( '|', $lines1 ); 
$member1 = trim($member); 
$city1 = trim($city); 
$state1 = trim($state); 

echo "$member1<br>";



}
echo "</td><tr></table>";

?>

now the probelm is in the FOREACH...

cant do that to an array?
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

bumpo
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

show me sample of your file contents and what it should look like
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

i luv u man

Post by romeo »

I have a text file that contains 3 values seperated by | on each line...

member|address|city


or

JIMS COMPUTERS|PITTsBURGh|PA


I currently have them being sorted in 2 columns...

but it displays them from left to right and then down
aa ab
ac ad
ba bb
bc bd


I need it to go from top to bottom then then left to right
like...

aa ba
ab bb
ac bc
ad bd

in the same 2 columns...

Im pulling out my hair as this seems simple.. but ive screwed it up twice...

Here is my working code.. can someone please help me modifiy it or does nayone have a better solution?


Code: Select all

<?php


//you need this - it tells the data what collumn to go to    
$count = 1;  
$column = 1;  

//db connect and select 

$filename = 'websuppliers.txt';  
$fp = fopen( $filename, 'r' );  
$file_contents = fread( $fp, filesize( $filename ) );  
fclose( $fp );  
$lines = explode ( "\n", $file_contents ); 

print("<table width=500>"); 
print("<tr><td>"); 

//loop statement 
foreach ($lines as $line) {  
list( $member, $city, $state ) = explode( '|', $line );  
$member = trim($member);  
$city = trim($city);  
$state = trim($state);  


$linecount=count(file("$filename"));  
$halfcount=$linecount/2;  
$currentcount=0;  



// this is the column 1 
if ($column == 1)  
{  

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> 
");  
}  

print("</td><td>"); 

if($column != 1)  
{   
//this  is the column 2    
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> 
");  


}  
//increment the count 
$count += 1;  
// this is a modulus operator it gets the remainder of the equation 
$column = $count % 2; 



if($currentcount <= $halfcount) { $column == 1; } else { $column == 2; } 


}  
print("</td></tr>"); 
print("</table>"); 



?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

ok, suppose you have in file:

Code: Select all

a member|a address|a city
b member|b address|b city
c member|c address|c city
d member|d address|d city
Do you want this records to appear on your page as:

Code: Select all

+--------+---------+------+--------+---------+------+
|a member|a address|a city|c member|c address|c city|
+--------+---------+------+--------+---------+------+
|b member|b address|b city|d member|d address|d city|
+--------+---------+------+--------+---------+------+
?
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

2 columns

yes

a e
b f
c g
d h
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

//----customize this section
$filename='websuppliers.txt';
$columns=2; // do not set $columns to numbers greater than 2. It requires different algo.
//----stop customizing

$file=file($filename);
$per_column=ceil(count($file)/$columns); //calculate the number of rows
$i=0; //counter
$j=0; //second one

$result=array();

foreach($file as $line){
   if($i>=$per_column) { $i=0; $j++; } //start another column
   $result[$i][$j]=$line;
   $i++;
}

$sub_field_count=count(explode("|",$result[0][0])); //the number of fields in your `csv` is counted by the first row.

echo "<table>\n";
foreach($result as $row){
    echo "<tr>";
    for($j=0;$j<$columns;$j++){            //to mantain proper html table structure
        $sub_row=(empty($row[$j])?array():explode("|",$row[$j]));
        for($i=0;$i<$sub_field_count;$i++) //again, this is done to mantain table structure.
            echo "<td>".(empty($sub_row[$i])?"&nbsp":$sub_row[$i])."</td>"; //same as above
    }
    echo "</tr>\n";
}
echo "</table>\n";
Post Reply