Current location: Hot Scripts Forums » Programming Languages » PHP » Please help - Flatfile question


Please help - Flatfile question

Reply
  #1 (permalink)  
Old 12-21-06, 04:39 PM
jayz jayz is offline
Newbie Coder
 
Join Date: May 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question Please help - Flatfile question

hope someone can help with this.

I am trying to create a a flatfile database which stores a line number as a unique id

Example:

1 | $Name | $Email | $Date |
2 | $Name | $Email | $Date |
3 | $Name | $Email | $Date |
4 | $Name | $Email | $Date |

and so on...

im have trouble trying to get the next save location, when an entry has been removed.

Example:

if entry 2 has been removed & the next entry is saved it is saved as

5 instead of 2.

I've tried a number of things but all fail.

i have tried doin this with a function

example:

PHP Code:



$x 
=1


function 
FindNextID($z)
{
$ffile "dbfile.dat";
$fdata file($ffile);
for (
$q 0$q sizeof($fdata); $q++)
{
list (
$id$name$email$date) = explode("|"trim($fdata[$q]));
if (
$id >= $z)
{
$z $id+1;
}
}
DoSave($z);   // Take the next ID and save
}

FindNextID($x); 

This code will find the next id location but does not take into account if id 2 is missing

so i tried somethin like this

PHP Code:

$x =1;


function 
FindNextID($z)
{
$dosave 1;
$ffile "dbfile.dat";
$fdata file($ffile);
for (
$q 0$q sizeof($fdata); $q++)
{
list (
$id$name$email$date) = explode("|"trim($fdata[$q]));

if (
$id == $z)
{
$dosave 0;
$z $z+1;
FindNext($z);
}
}
if (
$dosave)
{
DoSave($z);
}
}

FindNextID($x);  // can be called from another function 
this is as close as i can get .... it does save the next location but!!

after about 2 saves it does mental lol! and saves the same data over again with a new id

I know there is somethin missing but not sure what

any help would be great

thanks!!
Reply With Quote
  #2 (permalink)  
Old 12-21-06, 04:54 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
I hope you're not planning on having a large amount of records. Is there any reason you'd be opposed to a MySQL database? It'd save you loads of headaches.

...
And shouldn't the next entry be 5 anyhow? Why would you replace the deleted #2? That kind of kills the point of unique IDs.
__________________
The toxic ZCE
Reply With Quote
  #3 (permalink)  
Old 12-21-06, 05:05 PM
jayz jayz is offline
Newbie Coder
 
Join Date: May 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
hi thanks for the reply

sorry but sql is not an option if it had i wouldnt be writtin this lol


Quote:
'And shouldn't the next entry be 5 anyhow?
yes thats what i said!

but i want the code to check if an entry has been removed like in the example.

if 2 is missing then call the next save id 2 - is it really that difficult to understand?

the whole point is to stop the ids from spiraling out of control

if there are 200 entries and 100 have been removed previously the id will be above 300! instead of 200.

makes sence to me.

the size of the file at this stage is not important!

thanks
Reply With Quote
  #4 (permalink)  
Old 12-21-06, 05:15 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
I understood you the first time. The thing I fail to agree on is how you see single-incrementing values as sprawling out of control on what is evidently not going to become such a large file anyhow.



What you'll need to do is a bit crazy:

First, you'll need to explode the contents of the file into an array by line.

Then you'll have to loop through the array and get the ID of each line as you go. But save the ID to a temporary variable. You'll need it.

When you get to the next line and have the ID, you'll have to add 1 to the temp variable and compare it with the current variable. If they match, save that ID to the temp variable and move on. If they don't match, you've found a deleted value, and you now have your variable.

Then what do you do? You'll have to rewrite your file to that point and append the value to the end of the file - or write it there and then continue rewriting the entire file. If my guess is correct, you'll be rewriting your entire database file every time you insert, update, and delete. That is insane.

And I still don't get how the size of the file is not important, but you're concerned about the IDs sprawling out of control? This makes no sense.

But there you have it, an easy enough solution if you have an extremely low-traffic database and few extra hours on your hands.
__________________
The toxic ZCE

Last edited by Keith; 12-21-06 at 05:23 PM.
Reply With Quote
  #5 (permalink)  
