Warning: fopen(graphic_design/files/thread-2346-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened A Little PHP Help -
PDA

View Full Version : A Little PHP Help


David
12-01-2004, 03:51 AM
I'm quite new to PHP, so please bare with me if it doesn't make sense.


Say I have my pages set up like 'mydomain.com/news.php?article=34', how would I go about making it so that whenver I upload a new article into a specific directory that it is automatically included with its own dynamic url, numbered accordingly without me having to keep editing the news.php file to include these?

Example: I have 10 pages in my directory and they are numbered 1 - 10, (article=1, 2, 3, etc), and when I upload a new one, number 11, it automatically has its own number.


Is this even possible?


Any help from the PHP lovers would be great *looks at Bane*

Thanks :)

Arch Stanton
12-01-2004, 08:45 AM
Ive never done it, but you can get a directory listing. Once you have that, you could include each file in a directory.

http://ca3.php.net/manual/en/ref.dir.php

Hope that helps.

owentrier
12-01-2004, 09:12 AM
Usually I find something I like at hotscripts.com and then I break the code apart and learn from it. I know this sound like too much work but it the only way to understand PHP. I HATE reading books on coding. They never make any sense. They are coder teaching coders. Not teaching designer.

Anyway, hope this helps..

David
12-02-2004, 03:31 AM
Yeah this helped.

Thanks guys :)

Koobi
12-02-2004, 01:22 PM
Hey :)

I can't write any code now. I'm meeting a CRAZY deadline....but I will tell you which functions to use and the pseudocode. I can have something for you on saturday with an explanation on how it works.

But this is how you do it:

You read a directory using readdir() (http://www.php.net/readdir).
And only read in the file names, without the extension.



<?php
$fileName = basename($files, '.php');
/* Now $fileName contains ONLY the filename without the extension */
?>


Now sort $fileName like this:

<?php
sort($fileName, SORT_STRING);
?>



And then include it like this:

<?php
include 'directoryName/' . end($fileName) . '.php';
?>




I'd say spend some time on learning how to read the directory properly
http://koobi-studio.com/downloads/php/classes/RandomFiles/showSource.php
The function named getFiles() should help you out a bit with that.

Sorry to rush, I hope this helped a bit.
If not, I will post on how this works and the code soon :)

Octane
12-03-2004, 03:10 AM
This is what I ususally do for reading directories:

$picDir = dir($directory);

while($check = $picDir->read())
{
// ignore .. and .
if ($check != '..' && $check != '.')
{
// if the files are all just numbers, then this is the lazy way to get the value
$filename = intval($check);
echo "<a href=\"news.php?article==$filename\">News Item $filename</a><br />\n";
}
}


Not exactly sure how you have it setup, so i can't really go into any further detail ...

David
12-03-2004, 06:40 AM
Ah cheers, Bane!

I'm pretty sure how this works, as I said I'm pretty new..only about a month and a bit of using it, but I'll get better.

Jeff O.
12-06-2004, 10:14 AM
Hope you get it working David. Arch, Bane and Octane you guys rock and I'm glad you're in this forum! :)

Koobi
12-13-2004, 10:25 AM
heh heh sorry for the delay. Deadlines and exams :)
I have one exam tomrrow but argh I need some stress relief (PHP :D)





Here's some code I just wrote but haven't tested. It should work though. I have included comments :)



<?php
function latestFile($dir)
{
/* Clear cache */
clearstatcache();

/**
* This is the ternary method.
* It checks if the last character of the given directory is a slash (checking for trailing slashes)
* If the slash does not exist, it adds one otherwise it leaves it as it is.
*
* You can also write it like this:
* if(substr($dir, -1) != '/')
* {
* $dir .= '/';
* }
**/
$dir = (substr($dir, -1) == '/') ? $dir : $dir . '/';

/* If this is a directory... */
if(is_dir($dir))
{
/* ...and is readable... */
if(is_readable($dir))
{
/* ...then open the directory for reading */
$dirHandle = @opendir($dir);

/* Use a while loop to go through each file and read them in */
while(FALSE !== ($file = @readdir($dirHandle)))
{
/* Grab file names excluding their .php extensions */
$tmpName = basename($files, '.php');
/* Now check if those file names are numeric */
if(is_numeric($tmpName))
{
/* If they are numeric, add them to the $fileName array */
$fileName[] = $tmpName;
}
}
/* Now sort $fileNames numerically in ascending order */
sort($fileName, SORT_NUMERIC);
}
else
{
die('Directory cannot be read.');
}
}
else
{
die($dir . ' is not a valid directory.');
}
/* Return the directory path and the last entry in the $fileName array that is sorted in ascending order */
return $dir . end($fileName);
}

include latestFile('myDirectory');
?>




Octane's method for reading a directory is better but in the past I've had issues with using a predefined class such as 'dir' on different servers so I try and avoid them though I would prefer to use them.



If you have any questions, please don't hesitate to ask :)

David
12-13-2004, 11:49 AM
Wow thanks a lot.

I was a bit unsure at the start, but now I get it.

Thanks a lot!

Koobi
12-13-2004, 12:49 PM
Wow thanks a lot.

I was a bit unsure at the start, but now I get it.

Thanks a lot!


heh heh we are talking about PHP here so you KNOW I'm not kidding when I say the pleasure was all mine :P

David
12-13-2004, 02:05 PM
Just out of curiosity, where did you learn PHP? I mean what books or sites did you use or what do you recommend.

I have a Sams 'Teach Yourself PHP' book, but its not helping me much..they don't really explain a lot.

So yeah, any recommedations from the master?

Arch Stanton
12-13-2004, 09:08 PM
I learned PHP by downloading open source scripts from sourceforge.net and figuring them out. Also, php.net has a great function reference :)

Koobi
12-14-2004, 04:49 AM
heh heh if you wan't to learn PHP, get a really bad teacher. Yeah I know that sounds wierd :P but you would just dig for more to finish your assignments if you had a bad teacher and would learn more in the process.

I learnt PHP by downloading the CMH manual and using that as a reference and then there are the million tutorials you would find on google. But I started only in January 2004 (this year) and what made it easy for me was that I did C++ (PHP is similar to C which is in turn similar to C++) as a teenager in school.

I think the best thing for you to do (since you already know some PHP) would be to plan out a project and do it all by yourself in PHP...such as creating a very basic guestbook. And then you could ask someone who knows PHP pretty good to point out any mistakes you might have made.

There's so much in PHP....new things are being added with each release :D ANd I have so much more to learn :S