Reply LinkBack Thread Tools Search this Thread
Old 15-12-2007, 08:13   #1 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
help with If statement

i use the following to extract a word or two from a text file:

<?
$fp = file('word.txt');
for($i = 0; $i <sizeof($fp);$i++){
echo $fp[$i]. "";
}

?>

How can i get it, using the above and adding more to display depending on the word stored. for example, if the work in the text file is green, then how would i tell to change the colour of the it is in to gree, and if its red to red etc...
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 15-12-2007, 09:00   #2 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
i want something like this:


<?
$fp = file('word.txt');
for($i = 0; $i <sizeof($fp);$i++){

echo $fp[$i]. "";

}

$yes = " Yes";

if ($fp[$i] == $yes) {

echo 'Yes ha ha ah';

}

else {

echo 'No...';
}

?>

however this is not working, the output is "YesNo" - i need to to check the contents of the file and compare it to a variable i have set myself.
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 15-12-2007, 13:12   #3 (permalink)
wheedwacker
competitionmaster 2.0
 
wheedwacker's Avatar
 
Join Date: Oct 2006
Location: USA
Posts: 1,419
could you share the word.txt file so we know what is in it?
  Reply With Quote
Old 15-12-2007, 21:32   #4 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,695
Are you trying to check for the existence of a string in a text file? Your code is a little garbled.
  Reply With Quote
Old 15-12-2007, 22:36   #5 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
The word file only contains one word. either yes or no - its for my availablity on my website, so for instance, if the word file says "yes", i want the ouput to be a green div saying available for hire with the green div attributes like a green border etc.. and the words available for hire in the div. , however, if the word file says no i want a red div to appear saying not available for hire with a red border etc...
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 15-12-2007, 23:54   #6 (permalink)
wheedwacker
competitionmaster 2.0
 
wheedwacker's Avatar
 
Join Date: Oct 2006
Location: USA
Posts: 1,419
<?php
$word = include 'word.txt';
if($word=='yes') {
//show for hire stuff
} else {
//show not for hire stuff
}
  Reply With Quote
Old 16-12-2007, 01:25   #7 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,606
I think
$word = include 'word.txt';
should be
$word = include('word.txt');

Although having never used include this way, I could be wrong.

On another side note to mgpwr, you really should open your php scripts with <?php not just <?
  Reply With Quote
Old 16-12-2007, 01:40   #8 (permalink)
wheedwacker
competitionmaster 2.0
 
wheedwacker's Avatar
 
Join Date: Oct 2006
Location: USA
Posts: 1,419
Quote:
Originally Posted by haku
I think
$word = include 'word.txt';
should be
$word = include('word.txt');

Although having never used include this way, I could be wrong.

On another side note to mgpwr, you really should open your php scripts with <?php not just <?
include works either way you do it, it doesn't matter
I agree on the not using short tags part.
  Reply With Quote
Old 16-12-2007, 03:50   #9 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,695
Quote:
Originally Posted by mgpwr
The word file only contains one word. either yes or no - its for my availablity on my website, so for instance, if the word file says "yes", i want the ouput to be a green div saying available for hire with the green div attributes like a green border etc.. and the words available for hire in the div. , however, if the word file says no i want a red div to appear saying not available for hire with a red border etc...

If you really want to use files to do it, your approach wouldn't be the generally accepted method. What you have here is a boolean operation (i.e. there are only two possible choices, yes or no). Part of the art of programming is to handle operations as efficiently as possible. When you're faced with a simple yes/no choice, the file doesn't need to contain any data at all. It simply needs to exist or not to exist. Opening, reading and checking the contents of a file are all wasteful and unecessary.

All you need to do is create a blank file, called for example 'available', when you're available and delete it when you're not. The PHP code simply needs to check for the existence of the file to determine which branch to take.

Code:
<?php $filename="available"; if(file_exists($filename)) { // Code to display you are available } else { // Code to display you aren't available } ?>

This is the principal used by a lot of desktop applications (e.g. a word processor) to 'lock' a file so that other people can't edit it at the same time.

You can create a blank file in php using the touch() function and delete it with unlink(). Both of these functions are named according to Unix conventions. Be careful that the directory in which the file is to be created/deleted has read/write permissions for the webserver.

Last edited by Hunch : 16-12-2007 at 04:00.
  Reply With Quote
Old 16-12-2007, 06:36   #10 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
Quote:
Originally Posted by wheedwacker
<?php
$word = include 'word.txt';
if($word=='yes') {
//show for hire stuff
} else {
//show not for hire stuff
}


Using this to test:

<?php
$word = include ('word.txt');

if($word == 'Yes') {

echo 'Available for hire yes i am';

} else {

echo 'not just yet i am afraid';


}

I get the output "Yes not just yet i am afraid" - The word file contains the word yes, how come it is not working?
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 16-12-2007, 06:38   #11 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
Quote:
Originally Posted by Hunch
If you really want to use files to do it, your approach wouldn't be the generally accepted method. What you have here is a boolean operation (i.e. there are only two possible choices, yes or no). Part of the art of programming is to handle operations as efficiently as possible. When you're faced with a simple yes/no choice, the file doesn't need to contain any data at all. It simply needs to exist or not to exist. Opening, reading and checking the contents of a file are all wasteful and unecessary.

All you need to do is create a blank file, called for example 'available', when you're available and delete it when you're not. The PHP code simply needs to check for the existence of the file to determine which branch to take.

Code:
<?php $filename="available"; if(file_exists($filename)) { // Code to display you are available } else { // Code to display you aren't available } ?>

This is the principal used by a lot of desktop applications (e.g. a word processor) to 'lock' a file so that other people can't edit it at the same time.

You can create a blank file in php using the touch() function and delete it with unlink(). Both of these functions are named according to Unix conventions. Be careful that the directory in which the file is to be created/deleted has read/write permissions for the webserver.

I dont want to check the file exists, i wish to check that the file contains a word, the word YES, and if it does do that, if it contains the word NO - then do that!
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 16-12-2007, 06:56   #12 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,606
The point Hunch was making is that you are overburdening your code that way. When you first create the file, you have to do two steps:
1) create the file
2) write yes or no to it

