PDA

View Full Version : Hi Face ~_~ (PHP sucks)



Jojee
08-06-2008, 07:36 AM
I don't get PHP at all. Here's my code:

<?php
$catdir = 'avatars_icons';
$sort = 'first';
$getcount = true;
$perpage = 20;
$divider = ' | ';

if (!file_exists($catdir)) {
exit();
}
$getfolders = opendir($catdir);
while (($file = readdir($getfolders)) !== false) {
if (($file != '.' && $file != '..') && (is_dir($catdir.'/'.$file))) {
$cats[] = $file;
}
}
if (!$cats) {
exit();
}
natcasesort($cats);

if ((!$_GET['cat']) || (!in_array($_GET['cat'], $cats))) {
?>


<?php
foreach ($cats as $value) {
$catname = str_replace('_', ' ', $value);
echo '<a href="' . $_SERVER['PHP_SELF'] . '?cat=' . $value . '">';
<b> echo '&lt;img src=avatars_icons/ . $value . /icon.png&gt;&lt;/a&gt;';</b>
if ($getcount) {
$icons = array();
$getcount = opendir($catdir . '/' . $value);
while (($icon = readdir($getcount)) !== false) {
if (eregi(".jpg$|.jpeg$|.gif$|.png$|.bmp$", $icon)) {
$icons[] = $value;
}
}
$count = count($icons);
if ($count == 1) {
echo ' (' . $count . ' icon)';
} else {
echo ' (' . $count . ' icons)';
}
}
}
}

ETC IT GOES ON.


Okay, so basically this is an avatar gallery script. You make folders in a specified directory ($catdir), it displays the name of the folders inside that directory and how many avatars are inside, and when you click the link it displays those avatars.

Where I'm having trouble is still in the very beginning - displaying the categories. Something's wrong with the bolded part of the script. If I put simple text in there, it works fine, but when I try to add the image, nothing displays. It has to do with $value, because when I put in the folder name straight out, it does work. Is $value the wrong variable to use in this case, or am I just doing something wrong? I'm trying to go to each folder in the directory, pull out the image inside the folder called icon.png, and display that image.

Do you know what I'm trying to get at? ~_~

Thanks in advance to anyone who can help.

Jojee
08-06-2008, 07:48 AM
OH NEVERMIND I think I got it. xD I had to add some backslashes. I always get the escape, double, single, whatever stuff confused. T_T I ended up with

echo '&lt;a href="' . $_SERVER['PHP_SELF'] . '?cat=' . $value . '"&gt;&lt;img src=\'avatars_icons/'.$value.\'/icon.png\'&gt;&lt;/a&gt;';

Uh seriously, you guys can feel free to delete all my help forum threads. :p

rubah
08-06-2008, 07:53 AM
Does it work now? It seems that you should have put that echo in double quotes since you used a variable. single quotes aren't supposed to parse variables afaik.

but if it does work, just forget about this *jedi hand wave*

Flying Mullet
08-06-2008, 02:52 PM
In regards to the echo, I'd brush up on the differences between echo(), print() and printf(). They're all useful in different situations for printing data to the screen, although I typically use print() or printf(). For example, if you ever loop through data pulled from a database printf() is the way to go.

Jojee
08-06-2008, 11:50 PM
Edit: Never mind, I got this problem too. xD

I think you guys should ignore all my PHP help topics from now on, because I'm not really reading about this... lmao, this is all trial and error learning for me, and eventually I get it right!

Jojee
08-12-2008, 04:30 PM
Okay, I'm opening up this thread because I have another PHP question.

How do I use the PHP include function to include files that are not in the directory I'm including from? God I used the word include a lot in that sentence. xD

So say I have a file - nesakya.net/include.php.

I want to be able to include this file from forum.neskaya.net/index.php (subdomain) as well as neskaya.net/yourmom/isawesome/index.php.

Help? :3 Thanks.

Flying Mullet
08-12-2008, 04:35 PM
Do you want an answer or would you rather wait 30 minutes and you can post that you figured it out yourself? :p

Jojee
08-12-2008, 04:35 PM
I want an answer!! xD I'm at work so I can't just go about trying stuff.

Flying Mullet
08-12-2008, 04:41 PM
Well I did a quick search on Google for "fully qualified include php" and this was one of the suggestions:

include($_SERVER["DOCUMENT_ROOT"]."/php/utilities.php");

Problem with include file.php (http://www.webmasterworld.com/forum88/1397.htm)

Note: I haven't tried this, I just searched and found it.

Jojee
08-12-2008, 04:51 PM
Thanks, I'll test this out when I get home and I can access my site files and php server.

But $_SERVER["DOCUMENT_ROOT"] won't go back to subdomain.neskaya.net if I'm including from a subdomain, it'll actually go to neskaya.net, right?

o_O
08-13-2008, 01:27 AM
$_SERVER['document_root'] will refer back to the document root of the server and not virtual host in which the PHP script is running. I'm not sure how you have your hosts set up, but for subdomains you'd normally have something like this in your Apache configuration:

ServerRoot "/path/to/server/root"
DocumentRoot "/path/to/document/root" <!-- $_SERVER['DOCUMENT_ROOT'] should point here -->

NameVirtualHost *:80
<VirtualHost *:80>
ServerName "neskaya.net"
DocumentRoot "what/ever" <!-- Not here -->
...
</VirtualHost>

<VirtualHost *:80>
ServerName "forums.neskaya.net"
DocumentRoot "what/ever/lol" <!-- or here :p -->
...
</VirtualHost>Also not that $_SERVER['document_root'] != $_SERVER['DOCUMENT_ROOT']. :p

If you just use that variable as your absolute path, you never need to worry about the location of the script you're executing.

//So if the document root of your server is one directory above neskaya.net:
include $_SERVER['DOCUMENT_ROOT'] ."/neskaya.net/include.php";

//Or perhaps if your document root is the neskaya.net folder:
include($_SERVER['DOCUMENT_ROOT'] ."/include.php");

//If you need to find out what that document root is:
echo $_SERVER['DOCUMENT_ROOT'];

:p