brain spasm... formatting a text file into html columns
Moderator: General Moderators
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]ceil[/php_man]
[php_man]array_chunk[/php_man]
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>";so i tried...
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
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>";
?>Fatal error: Call to undefined function: array_chunk() in /home/httpd/vhosts/localsupport.com/httpdocs/vpcpages/sup_listing2.php on line 437
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;
}S.O.B
Thank you for all yoru help.. but its still not wokring...
I will seriously paypal you a dollar is you fix it
now the probelm is in the FOREACH...
cant do that to an array?
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?
i luv u man
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?
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  </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>");
?>ok, suppose you have in file:
Do you want this records to appear on your page as:
?
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";