Drippingblood123 Posted November 22, 2009 Share Posted November 22, 2009 Hello I'm trying to make it so it only show the items that are over $20 in a txt document. This is the original code that stores it into a txt doc. <? $arr=array('Sun Glasses', 'Jeans', 'T-shirt, 'Shoes', 'Wig'); $arr2=array('11.00', '2.00', '40.00', '50.00, '60.00'); $file=fopen("PERSON.txt","w+") or exit("Unable to open file!"); for($i=0; $i<=4; $i++){ fwrite($file, $arr[$i]."\t"); fwrite($file, $arr2[$i]."\n"); } fclose($file); ?> Now this is what it looks like in the txt doc. Sun Glasses 11.00 Jeans 2.00 T-Shirt 40.00 Shoes 50.00 Wig 60.00 All of that works fine..now this is what I have no idea how to do. I have to make it list ONLY the items over $40.00, but it just comes up blank. <? $person = array(); $fh = fopen("PERSON.txt", 'r') or die("Can't open file"); while(!feof($fh)) { $f = fgets($fh); array_push($person, $f); } for ($i=0; $i<=4; $i++){ $arr = explode(' ', $person); fwrite($fh, $arr[$i]."\t"); fwrite($fh, $arr2[$i]."\n"); } fclose($fh); ?> I also know i need to echo it somehow. I'm just not sure. Any help would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/ Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962901 Share on other sites More sharing options...
dbillings Posted November 22, 2009 Share Posted November 22, 2009 Are you using the text file as a type of database? i.e. do you have your heart set on the format you have. It's easier to break things up if you use some kind of repeatable pattern like below. sun glasses****20.00@@@@ Sun Glasses****11.00@@@@ Jeans****2.00@@@@ Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962913 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 Yeah i've never even mess with a database. i have to get these file concepts down before i move onto them. im trying to teach myself and it is kinda hard Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962935 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962952 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 anybody? it shouldnt be too hard. i'm just a noob and am stuck Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962959 Share on other sites More sharing options...
purencool Posted November 22, 2009 Share Posted November 22, 2009 You seem to have no if statement to test the price of the array you are putting into the text file and I you use a foreach loop instead of a for loop it will make your code cleaner. Something like this for($i=0; $i<=4; $i++){ if($arr2[i] > 20){ fwrite($file, $arr[$i]."\t"); fwrite($file, $arr2[$i]."\n"); } } Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962965 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 oh is that the only thing i'm missing? Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962967 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 its not working Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-962985 Share on other sites More sharing options...
emopoops Posted November 22, 2009 Share Posted November 22, 2009 you dont have anything about the 40 dollars in your code hon Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963006 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 I know. That's the only part I don't get how to do. Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963019 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963031 Share on other sites More sharing options...
emopoops Posted November 22, 2009 Share Posted November 22, 2009 no offense but database is easier to understand than files like this and wayy easier to do... just use a datbase. Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963042 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 I can't. Does anyone know how to do it without using a database? Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963520 Share on other sites More sharing options...
Drippingblood123 Posted November 22, 2009 Author Share Posted November 22, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963542 Share on other sites More sharing options...
Drippingblood123 Posted November 23, 2009 Author Share Posted November 23, 2009 .. Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963589 Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 yeah, give me a sec. Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963592 Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 $filename = 'filename.txt'; $gtValue = 20; if (filesize($filename) > 0) { $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); } $seperate = explode("****", $contents); $item = array(); $cost = array(); foreach ($seperate as $value) { // Split the array by IP and last page refresh. list($item[], $cost[]) = split('@@@@', $value); } for ($i=0; $i < count($cost); $i++) { if($cost[$i] < $gtValue) { unset($cost[$i]; unset($item[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963609 Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 You'll need to modify your textfile to this format and it isn't built to catch problems like a misformated textfile. sun glasses@@@@20.00****beans@@@@5.00**** Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963612 Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 you'd be able to read the values by doing the following for($i=0; $i < count($cost); $i++) { echo "Item: ".$item." Cost: ".$cost; } Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-963615 Share on other sites More sharing options...
dbillings Posted November 25, 2009 Share Posted November 25, 2009 Here's a debugged version, but it won't display unless you have the proper format in your text file. item@@@@cost****item2@@@@cost2**** <?php $filename = 'filename.txt'; // adjust for correct makeshift db file $gtValue = 20; // adjust for gt value if (filesize($filename) > 0) { $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); } $seperate = explode("****", $contents); $item = array(); $cost = array(); foreach ($seperate as $value) { list($item[], $cost[]) = split('@@@@', $value); } for($x=0; $x < count($cost); $x++) { if($cost[$x] > $gtvalue) { echo "Item: ".$item[$x]."\n"; echo "Cost: ".$cost[$x]."\n<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182455-files/#findComment-965167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.