homepage Welcome to WebmasterWorld Guest from 54.145.183.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How can I delete an entry after been selected?
fredfletcher



 
Msg#: 4431147 posted 3:55 am on Mar 20, 2012 (gmt 0)

Hello,

I would like to know if there is a way to delete an entry from a text file, after it has been randomly selected from a file and echoed on screen, then once echoed, to delete it after from the file?

Here is my present code:

<?php

$r_array=file('/home/user/fred/random.txt');

array_shift($r_array);

shuffle($r_array);
echo "<b>Single Random line</b>:<br>";

echo $r_array[0];

?>


 

nyteshade



 
Msg#: 4431147 posted 12:35 pm on Mar 20, 2012 (gmt 0)

I'm no php expert.. just for a lark, and practice I tried this:

<?php
$r_array=file('testin.txt');

if (!empty($r_array)){
array_shift($r_array);

if(!empty($r_array)){
echo "<b>Array after first shift</b>:<br>";
foreach($r_array as $ele){
echo "$ele<br/>";
}

shuffle($r_array);
echo "<b>Single Random line</b>:<br>";
echo "$r_array[0]<br/>";

array_shift($r_array);

echo "<b>Array after second shift</b>:<br>";
foreach($r_array as $ele){
echo "$ele<br/>";
}

$output = implode($r_array);

$fh = fopen('testin.txt', 'w') or die('Failed to create file');
fwrite($fh, $output) or die('Could not write to file');
fclose($fh);

echo "<p>File written successfully.</p>";
} else {
echo "<p>Shifted last element, array is now empty.</p>";}
}else {
echo "<p>Input file is empty.</p>";
}
?>

fredfletcher



 
Msg#: 4431147 posted 2:32 pm on Mar 21, 2012 (gmt 0)

Thanks, this worked fairly well, but only if I comment out the first "array_shift($r_array);". If not, it deletes more entries when reloading the script; meaning, it deletes 2 entries instead of only one entry at a time. I appreciate it though :)

nyteshade



 
Msg#: 4431147 posted 5:17 pm on Mar 21, 2012 (gmt 0)

Hmmmm, just following your original script. I'm just curious, what is the purpose of this script? Just wondering how I might, ahem, repurpose it to something useful? If I were to guess, I'd guess it has something to do with a dealing shoe?

fredfletcher



 
Msg#: 4431147 posted 8:10 pm on Mar 21, 2012 (gmt 0)

What I would like to do is have people sign up for a contest, where they would enter their first/last name and email, which is then added to a .txt file. Once the contest is due to be drawn, (I have a script for that), would read from the same file, randomly selecting an entry, echoeing it on screen, then have the one that was randomly selected, deleted from the file, in order to keep the other entries intact for a future contest draw.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved