ashess Posted November 12, 2008 Share Posted November 12, 2008 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 More sharing options...
Mark Baker Posted November 12, 2008 Share Posted November 12, 2008 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] Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688806 Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 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. Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688809 Share on other sites More sharing options...
Guardian-Mage Posted November 12, 2008 Share Posted November 12, 2008 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; } Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688810 Share on other sites More sharing options...
Mark Baker Posted November 12, 2008 Share Posted November 12, 2008 Err, he's using Javascript for those groups of functions. Yeah, got me... just as I was editing my response to correct my own mistake Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688816 Share on other sites More sharing options...
bobbinsbro Posted November 12, 2008 Share Posted November 12, 2008 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. Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688818 Share on other sites More sharing options...
ashess Posted November 12, 2008 Author Share Posted November 12, 2008 the php tags worked! thank you very much !^^ and sorry about the code tags. I will try them next time! Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688820 Share on other sites More sharing options...
ashess Posted November 12, 2008 Author Share Posted November 12, 2008 took the arguement out too- I was kinda trying everything I guess Link to comment https://forums.phpfreaks.com/topic/132480-solved-hopefully-a-stupid-question/#findComment-688823 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.