Re: code quest
| From: | Richard Quadling | Date: | Sat, 04 Dec 2010 23:09:30 +0000 |
| Subject: | Re: code quest | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-309854@lists.php.net to get a copy of this message | ||
On 4 December 2010 20:58, Kirk Bailey <kbailey@howlermonkey.net> wrote:
> the code is now:
>
> <?php # The next several lines declare an array of directories which
> are NOT to be listed!
> $excludes[] = 'images';
> $excludes[] = 'cgi-bin';
> $excludes[] = 'vti_cnf';
> $excludes[] = 'private';
> $excludes[] = 'thumbnail';
>
> $ls = scandir(dirname(__FILE__));
> foreach ($ls as $d) {
> if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
> !in_array(basename($d),$excludes)) {
> echo '<li><a href="'.$d.'">';
> echo include($d.'/desc.txt' );
> echo '</a><br/>'.PHP_EOL;
> }
> }
> ?>
>
> And it works!
> BUT!!!!!
> Where is the "1" coming from?!?
> Please inspect the page and see what I mean. This is not in the code, and
> it's not in the source file. Link:
> http://www.howlermonkey.net/dirlisting.php
>
> I hope this dialog is proving at least mildly interesting to the remainder
> of the list as an educational exercise.
>
>
> Tamara Temple wrote:
>>
>> On Dec 4, 2010, at 2:15 PM, Kirk Bailey wrote:
>>
>>> The hound barks, but does not yet properly hunt, and we need to bring
>>> home the bacon.
>>>
>>> OK, here is the current code:
>>>
>>> <?php # The next several lines declare an array of directories which
>>> are NOT to be listed!
>>> $excludes[] = 'images';
>>> $excludes[] = 'cgi-bin';
>>> $excludes[] = 'vti_cnf';
>>> $excludes[] = 'private';
>>> $excludes[] = 'thumbnail';
>>>
>>> $ls = scandir(dirname(__FILE__));
>>> foreach ($ls as $d) {
>>> if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
>>> !in_array(basename($d),$excludes)) {
>>> echo '<li><a
>>> href="'.$d.'">'.include('./'.$d.'/desc.txt')
>>> ;#.'</a><br/>'.PHP_EOL;
>>> }
>>> }
>>> ?>
>>>
>>> the url again, to view the results, is
>>> http://www.howlermonkey.net/dirlisting.php and is live
>>> right now. The
>>> results are a tad odd to say the least.
>>>
>>
>> Ok, I don't think that's actually the code that generated the page you
>> link to, but let's go with what you've got.
>>
>> First of all, include() does not return a string to the calling program.
>> include() basically redirects the php interpretter to process the contents
>> of the file. What include() returns is success or failure of the execution
>> of the included script. To use the current setup you have with the desc.txt
>> files, you want to do something like this:
>>
>> echo '<li><a href="'.$d.'">';
>> include('./'.$d.'/desc.txt');
>> echo '</a></li><br />'.PHP_EOL;
>>
>> If the file desc.txt contains only text, it will get sent to the browser
>> as is.
>>
>>
>>>
>>>
>>>
>>> Kirk Bailey wrote:
>>>>
>>>> Ok, let's kick this around.
>>>>
>>>> iterating an array(?; 1 dimensional listing of things) in php, I am
>>>> creating a list of direcoties. I want to open and read in a file in each
>>>> directory with a standard name, which contains a 1 line description of the
>>>> directory and it's purpose.
>>>>
>>>> Now, here's the existing code; this code is online NOW at this url:
>>>> http://www.howlermonkey.net/dirlisting.php
>>>>
>>>> * 1<?php # The next several lines declare an array of directories
>>>> which are 2NOT to be listed!
>>>> 3$excludes[] = 'images';
>>>> 4$excludes[] = 'cgi-bin';
>>>> 5$excludes[] = 'vti_cnf';
>>>> 6$excludes[] = 'private';
>>>> 7$excludes[] = 'thumbnail';
>>>> 8
>>>> 9$ls = scandir(dirname(__FILE__));
>>>> 10foreach ($ls as $d) {
>>>> 11if (is_dir($d) && !preg_match('/^\./',basename($d))
>>>> &&
>>>> 12!in_array(basename($d),$excludes)) {
>>>> 13 echo '<li><a
>>>> href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
>>>> 14 }
>>>> 15}
>>>> 16?>*
>>>>
>>>> Let's say the file to read in /elite is named 'desc.txt'.
>>>> It looks like you want me to modify line 13 to say:
>>>> echo '<li><a
>>>> href="'.$d.'">'.include($d.'desc.txt').'</a><br/>'.PHP_EOL;
>>>>
>>>> so, if the file '/elite/desc.txt' contains the line
>>>>
>>>> *82nd Airbourne - We are an elite unit of army Paratroopers
>>>> *
>>>>
>>>> Then that element in the list would appear as:
>>>>
>>>> * 82nd Airbourne -We are an elite unit of army Paratroopers
>>>>
>>>> And would be clickable. Let's try it and see if this hound hunts.
>>>>
>>>> Matt Graham wrote:
>>>>>
>>>>> From: Kirk Bailey <kbailey@howlermonkey.net>
>>>>>
>>>>>> OK, now here's a giggle; I like ssi includes. If I put the script
>>>>>> in as an ssi include, will it still work?
>>>>>>
>>>>>
>>>>> If you're using Apache, and you do
>>>>>
>>>>> <!--#include virtual="something.php" -->
>>>>>
>>>>> ...the PHP in something.php will execute and produce output, but
>>>>> something.php will not have any access to $_GET or $_POST or $_SESSION
>>>>> or any
>>>>> of those things. This is generally not what you want. If you do
>>>>>
>>>>> <?php include("something.php"); ?>
>>>>>
>>>>> ...then something.php will be able to see and work with the
>>>>> superglobals that
>>>>> have been set up further up the page, which is *usually* what you want.
>>>>> You
>>>>> could try both approaches in a test env and see what you get. I'll use
>>>>> SSI
>>>>> for "dumb" blocks of text and php include for "smart"
>>>>> blocks of code,
>>>>> because
>>>>> IME that tends to produce fewer instances of gross stupidity.
>>>>>
>>>>> Note that YMMV on all this and ICBW.
>>>>>
>>>>>
>>>>
>>>
>>> --
>>> end
>>>
>>> Very Truly yours,
>>> - Kirk Bailey,
>>> Largo Florida
>>>
>>> kniht +-----+
>>> | BOX | +-----+
>>> think
>>
>>
>
> --
> end
>
> Very Truly yours,
> - Kirk Bailey,
> Largo Florida
>
> kniht +-----+
> | BOX | +-----+ think
>
You don't need to echo the include statement.
Just use include
Unless your include looks like ...
<?php
return something.....
In which case, you can assign the result or echo it.
See example 5 on http://docs.php.net/manual/en/function.include.php
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY