View Full Version : Removing a line in a data base
I am making a site were if a member clicks on a website in the "shop" that they will get a bit of money.
The this is, that thease websites have only a curtain amount before they disapear.
How can I make it, that if $var2 is <1 that it will remove that line that the user clicked on?
This is the code i have so far:
if ($shop eq "") {$shop = "main/everything";}
open(FILE,"$shop.txt") || print "$!";
@LINES = <FILE>;
close(FILE);
$dl = "$formdata{'item'}";
$line = "@LINES[$dl]";
($var1,$var2,$var3) = split(/\|/, $line);
$var2 = $var2 -1;
@LINES[$dl] = "$var1|$var2|$var3\n";
open(FILE,">$shop.txt") || print "$!";
for ($i = 0; $i < $#LINES; $i++) {
print FILE "@LINES[$i]";
}
close(FILE);
Thanks in advanced
Tim
Grizz2
12-15-2002, 04:21 AM
You might try something like this. Be sure to check my syntax..Its Late......
$dl = "$formdata{'item'}";
#start a counter
$linecounter = 0;
open(FILE,"$shop.txt") || print "$!";
while(<FILE>)
{
$line = $_;
if($linecounter != $dl
{
push(@newlines,$line);
++$linecounter;
}else{
($var1,$var2,$var3) = split(/\|/, $line);
$var2 = $var2 -1;
unless($var2 < 1)
{
$newline = "$var1|$var2|$var3\n";
push(@newlines,$newline);
}
}
} # end while
close(FILE);
open(FILE,">$shop.txt") || print "$!";
print FILE "@newlines";
close(FILE);
Grizz
Sorry Grizz,
That did not work. Maybe if I gave you the zip file with all the inportant stuff ..... (please note you will have to change the directorys; )
Thanks in advance
See the problem is, that when youbuy an item in the shop, it will start going into minus' (e.g -1, -2) and i would like it if that item would dissapear from the shop.
Thanks in advance!
Grizz2
12-16-2002, 02:58 AM
Well darn it.
I looked at your txt file and we need to change this line in the script:
$linecounter = 0;
to
$linecounter = 1;
Since your id numbers start at 1.
Also added this line.
chomp($line);
Before you test, check your txt file for blank lines and remove them. Give this a try and let me know what happens and we'll go from there.
$dl = "$formdata{'item'}";
#start a counter
$linecounter = 1;
open(FILE,"$shop.txt") || print "$!";
while(<FILE> )
{
$line = $_;
if($linecounter != $dl
{
push(@newlines,$line);
++$linecounter;
}else{
chomp($line);
($var1,$var2,$var3) = split(/\|/, $line);
$var2 = $var2 -1;
unless($var2 < 1)
{
$newline = "$var1|$var2|$var3\n";
push(@newlines,$newline);
}
}
} # end while
close(FILE);
open(FILE,">$shop.txt") || print "$!";
print FILE "@newlines";
close(FILE);
The code WILL take away one item...
but if you buy an item the file were the shop items are, ever new line (except the first) will have a space before it.
1|9|3000
2|17|300
3|48|300
when it should be:
1|9|3000
2|17|300
3|48|300
thanks in advance if you can help!
Tim
Grizz2
12-16-2002, 03:02 PM
That is kinda strange!!
Here's a temporary fix you can try until I have a chance to look again.
This will remove all spaces so if you need spaces somewhere, we'll have to do something else.
foreach $i(@newlines)
{
$i =~ s/\s//g;
}
Put this after you close the file the first time and before you write the array back to the file.
I followed your steps....
BUT:
the file then didnt have any new lines!
I fixed that buy when it prints the arrays back to the file replaced it with this:
open(FILE,">$shop.txt") || print "$!";
foreach $i(@newlines)
{
$i =~ s/\s//g;
print FILE "$i\n";
}
print FILE "\n";
close(FILE);
and then it worked!
THANK YOU Very much for ALL of your help!
OK, this is the last message that i will send for help (i hope) on this subject.
I have a BIG problem!
What happens now, is if i "buy" the first item from the shop, the quantity for that item will go down by one.
Thats good, thanks all for your help on that!
BUT!.......
Now not only will it then take the quantity from that item, but from the items after that.
--OR--
if i take the second item that goes down by one, but then the next ones go down by one as well. but the first one wont be touched and it keeps going on!
I really do hope that you can help me with this as it is very difficult for me and as i am only starting with perl it is kind of like a trivial!
This is the .pl code so-far:
if ($shop eq "") {$shop = "main/everything";}
$shop = "shops/$shop";
$dl = "$formdata{'item'}";
#start a counter
$linecounter = 0;
open(FILE,"$shop.txt") || print "$!";
while(<FILE> )
{
$line = $_;
if($linecounter != $dl)
{
foreach $i(@newlines)
{
$i =~ s/\s//g;
}
push(@newlines,$line);
++$linecounter;
}else{
chomp($line);
($var1,$var2,$var3) = split(/\|/, $line);
$var2 = $var2 -1;
unless($var2 < 1)
{
$newline = "$var1|$var2|$var3\n";
push(@newlines,$newline);
}
}
} # end while
close(FILE);
####################################################
# DOES THE USER HAVE ENOUGH MONEY TO BUY THE ITEM? #
####################################################
open (FILE, "$MEMLOC/$username.dat");
@DATA=<FILE>;
close (FILE);
@DATA[1] =~ s/\n//g;
$money = $nd-$da;
if ($money < 0) {print "You don't have enough money!!!";exit;}
open (FILE, ">$MEMLOC/$username.dat");
print FILE "@DATA[0]";
print FILE "$money\n";
print FILE "@DATA[2]";
print FILE "@DATA[3]";
print FILE "@DATA[4]";
print FILE "@DATA[5]";
close (FILE);
open(FILE,">$shop.txt") || print "$!";
foreach $i(@newlines)
{
$i =~ s/\s//g;
print FILE "$i\n";
}
print FILE "\n";
close(FILE);
print "You can now safely go back because the item was bought sucsesfully";
Please Help Me!
Tim!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.