News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Need help with small php script (altering in Post.php)

Started by Stayfun, August 07, 2004, 04:46:56 AM

Previous topic - Next topic

Stayfun

I'm trying to write a small script in php to load the post icons from a directory into the $context['icons'] array.

That way I should be able to use uploaded icons as post icons instead of altering the array manually.

So, this is the current code:
$context['icons'] = array(
array('value' => 'xx', 'name' => $txt[281]),
array('value' => 'thumbup', 'name' => $txt[282]),
array('value' => 'thumbdown', 'name' => $txt[283]),
array('value' => 'exclamation', 'name' => $txt[284]),
array('value' => 'question', 'name' => $txt[285]),
array('value' => 'lamp', 'name' => $txt[286]),
array('value' => 'smiley', 'name' => $txt[287]),
array('value' => 'angry', 'name' => $txt[288]),
array('value' => 'cheesy', 'name' => $txt[289]),
array('value' => 'grin', 'name' => $txt[293]),
array('value' => 'sad', 'name' => $txt[291]),
array('value' => 'wink', 'name' => $txt[292])
);


And now I'm writing a new code wich looks like this (quite raw though):

if ($handle = opendir('*******\Themes\default\images\post')) {

   while (false !== ($file = readdir($handle))) {
    $list[] = $file;
   }

   closedir($handle);
}


foreach($list as $key) {

$context['icons'] = array(
array('value' => $key, 'name' => $key),
);

}


As you already might have seen I'm not an expert in php. The problem is that I do not know how to handle this because there is an array within an array.  I'm sure there are here some poeple that do know how to make the right loop.


PS: I do know that i've  to get rid of the ".." and ".gif" extension when reading the files, but thats no problem.

Aquilo

I don't know if this will work for you but it's worth a try!

$dir = '*******\Themes\default\images\post';
// Start dir class engine
$d = dir($dir);
while ( false !== ($obj = $d->read()) )
{
if ($obj != '.' && $obj != '..')
{
if (is_file($dir.'/'.$obj) && stristr($obj,'.gif'))
{
$context['icons'][] = array('value' => $obj, 'name' => $obj);
}
}
}
$d->close();

Aquilo

ok I tested it and here is a little revision!

$dir = 'C:\Program Files\Apache Group\Apache\htdocs\smf_forum\Themes\mysticwicks\images\post';
// Start dir class engine
$d = dir($dir);
while ( false !== ($obj = $d->read()) )
{
if ($obj != '.' && $obj != '..')
{
if (is_file($dir.'/'.$obj) && stristr($obj,'.gif'))
{
$obj = str_replace('.gif', '', $obj);
$context['icons'][] = array('value' => $obj, 'name' => $obj);
}
}
}
$d->close();

Stayfun

Quote from: Aquilo on August 07, 2004, 05:39:35 AM
ok I tested it and here is a little revision!

Haha yes that's great mate! I just wanted to say that the gif ext had to be stripped but you already managed that! Great work!

Aquilo


Advertisement: