Page 1 of 1

Super noob has a question

Posted: Wed Dec 31, 2003 8:18 pm
by hurdy gurdy
Hi, thanks for taking a moment to look over my very silly and noobish question.

First, a disclaimer, I have no programming experience, I am just now learning. If that didn't scare you off, then I'll get on with my question. :)

The purpose of this snippet of code is to have a script that open a text file, read the contents, break each line into an array key, then display each key in a for loop.

Heres what I've written:

Code: Select all

$openlist = fopen("content/list.txt", 'r'); //opens the file
	if (!$openlist)
	{
		echo '<strong>Whoopsie!  Cant open list.txt!</strong>'; //no file!
		exit;
	&#125;
	
	$list = fgetcsv ($openlist, 100, "\n"); // supposed to break the list into array elements by line breaks
	$num = count($list); // checking to see how many keys are made
	
	echo ' how many keys there are in the $list array = '.$num.'<br>'."\n";
	
	for ($i = 0; $i <= $num; $i++)
	&#123;
		echo $list&#1111;$i].' this is key '.$i.'<br>'; // show me the keys
	&#125;
	
	echo '<p>&nbsp;</p>';
	fpassthru ($openlist);  //making sure the list was opened 
	fclose ($openlist);  //closing it 
	echo '<p>&nbsp;</p>';
...and heres what the contents of list.txt looks like:

Code: Select all

03-12-30.txt
03-12-18.txt
03-12-12.txt
03-11-16.txt
03-11-02.txt
03-09.30.txt
I created the list.txt in notepad and uploaded that seperately (if thats important...)

and finally, this is what is output:
Image

I guess the problem is obvious. First of all the function of fgetcsv doesn't seem to recognize any lines past the first one and does not break them out to array keys (am I using the term "keys" correctly?).

Thanks in advance for any help you all can offer... I hope to be as good as you all someday. :)

Posted: Wed Dec 31, 2003 9:04 pm
by m3rajk
are you learning on your own? are you using any books to help? this seems rather complex for a first project. classes/books build up to something like this.

it looks like you have the idea of how to use parts of this correctly so far, but not all of it.

my advice is to get a tutorial and start off withthe basic "hello world" and build up until you have used several types of loops.

and fyi: key is a erm in associative arrays becasue instead of indicies, to acess that data one uses a name. in that case i what' read in is the data, so it's a value, not a key.

the reason i'm not actually giving anything else is because if you haven't done that you will learn far more by doing that and in a better way since it builds up, and from the looks of it you will breaze through until you get to this because the concepts are there to a good degree and there looks to be a good understanding

Posted: Wed Dec 31, 2003 9:10 pm
by Gen-ik
Not sure if this will work (I'm a wee bit drunk!)

Code: Select all

<?php

ob_start();
readfile("content/list.txt");
$file = ob_get_contents();
ob_end_clean();

$list = explode("\r\n", $file);

foreach($list as $value)
echo $value."<br />";

?>

Posted: Thu Jan 01, 2004 1:12 am
by JAM
Or...

Code: Select all

<?php
    $arr = file('content/list.txt');
    foreach ($arr as $key => $val) {
        echo 'Key: '.$key.' - Value: '.trim($val).'<br>';
    }
?>
(used trim to erase the newlines)

Muchas Gracias!

Posted: Thu Jan 01, 2004 8:26 am
by hurdy gurdy
Thanks everyone! Using foreach worked like a charm, however, could someone point out where I went wrong in my initial script?

To answer your questions, I am teaching myself based on a few books I found in local stores. All of the books seem to gloss over the fundamentals, then they run screaming into classes and OOP. lol

Believe me when I say that posting here was a last resort, I was trying very hard to find out what I was doing wrong on my own, but to no avail.

Again, thank you all so much and any pointers you can offer would be greatly appreciated!

hg

Posted: Thu Jan 01, 2004 9:15 am
by JAM
fgetcsv is personally not something I'd use as it's (for me) more for csv files. I'm not confident enough with to say it's bad or good though.

And without further testing the error lies somewhere there, as you get a count(array) result of 2, witch is of course bad as we see at least 6 rows of text in the file. And as that was broken, the for loop was not giving the expected result.

Not much of explanation perhaps, but... =)

Happy coding.

Ps.
Personal favourite as rerun: http://php.net/tips.php for some info on how to make your browser a good manual-lookup tool.

Posted: Thu Jan 01, 2004 12:51 pm
by hurdy gurdy
Thanks again!

I got another question for those with the patience. :)

The content of the list.txt is:

Code: Select all

03-12-30.txt 
03-12-18.txt 
03-12-12.txt 
03-11-16.txt 
03-11-02.txt 
03-09.30.txt
by using foreach, it breaks each line into values in an array...

Code: Select all

<?php 
    // breaking the content list into an array
    $list = file('content/list.txt'); 
    foreach ($list as $key => $val) 
    { 
        echo 'Key: '.$key.' - Value: '.trim($val).'<br>'; 
    } 
?>
So that :
$list[0] has the value of 03-12-30.txt
$list[1] has the value of 03-12-18.txt
$list[2] has the value of 03-12-12.txt

and so on and so on, etc...

Now, when I add this bit of code to the above script:

Code: Select all

$entry01 = fopen ('content/'.$list[0], 'r');
	fpassthru ($entry01);
	fclose ($entry01);
It displays properly in the foreach loop, but displays nothing of my little script below. What went wrong? Please help. :(

Posted: Thu Jan 01, 2004 4:03 pm
by JAM
Because you need to trim() the $list[0] also to erase the not for the eye visible newline.

You can make use of the readfile() function also if you want to display the information onto the page as is...