Closed Thread Icon

Topic awaiting preservation: a real wild one - JS to PHP to JS (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=12709" title="Pages that link to Topic awaiting preservation: a real wild one - JS to PHP to JS (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: a real wild one - JS to PHP to JS <span class="small">(Page 1 of 1)</span>\

 
smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 05-05-2003 02:18

Ok I have this js file as part of my site that selects a random artist from a list, then retrieves a random image from that artist's directory on my site from a maximum of 5 images -

code:
Math.floor(Math.random()*5)



Now what I want to do is to use PHP to find out how many possible images are in the javascript chosen directory and if there is more than 5 then let the javascript choose from that new number. All the possible images will be named thumb[number].jpg where [number] is a range starting at 0 (e.g. thumb0.jpg, thumb1.jpg etc), images named something else should be ignored.

I figure that this will involve passing the directory variable from JS to a PHP script which looks for the number of images and then returns that number to the JS for it to continue doing it's thang.

Please help if you can.

visit my CryoKinesis Online Gallery

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 05-05-2003 02:36

smonkey: PHP can do random numbers - why not use PHP for the whole thing?

___________________
Emps

FAQs: Emperor

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 05-05-2003 14:36
quote:
why not use php for the whole thing?



Well I have spent ages making this cool javascript thing that does it all for more, and then I recently decided that it would be better if the number of images wasn't set at 5 but was instead only limited by the number of possible images in the artist's directory that had been selected at random.

I know that the whole system would work better with php, but my site is built around using javascript to make it 'pseudo-dynamic', it is therefore a bit of a kick in the teeth to do it again using php (which incidently I have never used before).

One of the things I really like about using javascript to add sorta dynamism to a site is that because you can reuse so much of the scripts you can make you site so small and make it load so fast because the bulk of html code is written client side and doesn't have to be transmitted across the net, I know it isn't an ideal solution and php is a much neater and more professional solution but I haven't got as far as learning it yet, I've only recently started with javascript.

visit my CryoKinesis Online Gallery

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 05-05-2003 15:12

smonkey: Well OK - I'm not to sure of what is happening.

The page loads and a user selects the directory and you then want PHP to go off and find the number of images return them and then get the JS to do the random number thing?

If so its going to get messy and you might want to use a hidden frame and read and write to hidden inputs in there or something. Otherwise I suspect you'll have to be reloading the page a couple of times.

Hmmmmmmmmmm it might be possible to dynamically draw a script in through the script tags and have the script drawn in be a PHP one which returns the number of files.

I'm sure there is a better way than those messy ways above and some clever person will be here in a minute to sort it out

___________________
Emps

FAQs: Emperor

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 05-05-2003 15:55

Looks like it is something like opendir() you're looking for.

With that command you can open a directory and loop trough all the files in it. For each .jpg or .something else it finds in the directory increment a counter. When it has looped through all the files, the counter will hold the amount of pictures in the directory.

To use this value in js, do something like this:

code:
<script language="javascript">
...
var n = <?php getNum() ?>;

Math.floor(Math.random()*n);
...
</script>


The php function getNum() just returns the number you previously got when counting the images in the directory. I can't guarantee that it works just like that, but the general idea should be right...

If you want to chose the directory used with javascript you could pass that variable through the url like Ini mention in
this thread
.

But like Emps said; the best solution would probably be to do everything serverside.

_________________________
"There are 10 kinds of people; those who know binary, those who don't and those who start counting at zero"

[This message has been edited by Veneficuz (edited 05-05-2003).]

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 05-05-2003 16:03

all sounding very complicated - i agree all php would be the way to go if it's gonna be this awkward. Thanks for all your help, I guess I'll leave the php site redesign til next year when I have learnt more about it.

visit my CryoKinesis Online Gallery

« BackwardsOnwards »

Show Forum Drop Down Menu