Jump to content

[SOLVED] hopefully a stupid question


ashess

Recommended Posts

hi! I'm new here, and to php -roughing through it by just about using anything and any help I can get.

the script I've 'made' is pretty simple; it's meant to scroll through a folder with just images(jpg) in it.

I have a counter that adds or substracts and I have a readout for the number of total images (ok, files) in the folder.

- there remains one problem. instead of the 'last' button putting out the last image (image8.jpg), it simply looks for an image with the functioncounter name (&filecount).

now, I hope I've just messed up some brackets or something, but I've been messing with it for a while now, and I can't figure it out. anyone who's got more of a clue willing to take a look?

thank you!

(probematic part is the last function)

the php::

<?php

 

 

// Point to the folder containing the images. Relative path without trailing slash!

$imgfolder    =    "images";

 

$dir = 'images';

$filecount = 0;

$d = dir($dir);

while ($f = $d->read())

{  if(($f!= ".") && ($f!= ".."))

{  if(!is_dir($f)) $filecount++;  }

}

 

 

 

?>

 

<img id='plaatje' src="'.$imgfolder.'/'.$img.'" border="1">

<br>

<button type="button" id="last" class="button" width="100px" onClick="showFirstPicture();" > First </button>

 

<button type="button" id="last" class="button" width="100px" onClick="showPreviousPicture();" > Previous </button>

 

<button type="button" id="next" class="button" width="100px" onClick="showNextPicture();" > Next </button>

 

<button type="button" id="next" class="button" width="100px" onClick="showLastPicture();" > Last </button>

<br>

<br>

 

 

<script type="text/javascript">

 

var imageCount=0;

 

window.onload=function()

{

    imageCount=1;

    var fileName="images/image"+imageCount+".jpg";

    document.getElementById('plaatje').src=fileName;

 

}

 

function showNextPicture()

{

    imageCount++;

    var fileName="images/image"+imageCount+".jpg";

    document.getElementById('plaatje').src=fileName;

 

}

function showPreviousPicture()

{

    imageCount--;

    var fileName="images/image"+imageCount+".jpg";

    document.getElementById('plaatje').src=fileName;

 

}

function showFirstPicture()

{

    imageCount=1;

    var fileName="images/image"+imageCount+".jpg";

    document.getElementById('plaatje').src=fileName;

 

}

 

 

//it goes wrong here- the filename is just given as filecount.jpg

function showLastPicture($filecount)

{

    imageCount= '$filecount';

    var fileName="images/image"+imageCount+".jpg";

    document.getElementById('plaatje').src=fileName;

 

}

 

 

 

 

</script>

<?php

echo 'there are ',$filecount,' files in this folder';

 

?>

 

 

the php in action:

http://projectz.ashess.nl/test.php

 

 

Link to comment
https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/
Share on other sites

You're mixing PHP and javascript

 

//it goes wrong here- the filename is just given as filecount.jpg
function showLastPicture($filecount)
{
    imageCount= '<?php echo $filecount; ?>';
    var fileName="images/image"+imageCount+".jpg";
    document.getElementById('plaatje').src=fileName;

}
[code]

It probably isn't just the last function, but most of them.

 

Even in functions, variables are identified by a $ character before the variable name

 

And you're referencing variables that aren't in scope for the function.

 

Err, he's using Javascript for those groups of functions.

 

Also, please use


tags when you post code.

Actually no, he is using a php variable in javascript;

 

//it goes wrong here- the filename is just given as filecount.jpg
function showLastPicture($filecount)
{
    imageCount= '$filecount'; //PROBLEM HERE, IT NEEDS TO BE  imageCount= '<?php echo $filecount ?>';
    var fileName="images/image"+imageCount+".jpg";
    document.getElementById('plaatje').src=fileName;

}

also, don't declare showLastPicture as requiring $filecount as an argumet:

you don't pass it anything in the function call in the html, and you don't use it in the function. as Guardian-Mage said, you should be echoing the value of $filecount directly into imageCount.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.