Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/274103-loading-images-from-a-directory/
Share on other sites

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 by charlieholder

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 by Bald_and_Anrgy

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.