Results 1 to 10 of 10

Thread: array starting from 0

  1. #1
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy array starting from 0

    Hi, am using php & javascript for a slideshow my problem is:

    the arrays for images are:
    //define images. You can have as many as you want:
    photos[0]="photo1.jpg"
    photos[1]="photo2.jpg"
    photos[2]="photo3.jpg"

    I need that they start from (1) not 0 like this:

    photos[1]="photo1.jpg"
    photos[2]="photo2.jpg"
    photos[3]="photo3.jpg"

    If i do this the slideshow shows me and image error as first slide, whole code is below:


    <script type="text/javascript">
    var photos=new Array()
    var photoslink=new Array()
    var which=0
    //define images. You can have as many as you want:
    photos[0]="photo1.jpg"
    photos[1]="photo2.jpg"
    photos[2]="photo3.jpg"

    //Specify whether images should be linked or not (1=linked)
    var linkornot=0

    //Set corresponding URLs for above images. Define ONLY if variable linkornot equals "1"
    photoslink[1]=""
    photoslink[2]=""
    photoslink[3]=""

    //do NOT edit pass this line

    var preloadedimages=new Array()
    for (i=0;i<photos.length;i++){
    preloadedimages[i]=new Image()
    preloadedimages[i].src=photos[i]
    }


    function applyeffect(){
    if (document.all && photoslider.filters){
    photoslider.filters.revealTrans.Transition=Math.floor(Math.random()*23)
    photoslider.filters.revealTrans.stop()
    photoslider.filters.revealTrans.apply()
    }
    }



    function playeffect(){
    if (document.all && photoslider.filters)
    photoslider.filters.revealTrans.play()
    }

    function keeptrack(){
    window.status="Image "+(which+1)+" of "+photos.length
    }


    function backward(){
    if (which>0){
    which--
    applyeffect()
    document.images.photoslider.src=photos[which]
    playeffect()
    keeptrack()
    }
    }

    function forward(){
    if (which<photos.length-1){
    which++
    applyeffect()
    document.images.photoslider.src=photos[which]
    playeffect()
    keeptrack()
    }
    }

    function transport(){
    window.location=photoslink[which]
    }

    </script>
    If you could tell me how to modify it so it starts from 1 not 0 would be very appreciated!!

  2. #2
    Join Date
    Dec 2006
    Posts
    47
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default

    change the zero to one in this line...
    for (i=0;i<photos.length;i++){

  3. #3
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Kenny View Post
    change the zero to one in this line...
    for (i=0;i<photos.length;i++){
    Done, still the first slide is a image not loaded icon press the forward button to show next slide and it shows image #1, then #2, etc very well, when i press the backward button shows everything fine then goes to #0 and theres the image not loaded icon again :S

    the only diferent thing of the original code in mine is this line:

    //define images. You can have as many as you want:
    photos[1]="20061223051948.jpg"
    photos[2]="20061223063734.jpg"

    That shows the first slide with error.

  4. #4
    Join Date
    Dec 2006
    Posts
    47
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default

    Here's another zero to investigate...
    var which=0

    Try upping it to a one.

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by chakytoride View Post
    the arrays for images are:
    //define images. You can have as many as you want:
    photos[0]="photo1.jpg"
    photos[1]="photo2.jpg"
    photos[2]="photo3.jpg"

    I need that they start from (1) not 0 like this:

    photos[1]="photo1.jpg"
    photos[2]="photo2.jpg"
    photos[3]="photo3.jpg"
    I find that wholly unlikely; it's more plausible that you only think it needs to start from one, not zero. Perhaps you can explain what led you to this conclusion.

    ... whole code is below:
    Not true: you've omitted the copyright notice which is required by the Dynamic Drive Terms of Use. The whole script can be found in the slide shows section, under the title DHTML Slide Show Script.

    Mike

  6. #6
    Join Date
    Dec 2006
    Posts
    47
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default

    Hmmm, the plot thickens...

  7. #7
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    have you tried making the first part of the array, photo[0]=""
    Then you don't need to change the code at all, if the code automatically starts from one.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  8. #8
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow array from 0

    I need it to start from 1 since i was storing the data in a mysql database and i was using the index of the mysql data as the number for the variable photos[], the solution came on my sleep:

    while ($photo = mysql_fetch_array($photos))
    {
    $photon = ($photo[id] - 1); // see what i did? jejeje turn 1 into 0
    ?>
    photos[<?php echo $photon; ?>]="<?php echo $photo[filename]; ?>"
    <?php
    } // end while
    ?>

    well that did the trick, turning every 0 into 1 on the script didn't work.

    Also yes i remove the copyright only for the purpose of a smaller post, am on the dynamic drive forums so i guess it would be an overstatement to put that here but ok, i get the point

    thanks.

  9. #9
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by chakytoride View Post
    I need it to start from 1 since i was storing the data in a mysql database and i was using the index of the mysql data as the number for the variable photos[],
    Yes, I thought it would be something like that.

    the solution came on my sleep:

    while ($photo = mysql_fetch_array($photos))
    {
    $photon = ($photo[id] - 1); // see what i did? jejeje turn 1 into 0
    ?>
    photos[<?php echo $photon; ?>]="<?php echo $photo[filename]; ?>"
    <?php
    } // end while
    ?>
    If that works, OK, but why not just use an independent counter? The id isn't significant for constructing the array itself, only a consecutive sequence of numbers:

    PHP Code:
    <?php
    $count 
    0;
    while (
    $photo mysql_fetch_array($photos)) {
    ?>
    photos[<?php echo $count++; ?>] = "<?php echo $photo['filename']; ?>";
    <?php
    }
    ?>
    Note the use of quotes when indexing the PHP array. See Why is $foo[bar] wrong? in the PHP manual for more information.

    One can even avoid generating numbers at all using an array literal.

    PHP Code:
    var photos = [
            <?php
    while ($photo mysql_fetch_array($photos)) {
        
    $uriList[] = "\"{$photo['filename']}\"";
    }
    echo 
    implode(",\r\n\t\t"$uriList);
    ?>

        ];
    That would completely replace the declaration of the photos array in the client-side script.

    Mike

  10. #10
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking nice

    the $count thingie is gonna come handy soon

    thanks a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •