0

This php code uses opendir() to display an array of files in the folder.

Question: This code works as it is, but what i would like to do is display the description of the file as well.

I have added $description to the url in the code:

$result .= '<h6><a href="../folder/'.$file.'">'.$title.'</a></h6><p>$description</p><p><a href="../folder/'.$file.'">'.$file.'</a></p>';

which displays

File name

$description

file-name.php

Problem: I dont know how to call the function to get the description.

<?php 

if ($handle = opendir('../folder/')) {
    $fileTab = array();
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && $file != 'index.php') {
            $fileTab[] = $file;
        }
    }
    closedir($handle);
    shuffle($fileTab);
    foreach($fileTab as $file) {
        $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
        $result .= '<h6><a href="../folder/'.$file.'">'.$title.'</a></h6><p>$description</p><p><a href="../folder/'.$file.'">'.$file.'</a></p>';
    }
}
?>
<?=$result?>
Daisy Oopsy
  • 155
  • 9
  • 1
    What is `$description`, where is it, where was it defined??? – Kerem Jan 20 '13 at 23:54
  • sorry i want to get the meta description and echo it where it says $description in the $result, see link for example [link]http://test.whatanswered.com/health/a.php – Daisy Oopsy Jan 20 '13 at 23:57
  • You could use the http://php.net/manual/en/function.exif-read-data.php to get the `exif` headers for a jpg or tiff file. – Cyclonecode Jan 21 '13 at 00:00
  • You need to open the file and read the contents (aka the source code) to get that data. It's just a text and not something that you can obtain from the filesystem itself. You might want to check the [**`token_get_all()`**](http://php.net/manual/en/function.token-get-all.php) function. – inhan Jan 21 '13 at 00:05

2 Answers2

0

As qeremy says where is $description defined? Even if $description is assigned a value elsewhere it will never be displayed as the variable contents as you are encasing the string in single quotes ''. If you want PHP to parse a variable as the variables value (and not the variable name as a string) you need to either use double quotes "", or break the string and including the varibale name for example:

$result .= '<h6><a href="../folder/'.$file.'">'.$title.'</a></h6><p>'.$description.'</p><p><a href="../folder/'.$file.'">'.$file.'</a></p>';
Zappa
  • 453
  • 1
  • 5
  • 14
  • im just learning php and not sure how to call the function to get description, the reason why i had not added '.$description.' in quotes was because it will echo the current page url i'm viewing. – Daisy Oopsy Jan 21 '13 at 00:04
  • Ah okay. What sort of file types are you working with and what description are you hoping to get? The opendir function does not return any description of either a file or directory. As far as I know there is no native PHP function that returns anything more than just some basic file specific details (like size, type, etc). To get any other details you would need to use addional libraries or code (for example: http://code.google.com/p/php-reader/ If you are using image files then you will need to use the exif functions as mentioned by Krister Andersson – Zappa Jan 21 '13 at 00:23
  • I'm using php files and i'm hoping to get the meta description, see this link you will understand straight away [link]http://test.whatanswered.com/health/a.php i wasn't sure if there was some function i could add to the current code to get the meta description like file_get_contents. – Daisy Oopsy Jan 21 '13 at 00:32
0

OK!

As I see, you want to display items in a loop and write them orderly with title adn description. At this point, I think you need a proper system which is integrated to a database.

Simply;

// table: items
id | title     | description
-----------------------------------
1  | Headaches | Lorem ipsum dolor... 

If that not suit for you, so you can do so (but this is not handy!), you can keep your data in .txt files in folder directory like;

// folder/headaches.txt
Headaches|||What can you do to stay healthy, your...

// php
$files = glob("folder/*.txt");
foreach ($files as $file) {
    $info = pathinfo($file);
    $filename = $info['filename'];
    $contents = file_get_contents('descriptions/'. $filename .'.txt');
    list($title, $description) = explode('|||', $contents, 2);
    $result .= sprintf('<h6><a href="../folder/%s">%s</a></h6><p>%s</p><p><a href="../folder/%s">%s</a></p>',
                            $filename, $title, $description, $filename, $filename);
}
Kerem
  • 11,377
  • 5
  • 59
  • 58