28, May 2012

What is this code and this operator? - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: What is this code and this operator?
« previous next »
Pages: [1] 2 Print
Instabuck - The easy way to sell digital products online

Author Topic: What is this code and this operator?  (Read 2634 times)
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« on: Aug 29, 2006, 08:15:41 pm »

Hi All,

Found this code and it does exactly what I want, but I need to execute multiple times on the same page. The idea is to have a group of image files in a directory and use this code to randomly display one. Since I use it up to three times on the same page and it appears to use the time as the seed for the calculation, without modification it shows the same image 3 times.

I added something and it is working now, but would like to understand it better. My solution was to change a line of code and save the file with a new name. I have 3 files and vary which one is called.

Here's the file:

Quote
<?php

    $folder = '.';

    $extList = array();
    $extList['gif'] = 'image/gif';
    $extList['jpg'] = 'image/jpeg';
    $extList['jpeg'] = 'image/jpeg';
    $extList['png'] = 'image/png';

$img = null;

if (substr($folder,-1) != '/') {
    $folder = $folder.'/';
}

if (isset($_GET['img'])) {
    $imageInfo = pathinfo($_GET['img']);
    if (
        isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
) {
    $img = $folder.$imageInfo['basename'];
}
} else {
    $fileList = array();
    $handle = opendir($folder);
    while ( false !== ( $file = readdir($handle) ) ) {
        $file_info = pathinfo($file);
        if (
            isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
            $fileList[] = $file;
        }
    }
    closedir($handle);

    if (count($fileList) > 0) {
        $imageNumber = time() % count($fileList);
        $img = $folder.$fileList[$imageNumber];
    }
}
if ($img!=null) {
    $imageInfo = pathinfo($img);
    $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    header ($contentType);
    readfile($img);
} else {
    if ( function_exists('imagecreate') ) {
        header ("Content-type: image/png");
        $im = @imagecreate (100, 100)
            or die ("Cannot initialize new GD image stream");
        $background_color = imagecolorallocate ($im, 255, 255, 255);
        $text_color = imagecolorallocate ($im, 0,0,0);
        imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
        imagepng ($im);
        imagedestroy($im);
    }
}
?>

I modifiied this line:

Quote
$imageNumber = time() % count($fileList) + 0.5;

using 0.5 for one file and 1 for another. When I tried 2 it didn't work - which makes me think this sets $imageNumber to a decimal less than 1 but more than 0?

My question is what does the % operator do?

If I understood this code better, maybe there would be a way to only need one file instead of 3.

Thanks.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Aug 29, 2006, 08:49:36 pm »

Code:
$imageNumber = time() % count($fileList);

check this from the php manual:
http://nl2.php.net/manual/en/language.operators.arithmetic.php

EDIT: the code looks like some random image/banner generator...
« Last Edit: Aug 29, 2006, 08:51:35 pm by olaf »

Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #2 on: Aug 29, 2006, 08:54:30 pm »

Thanks for the link. That seems vaguely familiar from another language I've worked with.

Any idea on how to modify the script so I don't need 3 different copies to generate 3 different images on the same page?

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Aug 29, 2006, 08:58:30 pm »


Any idea on how to modify the script so I don't need 3 different copies to generate 3 different images on the same page?

as I can see this script is written to use with different images:
this variable must holder the image name: $_GET['img']

Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #4 on: Aug 29, 2006, 09:02:20 pm »

here's some code I'm using to display random images:

Code:
<?php 
$DIRNAME 
"./images/randoms/";   // Directory that contains the images

