text file into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

text file into an array

Post by paqman »

Hi there,

Is it possible to create an array from a text file? I have sort of gotten half way there. So far I have:

Code: Select all

$filename = $view.".txt";
$handle = fopen($filename, "r");
$order = array(fread($handle, filesize($filename)));
fclose($handle);

print_r($order);
That gives me one value with the contents of the file, which are a bunch of numbers seperated by commas (1, 2, 4, 5....). Is there a way to get those numbers to go into seperate values. The real purpose of this is to have the text file be a list of picture numbers, which will be taken and placed on the page. I could download a free picture gallery, but I really would like to make my own.

BTW, $view is which text file to open.

Thanks for your help!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

take a look at these functions


[php_man]file()[/php_man]
[php_man]explode()[/php_man]
ianlandsman
Forum Newbie
Posts: 24
Joined: Thu Dec 30, 2004 9:50 pm
Location: New York

Post by ianlandsman »

Yep you want the file() function
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

good but I still need help

Post by paqman »

ok I've made some progress but I am stuck again. I am able to get the pictures to be displayed in the correct order and the links work to open the larger picture. The only thing is I would like to make next/previous links in the pop up. This is what I have so far (the file_v2 is a function on the file() link you replied with). Is there some way to get the values of the previous and next in the foreach operator? I tried to use the previous() and next() functions but couldn't make it work.

Code: Select all

$filepath = "gallery/".$view.".txt";
if(file_exists($filepath)) {
$open = file_v2($filepath, 0);

foreach ($open as $key => $pic) {
echo "
<a href="indview.php?id=$key&file=$pic" target="indview">
<img src="thumb/$pic.jpg"></a> ";
}
Thanks so much for your help!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

can you post file_v2 function you wrote?
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

Here is the whole file:

Code: Select all

function file_v2 ( $filename, $return_max_lines=0, $callback_func=null, $do_rtrim=true, $buffer_size=1024 )
{
$open = fopen( $filename, 'rb' );
$open_data = array();
$line=0;

while( !feof($open) )
{
if( $do_rtrim )
{
$open_data[$line] = rtrim(fgets($open, $buffer_size));
}
else
{
$open_data[$line] = fgets($open, $buffer_size);
}
if( $callback_func != null && function_exists( $callback_func ) )
{
eval($callback_func . '($open_data[$line]);');
}
$line++; 
if( $return_max_lines > 0 )
{
if( $line >= $return_max_lines )
{
break;
}
}
}
fclose($open);
return $open_data;
}

$filepath = "gallery/".$view.".txt";
if(file_exists($filepath)) {
$open = file_v2($filepath, 0);

foreach ($open as $key => $pic) {
echo "
<a href="indview.php?id=$key&file=$pic" target="indview">
<img src="thumb/$pic.jpg"></a> ";
}
}
if(!file_exists($filepath)) {
echo "Gallery file not found: $file";
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. have indview.php look at the $_GET['id'] value match it in the file records
  2. determine if the value is valid and has a previous and/or next
  3. display a link to that fact.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Try this:

Assuming that the query string created in the first foreach loop is sent to the pop up window you could access the next and previous like this:

Code: Select all

<?php

$previous = $_GET['key'] -1;
$next = $_GET['key']+1;
$filepath = "gallery/".$view.".txt";
$open = file_v2($filepath, 0);
//Then you would build a link something like this

echo "<a href="indview.php?id=$previous&file=$open[$previous]" target="indview">
<img src="thumb/$pic.jpg"></a>";
//For the "next" link  you would replace $previous with $next

?>
Does that help?
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

I understand how to send the $id variable and recieve it. What I dont get is how to find the next and previous values in the file.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

The file is already in an indexed array. When the person clicks on the link to open the window you have the current array index stored in the query string id variable. So retrieve it and subtract one or add one.

Code: Select all

<?php
$previous = $_GET['id'] -1; // sorry on the previous version i had key in the brackets
$next = $_GET['id']+1;
?>
So than you can access the next and previous values in the $open array.
like so....

Code: Select all

<?php
$open[$previous];
$open[$next];
?>
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

Yep it worked! Thanks to everyone who helped me out!!! :D
Post Reply