Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP read and display file content in html table with 2 colums
Old 03-07-2009, 06:50 PM PHP read and display file content in html table with 2 colums
Experienced Talker

Posts: 38
Trades: 0
Dear guys,

I need help in reading a text file and displaying the content in an html table with 2 columns.

mytextfile.txt
value1
value2
value3
value4
value5

I've found this code but it's for mySQL use.

<?php
/**
* If the count is odd, then add an extra
* row to the first column, otherwise, keep
* the rows the same.
*/
$count = mysql_num_rows($result);
if (($count % 2) & 1) {
$c1 = (int) ($count / 2 + 1);
} else {
$c1 = $count / 2;
}
/**
* I'm using two loops here because it is slightly faster
* than forcing the an if..else construct for every iteration.
*/
$str1 = '';
$str2 = '';
for($i = 0; $i < $c1; $i++) {
$row = mysql_fetch_arry($result)
$str1 .= '<li>' . $row['columnName'] . '</li>';
}
for($j = $c1; $j < $count; $j++) { // Use the $c1 as the base num
$row = mysql_fetch_arry($result)
$str2 .= '<li>' . $row['columnName'] . '</li>';
}

print <<<BLOCK
<table>
<tr>
<td valign="top"><ul>$str1</ul></td>
<td valign="top"><ul>$str2</ul></td>
</tr>
</table>
BLOCK;
?>

Appreciate if you can please help, thanks.
superkingkong is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-07-2009, 07:21 PM Re: PHP read and display file content in html table with 2 colums
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
First you need to read your file:
PHP Code:
$file 'pathToYourFile.txt';
$fileHandle fopen($file'r');

$data fread($fileHandle4096); 
$data now contains all of the data in your file. To get each element, and assuming each element is line seperated you must parse the file contents into an array like this:

PHP Code:
$dataArray explode($data"\r\n"); 
Then with a loop you can output each element:

PHP Code:
$format '<tr>
                 <td>%s</td>
                 <td>%s</td>
               </tr>'
;

$first NULL;
$second NULL;

echo 
'<table>';

foreach(
$dataArray as $s)
{

     if(
$first == NULL)
           
$first $s;
     else
     {
           
$second $s;
           
printf($format$first$second);
           
$first NULL;
           
$second NULL;
     }
}

if(
$second == NULL && $first != NULL)
{
     
printf($format$first'');
}

echo 
'</table>'
Not tested, not as clean as I'd like, but it should do the trick.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-07-2009 at 07:34 PM.. Reason: Fixed bug
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-07-2009, 10:23 PM Re: PHP read and display file content in html table with 2 colums
Experienced Talker

Posts: 38
Trades: 0
hi,

thanks for the responce.

i've tried the code, but i get a blank page.
i point it to the right file 'file.txt' (php and txt are in the same dir).

please help, thanks.
superkingkong is offline
Reply With Quote
View Public Profile
 
Old 03-07-2009, 10:54 PM Re: PHP read and display file content in html table with 2 colums
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I fixed it.. I had the parameters for explode switched around (which is what happens when your brain is fried from writing java code all day). Also I replaced fopen and fread with file_get_contents because it is more efficient when you only need to read a file:

PHP Code:
<?php
$file 
'pathToYourFile.txt';
$data file_get_contents($file);
$dataArray explode("\n"$data);
$format '<tr> 
                 <td>%s</td> 
                 <td>%s</td> 
               </tr>'


$first NULL
$second NULL

echo 
'<table>';

foreach(
$dataArray as $s


     if(
$first == NULL
           
$first $s
     else 
     { 
           
$second $s
           
printf($format$first$second); 
           
$first NULL
           
$second NULL
     } 


if(
$second == NULL && $first != NULL

     
printf($format$first''); 


echo 
'</table>'

?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-07-2009, 10:55 PM Re: PHP read and display file content in html table with 2 colums
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
It should work without any problems this time. I tested it myself.

Edit: Didn't mean to double post I thought I had hit the edit button for some reason. This is also what happens when your brain is fried from writing java code all day.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 12-27-2010 at 10:02 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-07-2009, 11:32 PM Re: PHP read and display file content in html table with 2 colums
Experienced Talker

Posts: 38
Trades: 0
Hey, thank you very much!
it works!
really appreciate your help, thanks again
superkingkong is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP read and display file content in html table with 2 colums
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17836 seconds with 12 queries