Page 1 of 1
text file into an array
Posted: Fri Dec 31, 2004 6:52 pm
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!
Posted: Fri Dec 31, 2004 6:56 pm
by rehfeld
take a look at these functions
[php_man]file()[/php_man]
[php_man]explode()[/php_man]
Posted: Fri Dec 31, 2004 8:50 pm
by ianlandsman
Yep you want the file() function
good but I still need help
Posted: Sun Jan 02, 2005 3:18 pm
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!
Posted: Sun Jan 02, 2005 3:28 pm
by neophyte
can you post file_v2 function you wrote?
Posted: Sun Jan 02, 2005 3:42 pm
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";
}
Posted: Sun Jan 02, 2005 4:35 pm
by feyd
- have indview.php look at the $_GET['id'] value match it in the file records
- determine if the value is valid and has a previous and/or next
- display a link to that fact.
Posted: Sun Jan 02, 2005 4:54 pm
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?
Posted: Sun Jan 02, 2005 4:57 pm
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.
Posted: Sun Jan 02, 2005 5:13 pm
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];
?>
Posted: Sun Jan 02, 2005 5:36 pm
by paqman
Yep it worked! Thanks to everyone who helped me out!!!
