Jump to content

Files


Drippingblood123

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/182455-files/
Share on other sites

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");
}
}

Link to comment
https://forums.phpfreaks.com/topic/182455-files/#findComment-962965
Share on other sites

   

    $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];
}
    }
    

Link to comment
https://forums.phpfreaks.com/topic/182455-files/#findComment-963609
Share on other sites

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 />";
	}
}
?>   

 

Link to comment
https://forums.phpfreaks.com/topic/182455-files/#findComment-965167
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.