Old 12-21-06, 05:23 PM
jayz jayz is offline
Newbie Coder
 
Join Date: May 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
ok i get what your saying about rewrite the entire file..

would it be more stable to save the line data to its own file rather then all data to a single file... example

file_1.dat < inside has the data .. $name | $email | $date .....

what would you say ?

and not mysql lol
Reply With Quote
  #6 (permalink)  
Old 12-21-06, 06:10 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Well, rather than save it pipe-delimited, I'd either var_export() or serialize() the array. That way you don't have to worry about replacing characters or checking for pipes in the user input.

To save the data:
PHP Code:

<?php


// dummy up some user input... could easily be _POST data
$data = array
(
    
'name'  => 'Joe Smith',
    
'email' => 'joe@smith.com',
    
'date'  => '12/25/2007'
);

// get your current ID
$id = ( int ) $_REQUEST['id'];

// prepare your data array
$data var_export$data); // $data = serialize( $data );

// set the current working file
$file 'file_' $id '.dat';

// write the data array to the file
$fp = @fopen$file'w' );
if ( 
$fp )
{
    
fwrite$fp$data );
    
fclose$fp );
}
else
    echo 
'Could not save.';
To read the data:
PHP Code:

<?php


// get your current ID
$id = ( int ) $_REQUEST['id'];

// set the current working file
$file 'file_' $id '.dat';

// read your data array
if ( file_exists$file ) )
    eval( 
'$data = ' file_get_contents$file ) . ';' ); // $data = unserialize( file_get_contents( $file ) );
else
    
$data = array();

// $data now has the original saved array

?>
__________________
The toxic ZCE

Last edited by Keith; 12-21-06 at 06:32 PM.
Reply With Quote
  #7 (permalink)  
Old 12-21-06, 06:50 PM
jayz jayz is offline
Newbie Coder
 
Join Date: May 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
ok...

so save each submitted date to its own file is the best way yeah....

i will have to look into var_export & serialize as i dont know anythin about them

$id... in your example what is the $id where are you getting this from a form or somthin?
Reply With Quote
  #8 (permalink)  
Old 12-21-06, 06:55 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Yeah, using $_REQUEST, it will pick from $_GET, $_POST, and $_COOKIE, so you could use:
Code:
file.php?id=3
or in a form:
Code:
<input type="hidden" name="id" value="3" />
To make the two work together:
PHP Code:

<?php


$id 
= ( int ) $_REQUEST['id'];

?>

<form action="<?= basename$_SERVER['PHP_SELF'] ); ?>" method="post">
    <input type="hidden" name="id" value="<?= $id?>" />
    <!-- REST OF YOUR FORM -->
</form>
__________________
The toxic ZCE

Last edited by Keith; 12-21-06 at 06:59 PM.
Reply With Quote
  #9 (permalink)  
Old 12-21-06, 07:13 PM
jayz jayz is offline
Newbie Coder
 
Join Date: May 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
wow thats interesting about 'REQUEST'

sorry for sounding stupid but what is the ( int )

by doin it this way will solve my problem to begin with too

example:

PHP Code:



$x
=1;

// Check if file already exists - Start
function checkfile($id)
{

$fpath "file-$id.dat";    // Save File

if (file_exists($fpath))    // Does the ID already exist?
{
$id++;                      // Yes! so increase it
$x $id;
checkfile($x);              // Check the new id
}
else
{
dosave ($fpath);            // Found new ID save it!
}
}
// Check if file already exists - End


checkfile($x); 
thanks for your help - i will look into var_export it sounds just what i need to not have delimiters
Reply With Quote
  #10 (permalink)  
Old 12-21-06, 07:26 PM
coolmark18's Avatar
coolmark18 coolmark18 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
I suggest not using comments such as
Quote:
- is it really that difficult to understand?
when people are voluntarily giving up their time to try and help you.
Reply With Quote
Reply

Bookmarks


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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 02:04 PM
Posting a question / answer on site markcody PHP 2 11-23-04 01:58 PM
[PHP] Array question UmiSal Script Requests 1 04-05-04 01:52 PM
question and answer software jaydifox C/C++ 0 02-21-04 09:26 AM
Flatfile question Agent PHP 1 08-02-03 01:12 AM


All times are GMT -5. The time now is 05:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.