Still problem with files

This is a discussion on Still problem with files within the PHP General forums, part of the PHP Programming Forums category; Hi, Sorry for the extra emails, but I thought I had this licked, but now other problem pop up. As ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-17-2003
Beauford.2005
 
Posts: n/a
Default Still problem with files

Hi,

Sorry for the extra emails, but I thought I had this licked, but now
other problem pop up. As I said, I have a file with many lines like
<Option Value="3">Alfredsson in it. I have been able to read the file
into an array and display it, but I am trying to do a comparison IF
statement and no matter what I try I can't figure out what to compare.
To explain better here is my code so far.

$filename = "players.inc";

$fd = fopen ($filename, "r");
while (!feof ($fd)) {
$buffer = fgets($fd);
$list[] = $buffer;
}
fclose ($fd);

for($i = 0; $i < 131; $i++) {
if($list[$i] != ????????? {
echo "<BR>".htmlspecialchars($list[$i]); }
}

So using the example above what would I use where the question marks are
if I wanted to display everything but this line - <Option
Value="3">Alfredsson. I have tried a variety of ways to do this:

i.e

$p = "<Option Value=\"131\">Alfredsson";

for($i = 0; $i < 131; $i++) {
if(htmlspecialchars($list[$i]) != $p) {
echo "<BR>".htmlspecialchars($list[$i]);
}
}

This is just a sample script that I want to get running before I
actuaaly put it into use, so any help is appreciated.

Reply With Quote
  #2 (permalink)  
Old 07-17-2003
Beauford.2005
 
Posts: n/a
Default RE: [PHP] Still problem with files

For whatever reason your suggestions still caused me problems, but I did
come up with a solution that works.

if (preg_match ("/Alfredsson/", $buffer))

Another question though regarding \n. What I'm doing here is reading the
file, omitting one entry, adding another, and saving it back to the
file. When I add the entry and save it back to the file it appears
beside the last entry instead of below it. How do I get it so it is
below it. I have tried using a \n but no go. I am probably missing
something, but not sure what. This is the first time I've played with
files in PHP.

i.e.

<Option Value="155">Roy, P
<Option Value="77">Theodore
<Option Value="26">Thibault<Option Value="68">Worrel

-----Original Message-----
From: Curt Zirzow [mailto:curt@zirzow.dyndns.org]
Sent: July 18, 2003 1:50 AM
To: PHP
Subject: Re: [php] Still problem with files


Curt Zirzow <curt@zirzow.dyndns.org> wrote:
> Beauford.2005 <beauford.2005@rogers.com> wrote:
> >
> > i.e
> >
> > $p = "<Option Value=\"131\">Alfredsson";


And watch out for case sensitive issues
'Option' != 'option'

Curt
--

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply With Quote
  #3 (permalink)  
Old 07-18-2003
Jason Wong
 
Posts: n/a
Default Re: [PHP] Still problem with files

On Thursday 17 July 2003 12:18, Beauford.2005 wrote:

> Sorry for the extra emails, but I thought I had this licked, but now
> other problem pop up. As I said, I have a file with many lines like
> <Option Value="3">Alfredsson in it. I have been able to read the file
> into an array and display it, but I am trying to do a comparison IF
> statement and no matter what I try I can't figure out what to compare.
> To explain better here is my code so far.
>
> $filename = "players.inc";
>
> $fd = fopen ($filename, "r");
> while (!feof ($fd)) {
> $buffer = fgets($fd);
> $list[] = $buffer;
> }
> fclose ($fd);


Using file() instead of the above would be much easier.

> for($i = 0; $i < 131; $i++) {
> if($list[$i] != ????????? {
> echo "<BR>".htmlspecialchars($list[$i]); }
> }
>
> So using the example above what would I use where the question marks are
> if I wanted to display everything but this line - <Option
> Value="3">Alfredsson. I have tried a variety of ways to do this:


If you're going to be using file() then:

if($list[$i] != "<Option> Value=\"3\">Alfredsson\n";

Note the \n, substitute for whatever CR/LF characters are present in your
file because file() leaves them intact.

--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
Don't expect people to keep in step--it's hard enough just staying in line.
*/

Reply With Quote
  #4 (permalink)  
Old 07-18-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Still problem with files

Beauford.2005 <beauford.2005@rogers.com> wrote:
> Hi,


Hello

>
> Sorry for the extra emails, but I thought I had this licked, but now
> other problem pop up. As I said, I have a file with many lines like
> <Option Value="3">Alfredsson in it. I have been able to read the file
> into an array and display it, but I am trying to do a comparison IF
> statement and no matter what I try I can't figure out what to compare.
> To explain better here is my code so far.
>
> $filename = "players.inc";
>
> $fd = fopen ($filename, "r");
> while (!feof ($fd)) {
> $buffer = fgets($fd);
> $list[] = $buffer;
> }
> fclose ($fd);


Have you looked at the file() function? it does all this work for you.
http://php.net/file


>
> for($i = 0; $i < 131; $i++) {
> if($list[$i] != ????????? {
> echo "<BR>".htmlspecialchars($list[$i]); }
> }
>
> So using the example above what would I use where the question marks are
> if I wanted to display everything but this line - <Option
> Value="3">Alfredsson. I have tried a variety of ways to do this:


Be careful with loops like this you might want to use the foreach
statement in this case

http://us3.php.net/foreach

>
> i.e
>
> $p = "<Option Value=\"131\">Alfredsson";
>
> for($i = 0; $i < 131; $i++) {
> if(htmlspecialchars($list[$i]) != $p) {
> echo "<BR>".htmlspecialchars($list[$i]);
> }
> }


first of you dont want to compare the value with the htmlspecialchar()
function, only use that to translate html chars like < to &lt; so the
browser is will display the html code instead of rendering it.

second, the fgets() you used above still has the carriage return on it
so you will need to chomp that off or add it to your compare string.

http://php.net/trim

>
> This is just a sample script that I want to get running before I
> actuaaly put it into use, so any help is appreciated.


I hope that helps you getting it running.

>
>


Curt
--

Reply With Quote
  #5 (permalink)  
Old 07-18-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Still problem with files

Jason Wong <php-general@gremlins.biz> wrote:
> On Thursday 17 July 2003 12:18, Beauford.2005 wrote:
> >
> > So using the example above what would I use where the question marks are
> > if I wanted to display everything but this line - <Option
> > Value="3">Alfredsson. I have tried a variety of ways to do this:

>
> If you're going to be using file() then:
>
> if($list[$i] != "<Option> Value=\"3\">Alfredsson\n";
>
> Note the \n, substitute for whatever CR/LF characters are present in your
> file because file() leaves them intact.


I would recomend using
if(trim($list[$i]) != "<Option> Value=\"3\">Alfredsson")

it will handle the any problems with DOS or UNIX text files.

Curt.
--

Reply With Quote
  #6 (permalink)  
Old 07-18-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Still problem with files

Curt Zirzow <curt@zirzow.dyndns.org> wrote:
> Beauford.2005 <beauford.2005@rogers.com> wrote:
> >
> > i.e
> >
> > $p = "<Option Value=\"131\">Alfredsson";


And watch out for case sensitive issues
'Option' != 'option'

Curt
--
Reply With Quote
  #7 (permalink)  
Old 07-18-2003
Skate
 
Posts: n/a
Default Re: [PHP] Still problem with files

have you tried \r\n ?

----- Original Message -----
From: "Beauford.2005" <beauford.2005@rogers.com>
To: "'Curt Zirzow'" <curt@zirzow.dyndns.org>; "'PHP'"
<php-general@lists.php.net>
Sent: Thursday, July 17, 2003 3:40 PM
Subject: RE: [php] Still problem with files


> For whatever reason your suggestions still caused me problems, but I did
> come up with a solution that works.
>
> if (preg_match ("/Alfredsson/", $buffer))
>
> Another question though regarding \n. What I'm doing here is reading the
> file, omitting one entry, adding another, and saving it back to the
> file. When I add the entry and save it back to the file it appears
> beside the last entry instead of below it. How do I get it so it is
> below it. I have tried using a \n but no go. I am probably missing
> something, but not sure what. This is the first time I've played with
> files in PHP.
>
> i.e.
>
> <Option Value="155">Roy, P
> <Option Value="77">Theodore
> <Option Value="26">Thibault<Option Value="68">Worrel
>
> -----Original Message-----
> From: Curt Zirzow [mailto:curt@zirzow.dyndns.org]
> Sent: July 18, 2003 1:50 AM
> To: PHP
> Subject: Re: [php] Still problem with files
>
>
> Curt Zirzow <curt@zirzow.dyndns.org> wrote:
> > Beauford.2005 <beauford.2005@rogers.com> wrote:
> > >
> > > i.e
> > >
> > > $p = "<Option Value=\"131\">Alfredsson";

>
> And watch out for case sensitive issues
> 'Option' != 'option'
>
> Curt
> --
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>



Reply With Quote
  #8 (permalink)  
Old 07-18-2003
Beauford.2005
 
Posts: n/a
Default RE: [PHP] Still problem with files

Yep, but am I doing it right I don't know. This is the line that will be
written to the database (there will be 130 of them).

$p = "<Option Value=\"131\">Player Name";

So where do I put the \r\n. I have tried just \n at the end, but same
thing.

$p = "<Option Value=\"131\">Player Name\n";

TIA

-----Original Message-----
From: skate [mailto:root@fatcuban.com]
Sent: July 18, 2003 11:18 AM
To: Beauford.2005; 'Curt Zirzow'; 'PHP'
Subject: Re: [php] Still problem with files



have you tried \r\n ?

----- Original Message -----
From: "Beauford.2005" <beauford.2005@rogers.com>
To: "'Curt Zirzow'" <curt@zirzow.dyndns.org>; "'PHP'"
<php-general@lists.php.net>
Sent: Thursday, July 17, 2003 3:40 PM
Subject: RE: [php] Still problem with files


> For whatever reason your suggestions still caused me problems, but I
> did come up with a solution that works.
>
> if (preg_match ("/Alfredsson/", $buffer))
>
> Another question though regarding \n. What I'm doing here is reading
> the file, omitting one entry, adding another, and saving it back to
> the file. When I add the entry and save it back to the file it appears


> beside the last entry instead of below it. How do I get it so it is
> below it. I have tried using a \n but no go. I am probably missing
> something, but not sure what. This is the first time I've played with
> files in PHP.
>
> i.e.
>
> <Option Value="155">Roy, P
> <Option Value="77">Theodore
> <Option Value="26">Thibault<Option Value="68">Worrel
>
> -----Original Message-----
> From: Curt Zirzow [mailto:curt@zirzow.dyndns.org]
> Sent: July 18, 2003 1:50 AM
> To: PHP
> Subject: Re: [php] Still problem with files
>
>
> Curt Zirzow <curt@zirzow.dyndns.org> wrote:
> > Beauford.2005 <beauford.2005@rogers.com> wrote:
> > >
> > > i.e
> > >
> > > $p = "<Option Value=\"131\">Alfredsson";

>
> And watch out for case sensitive issues
> 'Option' != 'option'
>
> Curt
> --
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply With Quote
  #9 (permalink)  
Old 07-19-2003
Jason Wong
 
Posts: n/a
Default Re: [PHP] Still problem with files

On Saturday 19 July 2003 04:58, Beauford.2005 wrote:
> Yep, but am I doing it right I don't know. This is the line that will be
> written to the database (there will be 130 of them).
>
> $p = "<Option Value=\"131\">Player Name";
>
> So where do I put the \r\n. I have tried just \n at the end, but same
> thing.
>
> $p = "<Option Value=\"131\">Player Name\n";


It beats me why you're not storing the info in a DB rather than a file.

Anyway here's some psuedo-code:



$file = file('file);
foreach ($file as $line => $data) {
$file[$line] = trim($data);
if (!strcmp($what_you_are_looking_for, $file[$line])) {
// found, do your replacement stuff
}
}

implode('choose a suitable EOL character(s)', $file);
write_out_file();


--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
GOOD-NIGHT, everybody ... Now I have to go administer FIRST-AID to my
pet LEISURE SUIT!!
*/

Reply With Quote
  #10 (permalink)  
Old 07-20-2003
Jason Wong
 
Posts: n/a
Default Re: [PHP] Still problem with files

On Monday 21 July 2003 00:23, Beauford.2005 wrote:
> These files were already done from last year, so I figured I would just
> carry on with them. Last year they did not have to be manipulated - so
> it wasn't a big deal. I have since changed the whole thing and am now
> using a database (took me a whole 45 minutes), which I was using anyway
> to hold all my other info. I'd still be curious though as to why the
> other way did not work.


If it's not obvious from examining the code then you have to get down to some
serious debugging. Most importantly you need to:

- incorporate error checking into your code, examine the values returned from
all functions

- var_dump() all your major variables and verify that they are what you
expect them to be

--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
What's all this brouhaha?
*/

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:21 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0