SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
-
Nov 27, 2000, 13:47 #1
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I thought that I'll have to use fread() for this, but from that I'm stuck...
I could use some help on this. Basically I want to get URL's and their description from a text file and display them apart in a table.
I would appreciate any links to tutorials on how to use (text) files with PHP as well.
Thanks for your time.www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 27, 2000, 14:08 #2
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Actually you are better off using file() for this.
I assume the text file as the urls and descriptions delimited in some fashion and one record per line? Let's for insatnce say that the records are delimited by a double pipe(||). First the file takes the text file in and puts each line into an array. Then I loop through each elemenet of the array and split it into its own array by the delimiter then I print it out in any layout that I want:
Example:
<table>
<?
$file = file("yourtextfile");
while(list($key,$val) = each ($file)) {
$data = explode("||", $val);
printf('<tr><td></td><td></td></tr>', $data[0], $data[1]);
}
?>
</table>Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 27, 2000, 15:27 #3
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, it's just me probably, but I don't understand this part:
while(list($key,$val) = each ($file)) {
$data = explode("||", $val);
printf('<tr><td></td><td></td></tr>', $data[0], $data[1]);
}
I just started coding PHP, so this is a bit far for me...
I really appreciate your help.www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 27, 2000, 15:41 #4
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Whoops Sorry I kinda forgot the %s in the printf() sattement
anyways let me try and explain it a bit more
Example:
<table>
<?
//Read the contents of the text file into an array(one line = one element of the array) http://www.php.net/file
$file = file("yourtextfile");
//Loop through the array using while loop and list() function http://www.php.net/list
while(list($key,$val) = each ($file)) {
//Split the current elemenet of the array(in other words split the current line for them text file) by the specified delimiter(in this case ||) http://www.php.net/explode
$data = explode("||", $val);
//Use printf to display the row http://www.php.net/printf
printf('<tr><td>%s</td><td>%s</td></tr>', $data[0], $data[1]);
}
?>
</table>Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 27, 2000, 16:24 #5
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can you explain to me what those pieces do:
while(list($key,$val) = each ($file)) {
and:
$data = explode("||", $val);
and the last snippet:
printf('<tr><td>%s</td><td>%s</td></tr>', $data[0], $data[1]);
I've looked them all up at php.net, but I still don't really understand them...www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 27, 2000, 16:55 #6
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Isn't there a way to get the data after the array has been split (using explode() ) into variables? This would be very convenient since I can use the data then the same way as when I pull it out of a mySQL db.
www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 27, 2000, 17:01 #7
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$file is an array, it has a key and a value each line from the text file is an elemenet of the array. So in other words each element is each line form the text file. As I stated before I don't know what your text file looks like but let's say we have a text file that looks like
http://www.yahoo.com||A full service search engine
http://www.somedomain.com||Another great website
By using $file = file("yourtextfile");
you end up with an array the first element being
$file[0] = http://www.yahoo.com||A full service search engine
and the second being:
$file[1] = http://www.somedomain.com||Another great website
where 0 and 1 are the keys and the values are the string from the text file
so now we use
while(list($key,$val) = each ($file)) {
We are looping through each element of the array separating the keys from the values and assigning them to the variable $key and $val
So we now have $val which looks like http://www.yahoo.com||A full service search engine for the first element of the array
So now I use explode() to split that up into two parts the url and the description based on the delimiter in this case ||
$data = explode("||", $val);
so now $data[0] = http://www.yahoo.com
and
$data1 = A full service search engine
Now I use the printf(); statement to display the html
the %s is a place holder for the args at the end of the line
so
printf('<tr><td>%s</td><td>%s</td></tr>', $data[0], $data[1]);
will look like this on the screen
<tr><td>www.yahoo.com</td><td>A full service search engine </td></tr>
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 27, 2000, 17:02 #8
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
IN response to your last post that is what I do in the printf() staement
I suppose you could say $url = $data[0] and
$description = $data[1];
But that would be an extra step.Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 27, 2000, 17:08 #9
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, I fully understand it now!
I'm really grateful for your help. I don't think I could have done it without you.www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 27, 2000, 17:19 #10
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm... it works, but I get this warning:
Warning: Undefined offset: 1 in c:\inetpub\wwwroot\exp5\links2\index.php on line 37
Warning: Undefined offset: 2 in c:\inetpub\wwwroot\exp5\links2\index.php on line 38
It's at the part where I use $content = $data[1] and description = $data[2].
What does it mean?www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 27, 2000, 17:32 #11
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Could you show me all the code and Its $data[0] and $data[1] arrays start at 0
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 28, 2000, 04:06 #12
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'll post the code when I'm home (I'm now at school <sigh>).
www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 28, 2000, 10:04 #13
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:<table width="100%" border="0" cellspacing="0" cellpadding="0"> <? //Read the contents of the text file into an array(one line = one element of the array). http://www.php.net/file $file = file("file.txt"); //Loop through the array using while loop and list() function. http://www.php.net/list while(list($key,$val) = each($file)) { //Split the current elemenet of the array(in other words split the current line for them text file) //by the specified delimiter(in this case //||). http://www.php.net/explode $data = explode("||", $val); /*Use printf to display the row. http://www.php.net/printf printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>", $data[0], $data[1], $data[2]); */ //split the array and create the variables $url = $data[0]; $content = $data[1]; (this is line 37) $description = $data[2]; (line 38) //print the results echo("<tr><td><a href=\"" . $url . "\" target=\"_new\">" . $url . "</a></td><td>" . $content . "</td><td>" . $description . "</td></tr>"); } ?> </table>
www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 28, 2000, 11:23 #14
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Remember that I said you would need to use the same delimiter that you used in your text file in the explode() statement what does your text file look like?
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 28, 2000, 12:14 #15
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The content of the text file (temporary, just to test):
1:http://home.support.nl/~lemmen||None, test site||Lemmen s script archive
2:http://www.zophar.net||Emulators||huge archive of emulators and more
www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 28, 2000, 12:20 #16
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There are no empty lines in your text file correct??
There are several ways to error check this you might try not assigning the variables new names and just using $data[0] $data[1] and $data[2] in your echo statement. And actually there is no need to assign them new names. but also try commenting out the echo line and use:
print count($data)."<br>";
It should print a 3 for every line in your text file. If it doesn't then there must be a problem with your text file.Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 28, 2000, 12:32 #17
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm... I get this:
4
1
4
www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
-
Nov 28, 2000, 12:40 #18
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well that certianly narrows it down. How are you putting your records into the text file?? What program did you use to put the text file together. YOU need to make sure that you put a carriage return at the end of each line. BUt there should be no blank lines just one record per line with a carriage return at the end. And are you uploading this text file to a remote server for testing? If so, make sure you transfer it in ASCII format
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Nov 28, 2000, 12:47 #19
- Join Date
- Jun 2000
- Location
- Netherlands
- Posts
- 1,356
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, somebody shoot me
It was one space too much. I created the file manually, you see...
It's now working fine. Thanks for your help!www.nyanko.ws - My web-, software- and game development company.
www.mayaposch.com - My personal site and blog.
Bookmarks