Log in

View Full Version : array starting from 0



chakytoride
12-23-2006, 01:18 PM
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!!

Kenny
12-23-2006, 02:30 PM
change the zero to one in this line...
for (i=0;i<photos.length;i++){

chakytoride
12-23-2006, 02:39 PM
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.

Kenny
12-23-2006, 05:51 PM
Here's another zero to investigate...
var which=0

Try upping it to a one.

mwinter
12-24-2006, 12:23 AM
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 (http://www.dynamicdrive.com/notice.htm). The whole script can be found in the slide shows section, under the title DHTML Slide Show Script (http://www.dynamicdrive.com/dynamicindex14/dhtmlslide.htm).

Mike

Kenny
12-24-2006, 12:36 AM
Hmmm, the plot thickens...

boxxertrumps
12-24-2006, 01:47 AM
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.

chakytoride
12-24-2006, 02:24 AM
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.

mwinter
12-24-2006, 03:07 AM
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
$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? (http://uk.php.net/manual/en/language.types.array.php#language.types.array.foo-bar) in the PHP manual for more information.

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



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

chakytoride
12-24-2006, 08:31 AM
the $count thingie is gonna come handy soon ;)

thanks a lot.