Page 2 of 11
Re: Newb advice
Posted: Fri Sep 04, 2009 2:31 pm
by califdon
MiniMonty wrote:LOL - don't take the mick - I'm really new to this !
jackpf obviously understood you, but that's a new expression for me (being an old, brash American), and I'd seriously enjoy knowing what that means. I tried Wikipedia and I Googled it and found lots of instances, but no definition.
Re: Newb advice
Posted: Fri Sep 04, 2009 2:52 pm
by MiniMonty
Jack - it works a treat, cheers.
Next step...
So I now have a directory with 1.jpg, 2.jpg, 3.jpg etc.,
The front end of this monster will eventually be built in Flash which will want
to import an array which looks like this:
"photos = ["portraits/1.jpg", "portraits/2.jpg", "portraits/3.jpg", "portraits/4.jpg", "portraits/5.jpg"];"
(unless anyone has a better idea / solution of course)
Any advice / pointers / links on how to take the contents of my directory and build the list into an array ?
Best wishes
Monty
Re: Newb advice
Posted: Fri Sep 04, 2009 2:55 pm
by Mirge
MiniMonty wrote:Jack - it works a treat, cheers.
Next step...
So I now have a directory with 1.jpg, 2.jpg, 3.jpg etc.,
The front end of this monster will eventually be built in Flash which will want
to import an array which looks like this:
"photos = ["portraits/1.jpg", "portraits/2.jpg", "portraits/3.jpg", "portraits/4.jpg", "portraits/5.jpg"];"
(unless anyone has a better idea / solution of course)
Any advice / pointers / links on how to take the contents of my directory and build the list into an array ?
Best wishes
Monty
You already HAVE access to that array that's returned from glob()...
php > print_r(glob("/home/mike/Pictures/*"));
Array
(
[0] => /home/mike/Pictures/car1.jpg
[1] => /home/mike/Pictures/car2.jpg
[2] => /home/mike/Pictures/car3.jpg
[3] => /home/mike/Pictures/car4.jpg
[4] => /home/mike/Pictures/car5.jpg
[5] => /home/mike/Pictures/car6.jpg
[6] => /home/mike/Pictures/car7.jpg
[7] => /home/mike/Pictures/car8.jpg
[8] => /home/mike/Pictures/car9.jpg
)
php >
Re: Newb advice
Posted: Fri Sep 04, 2009 3:07 pm
by jackpf
You have a list of files from glob(), like I posted before.
califdon wrote:MiniMonty wrote:LOL - don't take the mick - I'm really new to this !
jackpf obviously understood you, but that's a new expression for me (being an old, brash American), and I'd seriously enjoy knowing what that means. I tried Wikipedia and I Googled it and found lots of instances, but no definition.
Haha, you americans....
http://www.peevish.co.uk/slang/t.htm#take_the_mickey
I suggest you brush up on your british slang
EDIT
Mirge, just saw your post - yeah, basically the same idea.
Re: Newb advice
Posted: Fri Sep 04, 2009 3:19 pm
by MiniMonty
Mirge wrote:MiniMonty wrote:Jack - it works a treat, cheers.
Next step...
So I now have a directory with 1.jpg, 2.jpg, 3.jpg etc.,
The front end of this monster will eventually be built in Flash which will want
to import an array which looks like this:
"photos = ["portraits/1.jpg", "portraits/2.jpg", "portraits/3.jpg", "portraits/4.jpg", "portraits/5.jpg"];"
(unless anyone has a better idea / solution of course)
Any advice / pointers / links on how to take the contents of my directory and build the list into an array ?
Best wishes
Monty
You already HAVE access to that array that's returned from glob()...
php > print_r(glob("/home/mike/Pictures/*"));
Array
(
[0] => /home/mike/Pictures/car1.jpg
[1] => /home/mike/Pictures/car2.jpg
[2] => /home/mike/Pictures/car3.jpg
[3] => /home/mike/Pictures/car4.jpg
[4] => /home/mike/Pictures/car5.jpg
[5] => /home/mike/Pictures/car6.jpg
[6] => /home/mike/Pictures/car7.jpg
[7] => /home/mike/Pictures/car8.jpg
[8] => /home/mike/Pictures/car9.jpg
)
php >
Sweet as a nut. ON a step curve here but learning a lot today. Thanks.
Off to do some reading now to learn how to strip out all the [0] => bits and bobs and format the data for easy-reading in Flash.
Any pointers to good tutorial sites (I can't let you very helpful people write the whole thing for me) !
EDIT: One last question - can I name the array to identify it when it's imported to Flash ?
Or am I wiser to print it into a separate .txt file and import from there ?
Any opinions ?
Best wishes
Monty
Re: Newb advice
Posted: Fri Sep 04, 2009 3:23 pm
by jackpf
I don't know how flash works, but you don't need to use print_r() to display an array. I think you want implode().
Re: Newb advice
Posted: Fri Sep 04, 2009 3:34 pm
by Mirge
MiniMonty wrote:Mirge wrote:MiniMonty wrote:Jack - it works a treat, cheers.
Next step...
So I now have a directory with 1.jpg, 2.jpg, 3.jpg etc.,
The front end of this monster will eventually be built in Flash which will want
to import an array which looks like this:
"photos = ["portraits/1.jpg", "portraits/2.jpg", "portraits/3.jpg", "portraits/4.jpg", "portraits/5.jpg"];"
(unless anyone has a better idea / solution of course)
Any advice / pointers / links on how to take the contents of my directory and build the list into an array ?
Best wishes
Monty
You already HAVE access to that array that's returned from glob()...
php > print_r(glob("/home/mike/Pictures/*"));
Array
(
[0] => /home/mike/Pictures/car1.jpg
[1] => /home/mike/Pictures/car2.jpg
[2] => /home/mike/Pictures/car3.jpg
[3] => /home/mike/Pictures/car4.jpg
[4] => /home/mike/Pictures/car5.jpg
[5] => /home/mike/Pictures/car6.jpg
[6] => /home/mike/Pictures/car7.jpg
[7] => /home/mike/Pictures/car8.jpg
[8] => /home/mike/Pictures/car9.jpg
)
php >
Sweet as a nut. ON a step curve here but learning a lot today. Thanks.
Off to do some reading now to learn how to strip out all the [0] => bits and bobs and format the data for easy-reading in Flash.
Any pointers to good tutorial sites (I can't let you very helpful people write the whole thing for me) !
EDIT: One last question - can I name the array to identify it when it's imported to Flash ?
Or am I wiser to print it into a separate .txt file and import from there ?
Any opinions ?
Best wishes
Monty
I was only using print_r() to show that it was an array and to display its contents. You can do whatever you want with the PHP array. If you are wanting a JSON-type format, there are plenty of ways to accomplish that.
EDIT:
If you're just wanting it in that simple of a format, do something like:
Code: Select all
<?php
$filesArray = glob("/home/mike/Pictures/*");
$filesString = '[';
$count = 0;
foreach($filesArray as $filename) {
if($count > 0) {
$filesString .= ", ";
}
$filesString .= "\"$filename\"";
$count++;
}
$filesString .= ']';
print "$filesString\n";
?>
Which outputs (meaning $fileString contains):
["/home/mike/Pictures/car1.jpg", "/home/mike/Pictures/car2.jpg", "/home/mike/Pictures/car3.jpg", "/home/mike/Pictures/car4.jpg", "/home/mike/Pictures/car5.jpg", "/home/mike/Pictures/car6.jpg", "/home/mike/Pictures/car7.jpg", "/home/mike/Pictures/car8.jpg", "/home/mike/Pictures/car9.jpg"]
Not the most elegant code, but it works.
Re: Newb advice
Posted: Fri Sep 04, 2009 4:09 pm
by MiniMonty
That output is exactly what I'm after - only trouble is that I copied your code, ran it on the server and got
"Unexpected T_Variable on line 3"
Any ideas?
Re: Newb advice
Posted: Fri Sep 04, 2009 4:12 pm
by Mirge
MiniMonty wrote:I was only using print_r() to show that it was an array and to display its contents. You can do whatever you want with the PHP array. If you are wanting a JSON-type format, there are plenty of ways to accomplish that.
EDIT:
If you're just wanting it in that simple of a format, do something like:
Code: Select all
<?php
$filesArray = glob("/home/mike/Pictures/*");
$filesString = '[';
$count = 0;
foreach($filesArray as $filename) {
if($count > 0) {
$filesString .= ", ";
}
$filesString .= "\"$filename\"";
$count++;
}
$filesString .= ']';
print "$filesString\n";
?>
Which outputs (meaning $fileString contains):
["/home/mike/Pictures/car1.jpg", "/home/mike/Pictures/car2.jpg", "/home/mike/Pictures/car3.jpg", "/home/mike/Pictures/car4.jpg", "/home/mike/Pictures/car5.jpg", "/home/mike/Pictures/car6.jpg", "/home/mike/Pictures/car7.jpg", "/home/mike/Pictures/car8.jpg", "/home/mike/Pictures/car9.jpg"]
Not the most elegant code, but it works.
That output is
exactly what I'm after - only trouble is that I copied your code, ran it on the server and got
"Unexpected T_Variable on line 3"
Any ideas?[/quote]
Paste your code, so we can see what lines 1-10 are at least.
Re: Newb advice
Posted: Fri Sep 04, 2009 4:15 pm
by MiniMonty
Same as yours...
Code: Select all
<?php
$filesArray = glob("/path/to/directory/*");
$filesString = '[';
$count = 0;
foreach($filesArray as $filename) {
if($count > 0) {
$filesString .= ", ";
}
$filesString .= "\"$filename\"";
$count++;
}
$filesString .= ']';
print "$filesString\n";
?>
Re: Newb advice
Posted: Fri Sep 04, 2009 4:17 pm
by Mirge
MiniMonty wrote:Same as yours...
Code: Select all
<?php
$filesArray = glob("/path/to/directory/*");
$filesString = '[';
$count = 0;
foreach($filesArray as $filename) {
if($count > 0) {
$filesString .= ", ";
}
$filesString .= "\"$filename\"";
$count++;
}
$filesString .= ']';
print "$filesString\n";
?>
Then it's somewhere else in your code, because I copy/pasted *that*... changed the path to /home/mike/Pictures/*, and ran it... and it gave the expected output:
["/home/mike/Pictures/car1.jpg", "/home/mike/Pictures/car2.jpg", "/home/mike/Pictures/car3.jpg", "/home/mike/Pictures/car4.jpg", "/home/mike/Pictures/car5.jpg", "/home/mike/Pictures/car6.jpg", "/home/mike/Pictures/car7.jpg", "/home/mike/Pictures/car8.jpg", "/home/mike/Pictures/car9.jpg"]
Re: Newb advice
Posted: Fri Sep 04, 2009 4:30 pm
by MiniMonty
That's really frustrating because I copy/pasted it, ran it as a stand-alone file and I get
"Parse error: syntax error, unexpected T_VARIABLE in /blah/blah/blah/nicearray.php on line 3"
Could it be something to do with the server (it's Linux) or the version of php ?
If the code is identical (and it is save for the path) it must be some other factor.
Any ideas ? Any tests I can do ?
I'm really grateful to you lot for all your help by the way - I thought this would take a week and
so far it's taken a day and I've got a lot further than I'd hoped.
Re: Newb advice
Posted: Fri Sep 04, 2009 4:33 pm
by jackpf
Btw, you could do this in like one line if you use implode().
Re: Newb advice
Posted: Fri Sep 04, 2009 4:39 pm
by Mirge
jackpf wrote:Btw, you could do this in like one line if you use implode().
I thought about that first, using implode/join, but couldn't find out a way to actually wrap the filenames with quotes, and have an ending comma... using only implode/join.
Re: Newb advice
Posted: Fri Sep 04, 2009 4:43 pm
by MiniMonty
jackpf wrote:Btw, you could do this in like one line if you use implode().
Forgive the pun but... care to expand on that ?