TinyPortal

Development => Support => Topic started by: mikeh on October 24, 2006, 02:50:37 AM

Title: block snippet help
Post by: mikeh on October 24, 2006, 02:50:37 AM
I have a php code i would to get working for a block advertizment block.

here is the code:

<?php 



if ($dir opendir(".")) 



     
$list buildimagearray($dir); 

     
displayrandomimage($list); 





// This function reads all the files in the current directory and adds all image files to the array $list[] 



function buildimagearray($dir



     while (
false !== ($file readdir($dir))) 

     { 

          if (!
is_dir($file) && getimagesize($file)) 

          { 

               
$list[] = $file

          } 

     } 

     return 
$list





// This function selects a random image, determines the mime type, opens the file for reading, 

// and then outputs the image 



function displayrandomimage($list



     
srand ((double) microtime() * 10000000); 

     
$sig array_rand ($list); 



     
$size getimagesize ($list[$sig]); 

     
$fp fopen($list[$sig], "rb"); 



     if (
$size && $fp

     { 

          
header("Content-type: {$size['mime']}"); 

          
fpassthru($fp); 

          exit; 

     } 



?>


any help woild be appreciated. I even put in a php block: <? php include("directory\index.php"); ?>
Title: Re: block snippet help
Post by: JPDeni on October 24, 2006, 03:29:33 AM
First, if you are using a php block, you never use <? php and ?>.

Second, you should be able to just put the code into a block, rather than using an "include." The only thing I would suggest is that you specify the directory for the images. You will probably need to include the entire path, instead of a relative one.
Title: Re: block snippet help
Post by: mikeh on October 28, 2006, 03:33:02 PM
guess my scripting isn.t that good, i  can't even get this to work in a TP block.
Title: Re: block snippet help
Post by: JPDeni on October 28, 2006, 03:59:29 PM
I don't see that it actually does anything. In order to show something on the page you have to have an "echo" statement.

I have a little routine I use to randomly order files in a directory. I use it several times, so I created a file called UserFunctions.php, which I put in the Sources directory. (I mentioned this file in my Greetings block snippet. I keep all the functions I write in this file.) Here's the function:
  function shufflefiles($current_dir) {
    $dir = opendir($current_dir);

    while ($file = readdir($dir))
    {
      if ($file != '.' && $file != '..')
      {
        $array[] = $file;
      }
    }
    closedir($dir);
    shuffle($array);
    return $array;
  }


When I want to use it in a php article or php block or in any other template file, I use:

require_once("UserFunctions.php");
$dir =  "directory/"; // The directory name, relative to the root directory of your forum
$random_file_array=shufflefiles($dir);

At that point, you will have all of the file names from that directory in random order in an array called $random_file_array. To pick just one of them, use $random_file_array[0].

Does this help any? If you need more help, I need to know exactly what you want to accomplish.
Title: Re: block snippet help
Post by: mikeh on October 28, 2006, 05:07:09 PM
what i am trying to accomplishes is a rotating advertizment block.
Title: Re: block snippet help
Post by: JPDeni on October 28, 2006, 05:33:28 PM
I understand that. What I need to know is how it works.

You've got files (images?) in a directory. You want to display them randomly, right? Or does there have to be a specific rotation? (That would be much more complicated.) With links? If so, where are the URLs for the links stored?
Title: Re: block snippet help
Post by: mikeh on October 28, 2006, 05:42:44 PM
it would be randomly shown images, and ya know i never thought about the links for the images in the block  :o  they would have to be data base driven.
Title: Re: block snippet help
Post by: JPDeni on October 28, 2006, 06:01:07 PM
Displaying the images is easy. All you have to do is shuffle the files in a directory and then print out the first one.

Depending on how many URLs you have, you could put them all into the code in the block. Here's an example of how.

Let's say you have an image for acme.com. They have an image that's "acmepic.jpg" and the URL is "http://www.acme.com". The image file goes into your directory. The URL goes into your block like

$url_array['acmepic.jpg'] = 'http://www.acme.com';


Notice that the part in the brackets is the same as the image.

Building on the code I posted earlier...

Create your UserFunctions.php file, with the shuffle files function. Then use the code below:



// Define your urls here. Make sure you have one URL for each of the images in your directory
$url_array['acmepic'] = 'http://www.acme.com';

require_once("UserFunctions.php");
$dir =  "directory/"; // The directory name where your ad images are, relative to the root directory of your forum
$image_url = "http://yoursite.com/directory"; // The URL to the directory of your ad images.
$random_file_array=shufflefiles($dir);

$image = $random_file_array[0];
$url = $url_array[$image];
echo '<a href="',$url,'" target="_blank"><img src="',$image,'"></a>';


You can get fancier with it and include the size of the image (which is good coding practice) and alt tags (also good practice), but the above ought to print out your images and make them clickable.
Title: Re: block snippet help
Post by: mikeh on October 28, 2006, 09:13:25 PM
I'll leave the coding to the experts.
Title: Re: block snippet help
Post by: gerrymo on October 28, 2006, 09:20:12 PM
No coding requiered.

http://www.tinyportal.net/smf/index.php?topic=9924.0