TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
Stats
  • Total Posts: 196,004
  • Total Topics: 21,330
  • Online today: 219
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 111
  • Total: 111

block snippet help

Started by mikeh, October 24, 2006, 02:50:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

mikeh

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"); ?>

JPDeni

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.

mikeh

#2
guess my scripting isn.t that good, i  can't even get this to work in a TP block.

JPDeni

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.

mikeh

what i am trying to accomplishes is a rotating advertizment block.

JPDeni

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?

mikeh

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.

JPDeni

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.

mikeh

I'll leave the coding to the experts.


This website is proudly hosted on Crocweb Cloud Website Hosting.