vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Creating a widget from this code.. (https://vborg.vbsupport.ru/showthread.php?t=270337)

B16MCC 09-15-2011 03:40 PM

Creating a widget from this code..
 
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.

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 04: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.

kh99 09-15-2011 04:14 PM

I'm not a widget expert, but I think if, in place of print() and echo() you use:

PHP Code:

$output .= "<a href=\"".$file."\">".$file."</a><br />\n"

and

PHP Code:

$output .= '<br /><br /><a href="..">Return</a>'


it might work.

ETA: ah, yeah, like Lynne said.

B16MCC 09-16-2011 02:11 PM

Thanks a lot for the help guys, I'm almost there. I'm now using the following code :-

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
Code:

www.example.com/file.mp3
The link i want displayed is
Code:

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 02:18 PM

search
Code:

$output .= "<a href=\"$file\">$file</a><br />";
and replace
Code:

$output .= '<a href="' . $path . '/' . $file'">' . $file . '</a><br />';

B16MCC 09-16-2011 04: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 [DATE]1316196988[/DATE] at [TIME]1316196988[/TIME] ---------------

OK, for future reference , I got it !

Code:

$output .= "<a href=\"/mp3/$file\">$file</a><br />";


All times are GMT. The time now is 09:35 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03519 seconds
  • Memory Usage 1,725KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (7)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete