php read text file(table)

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

Post Reply
tiingshii
Forum Newbie
Posts: 6
Joined: Wed Aug 26, 2009 3:49 am

php read text file(table)

Post by tiingshii »

Hi ppl, its me again~

Im currently working on php reading a text file. i managed to get the text showing orderly in a table. But there is an extra row between the title (school, date, time) and the first school (Anderson Primary School). How to remove that extra row?????? Anyone can help?

Lastly is there any ways that i can have a empty space between each school?
http://pddesignstudio.com/ckc/php2

php code

Code: Select all

<span class="style2">
<style type="text/css">
<!--
body, th, td, p, small {
    font-family:'Times New Roman',Times,serif;
    font-size:100%;
    color:#757675;
}
small {font-size:90%;}
 
td, th {
    background-color:#FFFFFF;
    border:1px solid #CCCCCC;
    padding:7px 20px 7px 20px;
}
th {background-color:#a5a5a5; color:#FFFFFF;}
 
h1 {font-size:120%; color:#558;}
h1 .sortby {color:#855;}
-->
</style>
</span>
 
<body>
 
<?php
echo '<h1><span class="sortby">'.$header.'</span></h1>
<table summary="List of demo fields">
<tr>
<th>Schools</th>
<th>Dates</th>
<th>Times</th>
</tr>';
 
$fp = fopen('flat-file-data2.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
 
while (!feof($fp)) {
    $line = fgets($fp,1024); //use 2048 if very long lines
    $row++;
    list ($schools, $dates, $times) = split ('\|', $line);
    if ($sortby == 'schools') $sortkey = strtolower($schools);
    if ($sortby == 'dates') $sortkey = strtolower($dates);
    if ($sortby == 'times') $sortkey = strtolower($times);
    $col[$row] = array($sortkey, $schools, $dates, $times);
}
 
fclose($fp);
 
$arrays = count($col) - 1;
 
$loop = -1;
while ($loop < $arrays) {
    $loop++;
    echo '
<tr>
<td>'.$col[$loop][1].'</td>
<td>'.$col[$loop][2].'</td>
<td>'.$col[$loop][3].'</td>
</tr>';
}
 
echo '
</table>
 
'?>
text file
Anderson Primary School|Monday 15 Dec 2009|2pm to 5pm
|Monday 15 Dec 2009|2pm to 5pm
|Monday 15 Dec 2009|2pm to 5pm
Ahmad Ibrahim Primary School|Monday 15 Dec 2009|2pm to 5pm
Chongfu Primary School|Monday 15 Dec 2009|2pm to 5pm
MacPherson Primary School|Monday 15 Dec 2009|2pm to 5pm
North View Primary School|Monday 15 Dec 2009|2pm to 5pm
Xishan Primary School|Monday 15 Dec 2009|2pm to 5pm
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: php read text file(table)

Post by N1gel »

Is it the simple case of having this

Code: Select all

$loop = 0;
instead of this

Code: Select all

$loop = -1;
As the array is indexed from 0. So you would be printing a row for array index -1 which dosen't exist

:D
tiingshii
Forum Newbie
Posts: 6
Joined: Wed Aug 26, 2009 3:49 am

Re: php read text file(table)

Post by tiingshii »

wow it works! thank you N1gel! its my first time dealing with php.

how about creating a spacing in between the school if i create paragraph spacing in .txt it will show empty tables.
Image
but i want something like this
Image
Anyways to create empty spaces between each school?

Last 2 things, can the text be centralize and is there a way to set the length of the table?
tiingshii
Forum Newbie
Posts: 6
Joined: Wed Aug 26, 2009 3:49 am

Re: php read text file(table)

Post by tiingshii »

anyone can help? :)
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: php read text file(table)

Post by N1gel »

Its not the best bit of code but this should add the extra row as you requested.

Code: Select all

 
 while ($loop < $arrays) 
{
     $loop++;
     echo '
 <tr>
 <td>'.$col[$loop][1].'</td>
 <td>'.$col[$loop][2].'</td>
 <td>'.$col[$loop][3].'</td>
 </tr>
 <tr><td colspan='3'>&nbsp;</td></tr>';
 }
 
You should be able to set the length of your table and the columns as followed

Code: Select all

 
 <table style='width:500px;' summary="List of demo fields">
 <tr>
 <th style='width:300px;'>Schools</th>
 <th style='width:100px;'>Dates</th>
 <th style='width:100px;'>Times</th>
 </tr>';
 
Not sure what text you wanted to be central? It all looks central to me
Post Reply