This has to be done regardless of whether the answer is yes or no.

Then, when you want to check what it says, you have to do to more steps:

1) access the file
2) read from it.

With Hunch's method, when you first decide if its yes or no, you either have one or no steps:

If yes
1) create the file

If no
Do nothing.

And when you want to check if its yes or no, you only have one step:

1) check if the file exists.

If it does, then its yes, and if it doesnt, its no.

Hunch's method cuts down the number of steps and slims down your code, and the number of things it has to do, which will speed things up. Granted, the amount it speeds it up will be quite minimal, but its just better coding.

Its not that you can't do it the way you want, rather that you can do it better than the way you want.


As to why your code isn't working, I can't help you there. It looks fine to me. Can you post either a link or more of your code?
  Reply With Quote
Old 16-12-2007, 07:37   #13 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
I don't think i understand you, I dont want to create a file - in my dir there are these two files test.php and word.txt

I want test.php to look at word.txt, if word.txt has the word yes in it, i want it to do this... if it has the word No, i want it to do that! - it seems simple and i cnt see why the code isnt working

That is all the code...
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 16-12-2007, 07:37   #14 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,695
mgpwr... as haku pointed out, you're misunderstanding the point of my previous post. The method you are using is unecessarily complex and more prone to failure for a number of reasons. The file existence suggestion I made, is a far better solution for what you are trying to do. However, if you are determined to continue down this route, your code as it stands will not work for a number of reasons.

First off, you don't seem to understand the way return values work, and how to use include. The include function is designed to allow you to read in a separate file and add it to the file that is currently being parsed by the web server. It may be additional PHP code, it may be HTML, it may be text. The point is, that it's not designed to be used the way you are trying to. When you call a function such as include, it returns a status value to indicate whether or not it succeeded in reading in the file. In this case, on a 'successful read' the function will return a value of 1. This is what your variable $word is being set to.

In brief terms, this is what your code does:

Code:
<?php $word = include ('word.txt'); // the include function is called, reads in the file, and assigns the value '1' (success) to the variable $word if($word == 'Yes') { // $word evaluated against 'Yes' and as it currently equals 1 doesn't match echo 'Available for hire yes i am'; // Will never be executed } else { // The else branch is taken echo 'not just yet i am afraid'; // This statement is executed } ?>

A couple of points:

Edited due to an error in my advice:
Don't use include to read in text. Use the appropriate fopen/fread/fclose functions.

If you are determined to use this method, and not take my previous advice, that you use preg_match() instead of the == operator to check the contents of the file. It'll avoid any potential problems with extraneous spaces, control characters or anything else that could potentially creep into the file when it's created.

Last edited by Hunch : 16-12-2007 at 08:10.
  Reply With Quote
Old 16-12-2007, 07:50   #15 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
The problem i have with this, is the file is already created, and on my cms it changes the word in the file depending on what option i checked. so ur script is always going to return the value "no"
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 16-12-2007, 07:55   #16 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,695
If you're editing the file using a CMS, take note of the points at the end of my previous comment and you'll be fine. However, given that you're going to the trouble of coding this solution in PHP, why not spend an extra 20 minutes creating a simple control form (pair of radio buttons, or a checkbox would be fine) which allow you to turn on or off your availability at a click of the mouse? Opening a file in the CMS, editing it to say yes or no, and then saving it is a bit long winded. You might as well just open the page itself and edit the bit that says "I'm available".
  Reply With Quote
Old 16-12-2007, 08:00   #17 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,962
PHP Code:
function gethtmlfromfile($file){

        
$filename "$file";

        
$htmlfromfile "";

        
$fp fopen ($filename"r") or die("Couldn't open $filename");    
    
        while (! 
feof($fp)){
            
$line fgets($fp,1024);
            
$htmlfromfile "$htmlfromfile"."$line";    
                }

        return 
$htmlfromfile;

        } 

Write this function above then

$word = gethtmlfromfile("filename.txt");

if($word=="Yes"){echo "Yes I am available to hire.";}
elseif($word=="No"){echo "Sorry I am busy right now.";}
else{echo "File does not contain Yes or No";}
__________________
  Reply With Quote
Old 16-12-2007, 08:00   #18 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,606
Or change the coding in your cms so that rather than printing "yes" or "no" to a file, it either creates one or not as hunch stated earlier.
  Reply With Quote
Old 16-12-2007, 08:05   #19 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,962
If you liked this function you might also enjoy
PHP Code:
function writealltofile($text,$file){


        
$fp fopen ($file"w") or die("Couldn't open $file");
        
flock ($fp,LOCK_EX);
    
        
fwrite($fp"$text");

        
flock ($fp,LOCK_UN);
        
fclose ($fp);
    

    } 
__________________
  Reply With Quote
Old 16-12-2007, 08:13   #20 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,695
That's fine datahound, but it's still leading him towards an overly complex solution.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8