function DisplayRandomImage() 
{
global $DIRNAME;
$msg ""; // IMG tag to be returned
$y 0; // counter for the images
$images = array();       // array to hold the list of images to display randomly
$file_handle "";


if (is_dir($DIRNAME)) 
{
$file_handle=opendir($DIRNAME); 
$y 0;
while ($file readdir($file_handle))  // while there's files to read....

     if (!is_dir($DIRNAME $file)) 
{
  $pieces explode ("."$file);
if ($pieces[0] && $pieces[1])  // filename.ext
{
if (($pieces[1]=="gif"))  // add only .gif files
{
     $images[$y] = $DIRNAME $file
     $y += 1;
}
}
}
}
closedir($file_handle); 
if (count($images) > 0
{
        
srand((double)microtime()*1000000);
$y rand(0,(count($images) - 1));
$msg "<IMG SRC=\"" $images[$y] . "\" width=\"30\" height=\"80\" alt=\"\">";

else 
{
$msg "No image found in \"" $DIRNAME "\"\n";
}

else 
{
$msg "\"" $DIRNAME "\" is not a valid directory!\n";
}
return $msg;

?>

This code is in the top of my page, after that HTML is used to create the page and where I want to display a random image I use
Code:
<?=DisplayRandomImage();?>


Last blog : Are You Stumbling Yet?
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #5 on: Aug 29, 2006, 09:19:18 pm »

Does that code require building a file list? Many of the others I looked at do.

I can call the routine I use multiple times, unfortunately it is displaying the same image - I would guess the time only changes by seconds and therefore the resulting value is the same.

Do you use this with more than one image on the page?

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #6 on: Aug 29, 2006, 09:46:56 pm »

Code:
$DIRNAME = "./images/randoms/";
refers to the directory the script looks at (in this case mydomain.com/images/randoms/) The scrip retrieves all the .gif images and displays a random one. If you have other images besides .gif you can change:
Code:
if (($pieces[1]=="gif"))
to:
Code:
if (($pieces[1]=="gif") OR ($pieces[1]=="jpg"))
for instance...
hope this helps!

edit: yes I use it with more then one image here: http://www.letsworkonline.com/ (its for the 2 images on the left next to the google ads) it can sometimes display the same image twice, but I guess that chance lowers when you add more images to your random image folder...
« Last Edit: Aug 29, 2006, 09:55:14 pm by Mind_nl »


Last blog : Are You Stumbling Yet?
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #7 on: Aug 29, 2006, 10:26:58 pm »

Thank you, I'll have to play with that some.

I noticed when testing that the incidence of the same image coming up goes down as the number of images in the directory goes up.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #8 on: Aug 29, 2006, 10:29:18 pm »

I like Mind_nl's snippet, it can do what you want, and actually it can be optimized a little.

If you are about to have only the images in the $DIRNAME directory then you can replace this :

Code:
<?php
$pieces 
explode ("."$file);
if (
$pieces[0] && $pieces[1])  // filename.ext
{
if (($pieces[1]=="gif"))  // add only .gif files
{
     
$images[$y] = $DIRNAME $file
    
$y += 1;
}
}

with this :

Code:
<?php
if ( is_file $file ) )
{
     
$images[$y] = $DIRNAME $file
    
$y += 1;
}

This way it will work with any type of file too Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #9 on: Aug 29, 2006, 10:59:59 pm »

Cool mod Nik, in my case the images are in a directory by themself.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #10 on: Aug 29, 2006, 11:04:32 pm »

I just took a closer look and realized that this script is showing one random image.

You want one for as many as you want right?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #11 on: Aug 29, 2006, 11:12:09 pm »

No I think this is exactly what he wants (from what I read in the first post) one random image displayed, but the script called more then one time so he gets random images in different places on his page, thats just how I use this snippet
Code:
<?=DisplayRandomImage();?>
in different places in my HTML

edit: I like your optimization, but I often keep my photoshop files in the same directory (on my PC) and they sometimes get uploaded to the site too, I don't think displaying a .PSD file will work this way, so I'll leave in the part you optimized out  Wink
« Last Edit: Aug 29, 2006, 11:15:39 pm by Mind_nl »


Last blog : Are You Stumbling Yet?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #12 on: Aug 29, 2006, 11:17:17 pm »

No I think this is exactly what he wants (from what I read in the first post) one random image displayed, but the script called more then one time so he gets random images in different places on his page, thats just how I use this snippet
Code:
<?=DisplayRandomImage();?>
in different places in my HTML

Correct, except than the he Smiley

BTW I think this is a nice idea for the code gallery. I will make a snippet for random images tomorrow and post it there Smiley

The only bad thing about this snippet is that when you use it for more than one images, it can show the same image twice or more times....

But I will fix that Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #13 on: Aug 29, 2006, 11:49:54 pm »

Yes, like I said before: it sometimes shows the same image more than once. I'd be very interested if you can fix that!


Last blog : Are You Stumbling Yet?
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #14 on: Aug 30, 2006, 12:23:48 am »

<Spoken in her deepest manliest voice...  Kiss >

Yes, Nik, I would love it if you could fix that too.

So, glad I came here and asked about this.  Smiley

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #15 on: Aug 30, 2006, 10:24:37 am »

Ok, I will mark this thread as unread, and later on I will post you the code fragment you need Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #16 on: Aug 31, 2006, 12:45:54 pm »

Ok the snippet is done, with some extra feautures Smiley

http://www.webdigity.com/index.php?action=tutorial;code=32

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #17 on: Aug 31, 2006, 01:08:27 pm »

Good work Nick! I'll be replacing my current random image script with this version.
One thing though: the code for displaying one random image is the same as for displaying more then one, looks like the second code line is missing something. Should that be:
Code:
<?php 
     
echo RandomImage(3);
to display 3 random images?


Last blog : Are You Stumbling Yet?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #18 on: Aug 31, 2006, 01:14:07 pm »

lol, that happens when you copy/paste Tongue

I have fixed that...

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #19 on: Sep 01, 2006, 02:40:50 am »

Nice job Nik.

I do have a couple of questions.

I would use the code multiple times in the same page, but not with the same function call, will that still keep the same image from appearing?

I will be using it with the script for my directory. Can this be easily saved as a separate file and then called from the template file?

I opted not to take the php class at the local community college, with the directory, I'm now sorry I didn't. I could probably learn it from a book, but the structure of a class makes it easier.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3880
Tags : google html photoshop webdigity snippets Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: What is this code and this operator?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 03:32:34 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.