Page 1 of 1

newbie array question

Posted: Fri Oct 07, 2005 3:16 pm
by plankguy
Hi, this is probably going to be really easy to resolve but i seem to be having a helluva time getting it.

I have text files that store an address and a description in each file. I am trying to display the contents of those files, but I cannot seem to get the entire description to display but only the first line [1]. I there a way to have a range of '$listDescription' lines displayed?? Say line [1] ---> [the last line]???

here's asnippet of what i have.

Code: Select all

array_multisort( $listings_array, SORT_NUMERIC, SORT_DESC ); 
array_splice( $listings_array, $display ); 

$num_listings = ( count( $listings_array ) ); 


foreach ( $listings_array as $val ) 
{ 
$listings_file = file("listings/$val"); 
$listAddress = $listings_file[0];
$listDescription = $listings_file[1];

echo '<b>'.$listAddress.'</b>'; 
echo $listDescription.'<br>';
}
thanks in advance.

Posted: Fri Oct 07, 2005 3:28 pm
by feyd

Code: Select all

$listAddress = array_shift($listings_file);
$listDescription = implode('',$listings_file);
:?:

Posted: Fri Oct 07, 2005 3:30 pm
by Skara
use

Code: Select all

tags instead of

Code: Select all

. 

Well, one you could use file_get_contents and preg_replace the first line.  Two, you could use implode().  Three, you could simply loop through the remaining lines and concat them.

Edit:  feyd beat me, and with a better solution of course. ^^;

Posted: Fri Oct 07, 2005 3:48 pm
by plankguy
Nice thanks....

It seems to be giving me all the data on one line:
This is a great property: -1200sq ft of floor space -Internet ready -Full Kitchen Please contact Doug for more info.
whereas i'd like to be able to get it onto multiple lines like this:
This is a great property:
-1200sq ft of floor space
-Internet ready
-Full Kitchen

Please contact Doug for more info.
Any ideas?? :oops:

Posted: Fri Oct 07, 2005 3:58 pm
by feyd

Posted: Sat Oct 08, 2005 11:50 am
by plankguy
How would I go about ignoring the first line and only posting the 2nd line to infinity??

Any thoughts?

Thanks again!

Posted: Sat Oct 08, 2005 11:57 am
by feyd
exactly like I posted originally, except you don't have to store the return from array_shift() ;)

Posted: Sun Oct 09, 2005 3:22 pm
by plankguy
Thanks alot for the help, but of course, I have another question. I am trying to filter the files depending on what page the user is on. So i've added a line that has a name that corresponds to the page that it should be displayed on, but is not actually listed on the page. I'm trying to write an 'if' statement that filters the files, but with no luck.

In plain english this is what I what i'm trying to get at:

Code: Select all

if (the first line of the text file is "dave"){
        display this listing;
}
else{
do not display this listing; //if the name is john, steve, or anything else
}
hope that makes sense. I'm sure it can't be too hard, i'll just be damned if I can make anything werk. :?

FYI: this is what I have currently:

Code: Select all

array_multisort( $listings_array, SORT_NUMERIC, SORT_DESC ); 
array_splice( $listings_array, $display ); 
$num_listings = ( count( $listings_array ) ); 

foreach ( $listings_array as $val ) 
{ 
$listings_file = file("listings/$val"); 

$listMembers = array_shift($listings_file);
$listAddress = array_shift($listings_file);
$listDescription = implode('<BR>',$listings_file); 


echo '<b>'.$listAddress.'</b>'; 
echo $listDescription.'<br><br>';
}

Posted: Sun Oct 09, 2005 6:17 pm
by Dm7
Why not something like that...

Code: Select all

array_multisort( $listings_array, SORT_NUMERIC, SORT_DESC );
array_splice( $listings_array, $display );
$num_listings = ( count( $listings_array ) );

if ($name == "Dave") 
{
    foreach ( $listings_array as $val )
    {
    $listings_file = file("listings/$val");

    $listMembers = array_shift($listings_file);
    $listAddress = array_shift($listings_file);
    $listDescription = implode('<BR>',$listings_file);


    echo '<b>'.$listAddress.'</b>';
    echo $listDescription.'<br><br>';
    } 
}
else
{
    echo "You don't have proper permissions to access those information.";
}


Of course it's a basic version... I hope that's what you meant? Of course you'll have to set $name variable to something like $name = $_POST['name']; or something like that. :)

Posted: Sun Oct 09, 2005 10:12 pm
by plankguy
so if I wanted line 1 to = $name I would have???

My god i'm bad at this...Only one way to learn I guess.

Thnx

Posted: Sun Oct 09, 2005 10:25 pm
by Dm7
plankguy wrote:so if I wanted line 1 to = $name I would have???

My god i'm bad at this...Only one way to learn I guess.

Thnx
you can put $name anywhere just BEFORE the if/else statement because $name have to be defined first! :)

Posted: Mon Oct 10, 2005 12:20 am
by plankguy
I know where i can put $name anywhere just BEFORE the if/else statement, I just can't identify $name as line 1 of the file...

I am trying to identify $name like this:

Code: Select all

$name = $listings_file[0];
but "no permission" after is goes through the if/else statements....

Arghhhh! :(

Posted: Mon Oct 10, 2005 11:20 am
by plankguy
I think I got it...maybe not the best way, but it works:

Code: Select all

array_multisort( $listings_array, SORT_NUMERIC, SORT_DESC ); 
array_splice( $listings_array, $display ); 
$num_listings = ( count( $listings_array ) ); 

foreach ( $listings_array as $val ) { 
	$listings_file = file("listings/$val"); 
	
$name = $listings_file[0];

	if ($name[0] == "d"){
		$listMembers = array_shift($listings_file);
		$listAddress = array_shift($listings_file);
		$listDescription = implode('<BR>',$listings_file); 

		echo '<b>'.$listAddress.'</b>'; 
		echo $listDescription.'<br><br>';
	}
}
So i'm just getting the $name variable like I had before, but comparing to only the first character because the entire name wouldn't work for some reason....

Thanks for everyone's help!