Bald_and_Anrgy Posted February 6, 2013 Share Posted February 6, 2013 Wow, am I glad I found you guys. The CSS forums I normally frequent are a little weak in PHP, so I hope someone might be able to help. PHP is something I've recently started playing with, but have been coding websites for a while. Ok, so the issue is that I managed to write a PHP script in a static site to load images from a directory, and load them into a list which has been styled with CSS. The script works perfectly. The key to the load is the path of the directory, which I pass in as a variable... $tempDir = "/images/" The problem arises when I try to change the value of the variable to a Wordpress shortcode combined with a relative path. I've included the code here: $tempDir = get_bloginfo('template_url') . '/img/gallery/generic/'; //$tempDir = '/img/gallery/generic/'; works on the static site $myDirectory = opendir($tempDir); while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } closedir($myDirectory); $indexCount = count($dirArray); echo $indexCount; The echo at the end is just so I can see whether the array is populating... which it's not. There is another piece of code to do the load, but that seems to be fine. Is someone able to help? Quote Link to comment https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/ Share on other sites More sharing options...
soycharliente Posted February 6, 2013 Share Posted February 6, 2013 (edited) A few things that might help. 1. You might look into using get_template_directory_uri instead. 2. According to the PHP manual, you'll want to update the way in which you're looping. Read over example #1. 3. Output more than just the $indexCount for testing your variables. I would output everything in HTML comments. The template URL, the directory, each file being read as you go, and the count as you go. It will give you a better indication of exactly where the script is failing. Edited February 6, 2013 by charlieholder Quote Link to comment https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/#findComment-1410508 Share on other sites More sharing options...
Bald_and_Anrgy Posted February 7, 2013 Author Share Posted February 7, 2013 (edited) Thanks so much Charlie. 1. I sat up playing with the code last night, and exchanging get_bloginfo('template_url') for the Wordpress constant TEMPLATEPATH fixed the issue, and the gallery is now loading 2. I had no idea that was the wrong way to do that loop, but I'm now updating it thanks to your advice 3. That's a great suggestion. I was only just getting caught up on the fact that the array was empty and not really checking where it was going wrong. Thanks. Could you suggest any resources that are handy in learning PHP, as opposed to just pulling apart Wordpress? It seems like a pretty powerful tool to have in your toolbox Edited February 7, 2013 by Bald_and_Anrgy Quote Link to comment https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/#findComment-1410636 Share on other sites More sharing options...
soycharliente Posted February 7, 2013 Share Posted February 7, 2013 Could you suggest any resources that are handy in learning PHP, as opposed to just pulling apart Wordpress? It seems like a pretty powerful tool to have in your toolbox Honestly just the manual. For me, I learned what PHP I know just by imagining websites and them trying to build them. What got me most of my knowledge, and this is no joke, was when I created a website exactly like Foursquare (except I did it 3 years before). Relational databases, login, sessions, statistics on database information, Google Maps, Google Charts, CSS, XHTML, image upload, and the list goes on. At work we have a pretty cool perk, which is a free account on lynda.com. They've got some good tutorials, but honestly just picking a scenario you don't know anything about and reinventing it is the best way to learn. You RTM, look through blog posts, and research how other people are doing it. It blew my mind 100 fold how complicated doing login can be. Password hash, password reset, expiring the hash, locked accounts, verifying email, thwarting attempted logins from "hackers" and the list goes on. TL;DR Imagine a scenario, RTM, and find other people doing it so you can ask questions. Books are ok, but hands on experience wins out for me. Quote Link to comment https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/#findComment-1410716 Share on other sites More sharing options...
Christian F. Posted February 7, 2013 Share Posted February 7, 2013 There are also quite a few resource thread in this forum, as well as links to resources in regular posts. Not to mention the signature of some of the badged members: Lots and lots of good information there. I also recommend following Anthony Ferrara on Youtube. Quote Link to comment https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/#findComment-1410743 Share on other sites More sharing options...
Bald_and_Anrgy Posted February 8, 2013 Author Share Posted February 8, 2013 Great! Thanks guys. Really appreciate the advice and help Quote Link to comment https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/#findComment-1410925 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.