View Full Version : Creating a widget from this code..
B16MCC
09-15-2011, 04:40 PM
Hi guys, can someone help me please with a beginners PHP problem.
I want to create a widget for my CMS using the following code to list files as links. I'm fine with the basics of creating a widget, but I need a little help please to implement this code.
<?php
$count = 0;
if ($handle = opendir('./mp3')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("<a href=\"".$file."\">".$file."</a><br />\n");
}
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?>
The above code will display files as links in a given folder, I just need to make it work as a widget.
Thanks for your time.:)
Lynne
09-15-2011, 05:12 PM
Download a couple of widgets that are posted here and take a look at how they did it. You cannot use print or echo in your widget. You must assign the output to a variable. As I said, if you download a widget or two from the mods area, you will see how it needs to be done.
I'm not a widget expert, but I think if, in place of print() and echo() you use:
$output .= "<a href=\"".$file."\">".$file."</a><br />\n";
and
$output .= '<br /><br /><a href="..">Return</a>';
it might work.
ETA: ah, yeah, like Lynne said.
B16MCC
09-16-2011, 03:11 PM
Thanks a lot for the help guys, I'm almost there. I'm now using the following code :-
// Define the full path to your folder from root
$path = "./mp3";
// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");
// Loop through the files
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "index.php" )
continue;
$output .= "<a href=\"$file\">$file</a><br />";
}
// Close
closedir($dir_handle);
This is very close to what I want. My only issue now is the link displayed for each file is missing the folder they are in.
The link displayed is
www.example.com/file.mp3
The link i want displayed is
www.example.com/mp3/file.mp3
I'm having terrible trouble making this seemingly easy modification. Could some one help with this last hurdle please ? Thanks a lot for your time.
Mooff
09-16-2011, 03:18 PM
search
$output .= "<a href=\"$file\">$file</a><br />";
and replace
$output .= '<a href="' . $path . '/' . $file'">' . $file . '</a><br />';
B16MCC
09-16-2011, 05:02 PM
Thanks for the quick response Mooff but I'm afraid that doesn't work.
$path contains the string ./mp3
While this is applicable to denote a path on the server, its not applicable to use as a link.
--------------- Added 1316196988 at 1316196988 ---------------
OK, for future reference , I got it !
$output .= "<a href=\"/mp3/$file\">$file</a><br />";
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.