UK WEB HOSTING FORUM FOR DISCUSSION ON WEB HOSTING SERVICE AND SUPPORT
LINUX HOSTING WINDOWS HOSTING PACKAGES SHOPPING CART OSCOMMERCE ZEN CART AGORA
ECOMMERCE HOSTING ASP MSSQL FRONTPAGE HOSTING PHP MYSQL HOSTING DISCUSSION FORUM
CPANEL RESELLER HOSTING DEDICATED SERVER VPS HOSTING PLESK VIRTUOZZO
Quick Search
Your forum announcement here!

  UK Web Hosting | Dedicated Server Windows and Linux VPS Forum > Web Hosting and Domains > PHP Hosting

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 28-01-2008, 11:43
LiberMan's Avatar
new member
 
Join Date: Jan 2008
Posts: 3
Send a message via ICQ to LiberMan
Default Pls help I need it badly thanks...

Hello guys,

I want to learn php, writing all these data instead, to the textfile called employees.txt in the notepad of course. I heard its posible.

Supposing you have a data already in a textfile.txt that contain

MY CLIENTS DATA in employees.txt
ID FIRSTNAME LASTNAME AGE
1 IAN UY 27 delete edit
2 JACKSON LEE 32 delete edit
3 JEAN TY 38 delete edit
4 JAMES HAT 22 delete edit
5 ARMAN CO 20 delete edit

and all i want is to make a php file that would display them all to a table format in view.php for example and of course I can EDIT and DELETE each one of them for example i would like to delete the first line IAN. Thats what exactly i want.

any sample code and right codes pls give me...i am getting crazy about this php is very confusing..but I am not giving up...never...pls help...ty

Pls help that would be greatly appreciated. Thanks..
REPLY ASAP
Reply With Quote
  #2 (permalink)  
Old 28-01-2008, 13:09
eUK-Martin's Avatar
Windows System Administrator
 
Join Date: Nov 2005
Location: Earth
Posts: 308
Default

I am not a programmer and not good at PHP. However I think you can achieve this with the PHP function file_get_contents();

Example :
Code:
 
$contents = file_get_contents('textfile.txt');
Hope some PHP coder will comment on this soon.
__________________
Martin
Windows System Admin.


Windows VPS Hosting - Windows Dedicated Server - Web Hosting Tutorials

Email :: windows @ eUKhost.com AND support @ eUKhost.com
Reply With Quote
  #3 (permalink)  
Old 28-01-2008, 14:02
DavidAllen's Avatar
Premium Member
 
Join Date: Jan 2007
Location: Amersham
Posts: 315
Send a message via MSN to DavidAllen Send a message via Skype™ to DavidAllen
Default

PHP Code:
//display the file in a table
echo '<table>';
//first read whole file into an array
$LinesArray file('path_to_file'.'/textfile.txt');
//loop through each line
for ($i=0;$i<count($LinesArray);$i++) {
//split the line into fields
$FieldsArray=explode(' ',$LinesArray[$i])
echo 
'<tr>';
for (
$j=0;$j<count($FieldsArray);$j++) {
echo 
'<td>'.rtrim($FieldsArray[$j].'</td>';
}
echo 
'</tr>';
}
echo 
'</table>'
That displays the file in a table

To edit you would need to wrap the whole lot in a form and use input fields
To delete you would need to use a checkbox or something at the end of each line

The processing of the form is a bit more complicated - but I hope the above helps for a start

PS - In the long run I think you would probably be better keeping the data in a database as that will give you much better flexibility (and security).

Regards
__________________
David Allen - www.serina.co.uk
Reply With Quote
  #4 (permalink)  
Old 28-01-2008, 14:11
eUK-Martin's Avatar
Windows System Administrator
 
Join Date: Nov 2005
Location: Earth
Posts: 308
Default

Wow David that was a very quick and short script. Just Great.
__________________
Martin
Windows System Admin.


Windows VPS Hosting - Windows Dedicated Server - Web Hosting Tutorials

Email :: windows @ eUKhost.com AND support @ eUKhost.com
Reply With Quote
  #5 (permalink)  
Old 28-01-2008, 14:14
mephisto's Avatar
Member
 
Join Date: Feb 2007
Location: Darlington
Posts: 99
Send a message via ICQ to mephisto Send a message via MSN to mephisto
Default

It's not exactly a simple job, but the best functions to use would probably be fgetcsv and fputcsv. You'll need to ignore the header of the file and then read the file into an array, and provide edit buttons to identify where in the file the row is located. This is a very quick 'n' dirty example, no security or file locking here.

PHP Code:
<?php
    $file 
'employees.txt'// make sure this points to your file

    
if(count($_POST) > 0) {
        
$rows = array();
        
$rows[] = array( // add header row
            
'ID',
            
'FIRSTNAME',
            
'LASTNAME',
            
'AGE'
        
);
        
// read in new structure
        
        
$size count($_POST['ID']);
        for(
$i 0$i $size$i++) {
            if(!isset(
$_POST['delete' $i])) { // we don't want deleted entries                
                
$rows[] = array(
                    
$_POST['ID'][$i],
                    
$_POST['firstname'][$i],
                    
$_POST['lastname'][$i],
                    
$_POST['age'][$i]
                );
            }
        }
        
        
$fp fopen($file'w'); // open the file for writing, truncate to zero length
        
foreach($rows as $row) {
            
fputcsv($fp$row' '); // write each row
        
}
        
fclose($fp);
    }
    
    
// open a file pointer as read only
    
$fp fopen($file'r');
    
$rows = array();
    
// ignore header
    
fgets($fp);
    while(!
feof($fp)) {
        
$rows[] = fgetcsv($fp0' '); // last parameter is delimter
    
}
    
$html = <<< EOH
<form method="post">
    <table>
        <tr>
            <th>ID</th>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Age</th>
            <th colspan="2"></th>
        </tr>
EOH;
    foreach(
$rows as $key => $row) {
        
$html .= <<< EOH
        <tr>
            <td>
                <input type="hidden" name="row
[]" value="{$key}"/>
                <input type="text" name="ID
[]" value="{$row[0]}"/>
            </td>
            <td>
                <input type="text" name="firstname
[]" value="{$row[1]}"/>
            </td>
            <td>
                <input type="text" name="lastname
[]" value="{$row[2]}"/>
            </td>
            <td>
                <input type="text" name="age
[]" value="{$row[3]}"/>
            </td>
            <td>
                <input type="submit" name="delete
{$key}" value="delete"/>
            </td>
            <td>
                <input type="submit" name="edit
{$key}" value="edit"/>
            </td>
        </tr>
EOH;
    }
    
    
fclose($fp);
    
    
$html .= "</table></form>";
    
    echo 
$html;
?>
I've not test the above, as I don't have anything to run it on available to me at the moment, but I think it should work.
Reply With Quote
  #6 (permalink)  
Old 28-01-2008, 14:33
mephisto's Avatar
Member
 
Join Date: Feb 2007
Location: Darlington
Posts: 99
Send a message via ICQ to mephisto Send a message via MSN to mephisto
Default

Quote:
Originally Posted by DavidAllen
PS - In the long run I think you would probably be better keeping the data in a database as that will give you much better flexibility (and security).
I agree, it would be must better to use a database. You could also use ajax to edit/delete each row, then a form wouldn't actually be needed, but that's on a whole another level.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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


All times are GMT. The time now is 19:12.

 

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by Web Hosting 3.1.0
Copyright © 2001-2008, eUKhost.com. All rights reserved.

 
Site Map

knowledgebase articles

popular blog categories