Simple PHP question - (Solving problems makes you feel good)

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
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Simple PHP question - (Solving problems makes you feel good)

Post by gulline »

--

I am creating a menu in Flash MX.

From Flash, I desperately need to retrieve the contents of a folder in my root directory (MC1), strip the dot extensions (.swf) and create an Array out of the modified file names.

So when I access the .PHP script from flash, it will look at ...

Folder-MC1
item1.swf
item2.swf
item3.swf
item4.swf
item5.swf


and return ...

z = new Array("item1", "item2", "item3", "item4", "item5");

This is my first post here!
I did search this site for about an hour with no success (doesn’t mean its not here).
Any help would be greatly appreciated.

Thank you in advance,
Mike

--
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

explode()

ex:

Code: Select all

$myArray = explode(".",$myListVar);
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Post by gulline »

--

Did I mention I don’t know anything about PHP?

That looks simple enough… What do I do with it?

Let’s see…

$myArray = explode(".",$myListVar);

“.” – is this retrieving the file name up to the dot extension (item-.swf)?
$myListVar – doesn’t this need to be defined before I can explode() it?
$myArray – do I change this to “$z” if I want my new Array to be “z”?

I’m just not sure how this …
$myArray = explode(".",$myListVar);

will return this …
z = new Array("item1", "item2", "item3", "item4", "item5");

--
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You could try

Code: Select all

$z = array();
foreach (glob("MC1 /*.swf") as $filename) {
   $newvar = explode('.',$filename);
   $z[] = $newvar[0];
}
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Post by gulline »

--

This looks like it could work (what do I know?).

Let’s see . . .

$z = array();
foreach (glob("MC1 /*.swf") as $filename) {
$newvar = explode('.',$filename);
$z[] = $newvar[0];
}

glob("MC1 /*.swf") – is there supposed to be a space between “MC1” and “/”?
explode('.',$filename); – I’m still not sure what this - ‘.’ is doing.
$z[] = $newvar[0]; – wont this return the first item in the Array (z = item1)?

We are getting closer. I can feel it.

--
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

gullineglob wrote:("MC1 /*.swf") – is there supposed to be a space between “MC1” and “/”?
explode('.',$filename); – I’m still not sure what this - ‘.’ is doing.
$z[] = $newvar[0]; – wont this return the first item in the Array (z = item1)?

We are getting closer. I can feel it.

--
the space between MC1 and / doesnt matter, it is read the same (i think)
explode turns the $filename into a array by the "." so if the $filename = this.is.a.sentence then the array will look like this

Code: Select all

array('this', 'is', 'a', 'sentence');
$newvar[0] would = 'this' since its the first element in the array (and is given the key '0' because the count starts with 0)

how close are we now?
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Post by gulline »

--

We’re this close (I’m doing this with my fingers).
One last bone head question. It’s a two parter.

Can I test something like this on my local computer? Does it have to be on a server for some reason?

--
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I suggest you read this: viewtopic.php?t=21400
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Post by gulline »

--

Just a little more help.

So, this code,

Code: Select all

<?PHP
$z = array();
foreach (glob(&quote;MC1/*.swf&quote;) as $filename) {
   $newvar = explode('.',$filename);
   $z&#1111;] = $newvar&#1111;0];
   echo $z;
}
?>
returns the word “Array” for every “.swf” file in my MC1 folder
like this . . .
ArrayArrayArrayArrayArrayArrayArrayArray

What I need is . . .
“Item1”, “Item2”, “Item3”, “Item4”, “Item5”, “Item6”, “Item7”

Any ideas?

Mike

--
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

gulline wrote:...and create an Array out of the modified file names.
This is building an array, as you wanted.

Code: Select all

$z = array();
foreach (glob(&quote;MC1 /*.swf&quote;) as $filename) {
   $newvar = explode('.',$filename);
   $z&#1111;] = $newvar&#1111;0];
}
If you want to directly output your contents

Code: Select all

$z = array();
foreach (glob("MC1 /*.swf") as $filename) {
   $newvar = explode('.',$filename);
   echo $newvar[0];
}
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Post by gulline »

YES!

that wasn't my problem but it totaly helped

Code: Select all

<?PHP$z = array();
foreach (glob(&quote;MC1/*.swf&quote;) as $filename) {   
$newvar = explode('.',$filename);   
$z&#1111;] = $newvar&#1111;0];   
echo $z; <--- i should have been using echo $z&#1111;0];
}
?>
echo $z; <--- i should have been using echo $z[0];

Thanks.

Mike
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

No $z[0] is only the first array element.
You need to echo $newvar[0] for what you want.

Good luck.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

once you have the z[] array built, you'll need to loop over it to display the values from its elements.

DO THAT OUTSIDE OF YOUR CURRENT FOREACH LOOP OTHERWISE IT WILL BE COMPOUNDED.
gulline
Forum Newbie
Posts: 8
Joined: Thu May 26, 2005 2:27 pm
Location: Dana Point

Post by gulline »

--

I have this menu in flash.

http://www.mikegulline.com/z/z.html

MC2, MC3, MC4 are all pulling from these Arrays in Flash;

-- MC2.z = new Array("item1", "item2", "item3","item4");
-- MC3.z = new Array("item1", "item2", "item3","item4", "item5", "item6", "item7", "item8", "item9", "item10");
-- MC4.z = new Array("item1", "item2");


I need to define “MC1.z[]” in Flash using PHP.

The code does the right thing; it just doesn’t seem to be formatted the right way

from this php code i get -- item1/item2/item3/item4/item5
i need to have ----------- "item1", "item2", "item3","item4", "item5"

is that the same thing?

I don’t think I am calling it correctly in flash. Should I be finishing my question in a flash forum? i trust the php, it's the flash im not sure about. FLASH!


Bah!
--
Post Reply