1
BJS3D
Re: TDMDownloads - Random Image Link Block
  • 2011/5/26 21:05

  • BJS3D

  • Just popping in

  • Posts: 7

  • Since: 2010/2/11


zyspec,

Your code alteration works perfectly. Thank you.

Random TDM Image Link Test

Also moved the extension string down with the rest of the defined strings:

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        }
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$extension ".jpg";
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>



2
BJS3D
Re: TDMDownloads - Random Image Link Block
  • 2011/5/26 20:49

  • BJS3D

  • Just popping in

  • Posts: 7

  • Since: 2010/2/11


Hi zyspec,

No worries. What I have now is working. Having said that, 'working' doesn't mean 'correctly coded'. By all logic, your code should have functioned and it's beyond my extremely limited php vocabulary as to why it's not. It's not an extremely pressing issue, though, so definitely don't lose any sleep. TDM, though, will definitely benefit from your code as this feature is far more effective than simply displaying a random shots image with no link to the actual entry.

I ended up reworking the problem of the blank string by forcing a default in the event a string returns null. So far, this is working 100% of the time without error. There were no other file formats in the directory, though, which is why the return of a null string is a mystery. I classified the problem as 'coding error' and handled it as best as a CG artist could.

I also fixed a redundancy issue. The error-catching is now done prior to the image and filename conversions.

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        
$extension ".jpg";
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
        }
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>


Again, here's how the block is setup:

<div align="left" style="verticle-align:top;"><Iframe src="http://coastalgfx.com/randcgi.php" width="172" height="172" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></Iframe></div>
<
div align="center"><Iframe src="http://coastalgfx.com/dl_count.php" width="164" height="27" frameborder="0" scrolling="no"></Iframe></div>


Thanks for the reply and I look forward to your input.

Is this the code change you're suggesting:

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        }
        
$extension ".jpg";
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>



3
BJS3D
Re: TDMDownloads - Random Image Link Block
  • 2011/5/16 16:43

  • BJS3D

  • Just popping in

  • Posts: 7

  • Since: 2010/2/11


zyspec,

Thank you for posting but I was unable to get the code to return anything but a blank string. Zyspec Live Test You did, however, prompt me to sharpen my code a bit since the way a blank string was handled was a bit redundant.

BTW: why it even returns a blank string is a peculiar issue. How is a blink string ending up in the array? Better yet, how could I check for and remove that from the array before choosing a random string?

Here's my update to the original code with error handling done immediately after the creation of string '$filename' instead of after creating '$image$extension':

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        
$extension ".jpg";
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
        }
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>


CGFX Live Test

Thank you again for posting, zyspec. Your code looks a lot more efficient and effective than mine. I'll probably have a play with it to see if I can get it working. :P



4
BJS3D
Re: I'm looking for a div-theme master for xoops 2.2
  • 2011/5/15 19:14

  • BJS3D

  • Just popping in

  • Posts: 7

  • Since: 2010/2/11


Thank you for this. It's amazing what a little code change can accomplish. Worked just fine in 2.5.x as well.



5
BJS3D
Re: Help with custom blocks
  • 2011/5/15 17:09

  • BJS3D

  • Just popping in

  • Posts: 7

  • Since: 2010/2/11


Understandable, this is an old thread but still valid. The problem is getting certain php code to work directly within the block. The solution is to use an iframe to load the php file.

We had a similar issue recently, attempting to generate random image links with php within a custom block. The code functioned just fine outside xoops on the test server. We concluded that it would be better to just use an html iframe within the custom block to load the php... and it worked.

Visit That Thread



6
BJS3D
TDMDownloads - Random Image Link Block
  • 2011/5/15 16:31

  • BJS3D

  • Just popping in

  • Posts: 7

  • Since: 2010/2/11


I love this module (TDMDownloads 1.6). My site co-conspirator and I wanted to come up with a block that would display a random image from the SHOTS directory which would link to its corresponding download listing.

Now, neither of us are hard-core coders. We're graphic designers by trade, but here's what I came up with. Since xoops.org has helped me immensely over the years, this is but a small contribution in return:

<?php 
// OPENS THE DIRECTORY
if ($handle opendir('uploads/TDMDownloads/images/shots/')) 
// READS THE DIRECTORY
{
    while (
false !== ($file readdir($handle))) 
// RETURNS A LIST OF FILES IN THE DIRECTORY
{
        if (
$file != "." && $file != ".."
// REMOVES THE .JPG EXTENSION FROM FILENAMES LISTED
{
        
$file substr($file, -4);
// CREATES AN ARRAY OF THE LIST WITHOUT EXTENSIONS
        
$narray[$i]=$file;
        
$i++; 
        }
// CHOOSES A RANDOM FILE FROM THE ARRAY
        
$j rand(0$i-1);
// CREATES THE STRING 'FILENAME' 
        
$filename "$narray[$j]";
// CREATES A STRING TO BE USED AS AN EXTENSION
        
$jpg ".jpg";
// ADDS THE EXTENSION BACK TO FILENAME FOR IMAGE
        
$image "$filename$jpg";
// ERROR TRAPPING - IF IMAGE NAME IS BLANK
        
if ($image==".jpg"
// CHANGES IMAGE NAME FROM BLANK TO USEFUL WITHOUT EXTENSION
{
        
$image "cid68lid2";
// RECREATES THE STRING 'FILENAME' FROM EMPTY STRING
        
$filename "$image";
// ADDS THE EXTENSION BACK TO THE IMAGE NAME
        
$image "$image$jpg";
        }
// CONVERTING RAW IMAGE NAME INTO LISTING-SPECIFIC URL
// CHANGES THE 'CID' IN FILENAME TO 'CID='
        
$filename str_replace('cid''cid='$filename);
// CHANGES THE 'LID' IN FILENAME TO '&LID='
        
$filename str_replace('lid''&lid='$filename);
        }
// ADDS STRING 'FILENAME' TO END OF URL FOR DOWNLOAD LINK
// ADDS STRING 'IMAGE' TO END OF URL FOR IMAGE DISPLAY
            
echo "<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
// CLOSES THE DIRECTORY BEFORE END SCRIPT
    
closedir($handle);
}
?>


This is saved to file.php and called into a block using an iframe like this:

<Iframe src="http://coastalgfx.com/randcgi.php" width="160" height="160" frameborder="0" scrolling="no"></Iframe>


See it in action here

Now, the script often returns a blank random filename and the reason eludes me. I've error-handled this by setting a default filename in case there's a blank return but I'm sure someone can greatly improve upon this.

* It should be noted that, in order for this to work as intended, shot images must be named accordingly as cid#lid#.jpg (change extension string if using other formats) as each shot relates to its corresponding download.

Kudos to TDM for an awesome and highly extensible module.




TopTop



Login

Who's Online

76 user(s) are online (40 user(s) are browsing Support Forums)


Members: 0


Guests: 76


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Aug 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits