Posted: Wed Nov 19, 2003 1:23 pm
can you ellabroate a little, im having trouble reading that
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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 columnsCode: 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>";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>";
?>If you have ancient PHP you may try a replacement:PHP manual wrote: array_chunk
Split an array into chunks (PHP 4 >= 4.2.0)
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;
}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>";
?>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  </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  </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>");
?>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 cityCode: 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|
+--------+---------+------+--------+---------+------+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])?" ":$sub_row[$i])."</td>"; //same as above
}
echo "</tr>\n";
}
echo "</table>\n";