newbie array question

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
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

newbie array question

Post 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.
Last edited by plankguy on Fri Oct 07, 2005 3:41 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$listAddress = array_shift($listings_file);
$listDescription = implode('',$listings_file);
:?:
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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. ^^;
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post by plankguy »

How would I go about ignoring the first line and only posting the 2nd line to infinity??

Any thoughts?

Thanks again!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

exactly like I posted originally, except you don't have to store the return from array_shift() ;)
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post 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>';
}
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post 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. :)
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post 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
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post 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! :)
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post 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! :(
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post 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!
Post Reply