Author Topic: Need help on randomly displaying images w/ hyperlinks  (Read 59300 times)

0 Members and 1 Guest are viewing this topic.

fesworks

  • Administrator
  • *****
  • Posts: 923
    • View Profile
    • http://www.fesworks.com
Need help on randomly displaying images w/ hyperlinks
« on: November 09, 2006, 01:27:04 am »
This is going to be in a *.php page.. so I guess I need code for PHP, JAVA, or HTML.

But I am wanting to have small image links to other webcomics, but I would like them to cycle through either timed, or per refresh... either as an ordered list or random... and then with each image that shows, there is a new link associated with that image. I think I used to be able to randomly display images before, but never with changing the  hyperlink.

is this easy to do? does anyone know how to do that, or at least tell me some key words of which I should look for the "how to" myself?

Thanks in advance.

dragyn

  • Sr. Member
  • ****
  • Posts: 282
    • View Profile
    • Pending Else
Need help on randomly displaying images w/ hyperlinks
« Reply #1 on: November 09, 2006, 06:30:59 am »
I'll have to watch this thread, as this is something I've been wondering about for a bit myself.

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,328
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Need help on randomly displaying images w/ hyperlinks
« Reply #2 on: November 09, 2006, 08:09:12 am »
Code: [Select]
$random = rand(10,13);

if($random == 1) { $logo = "xepher_logo_fredirc.gif"; }
if($random == 2) { $logo = "xepher_logo_nerdboy.png"; }
if($random == 3) { $logo = "xepher_logo_gwyn.gif"; }
if($random == 4) { $logo = "xepher_logo_icy.gif"; }
if($random == 5) { $logo = "xepher_logo_nerdboy2.png"; }
if($random == 6) { $logo = "xepher_logo_technostyle.jpg"; }
if($random == 7) { $logo = "xepher_logo_gwyn2.gif"; }
if($random == 8) { $logo = "xepher_logo_willsan.png"; }
if($random == 9) { $logo = "xepher_logo_databits.gif"; }
if($random == 11) { $logo = "XepherNet.jpg"; }
if($random == 12) { $logo = "XepherNet2.jpg"; }
if($random == 13) { $logo = "XepherNet3.jpg"; }


if($random == 10) { ?>

XEPHER.NET

Free Hosting for Free Thinkers

}else{
echo("XepherNet Logo");
}

?>
That file is http://xepher.net/logos/randomlogo.php and is included at the top of all main xepher.net pages to rotate the logo. You'll note in the code, I'm only using logos 10 through 13, as the other logos were user-submitted years ago, and no longer fit the layout. Logo "10" is a special case, and spits out a two line text logo. Hopefully you can see how you can easily include additional code to change the hyperlink in the same way. Also note this is VERY ugly/primitive code, being that you have to add a line for each new image you add. Of course, it adds a bit of security that way, as no one can trick it into including a file I haven't specifically listed. If I had a ton of images to rotate, I'd write something that just pulls from a directory listing, but as I've yet to do that, I can't actually post code here for it.

Databits

  • Global Moderator
  • *****
  • Posts: 1,607
  • Programming's not just a science, it's an art.
    • View Profile
Need help on randomly displaying images w/ hyperlinks
« Reply #3 on: November 09, 2006, 05:03:53 pm »
or if you wanna be more complex, you could use a database to store image location information in a sequential order, then simply do the random thing between the number of rows in that table, pull that row, and use that image.
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

fesworks

  • Administrator
  • *****
  • Posts: 923
    • View Profile
    • http://www.fesworks.com
Need help on randomly displaying images w/ hyperlinks
« Reply #4 on: November 09, 2006, 09:33:06 pm »
Quote from: Xepher
That file is http://xepher.net/logos/randomlogo.php and is included at the top of all main xepher.net pages to rotate the logo. You'll note in the code, I'm only using logos 10 through 13, as the other logos were user-submitted years ago, and no longer fit the layout. Logo "10" is a special case, and spits out a two line text logo. Hopefully you can see how you can easily include additional code to change the hyperlink in the same way. Also note this is VERY ugly/primitive code, being that you have to add a line for each new image you add. Of course, it adds a bit of security that way, as no one can trick it into including a file I haven't specifically listed. If I had a ton of images to rotate, I'd write something that just pulls from a directory listing, but as I've yet to do that, I can't actually post code here for it.
Ah yes! Thanks you! this will help when I re-vamp the site and images.

I suppose I could also do this code a few times with seperate sets of images for each, so I can have 2 different Webcomic Links in 2 different spots (so I don't get the same).. though I am sure that doing more would make the load time of the home page much longer...


Quote from: Databits
or if you wanna be more complex, ...
ooh, no no, that's ok ;)

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,328
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Need help on randomly displaying images w/ hyperlinks
« Reply #5 on: November 10, 2006, 12:28:06 am »
I wouldn't worry about load time increasing if you want to add more stuff. This code is so simple, it takes an order of magnitude longer to actually send the page than it does to process it. And if you want to throw out two random links, just include the file in both places on your actual page.

Lemme give this a shot...

Code: [Select]
$random = rand(1,3);

if($random == 1) { $img = "comic1.gif"; $link = "http://www.comic1.net"; }
if($random == 2) { $img = "comic2.png"; $link = "http://www.comic-2.net"; }
if($random == 3) { $img = "othercomic.jpg"; $link = "http://www.comic-other.com"; }

echo("");

?>
There's a three-way example. Just add more lines and change the "rand(1,3)" to a match the number of lines to scale it up.

dragyn

  • Sr. Member
  • ****
  • Posts: 282
    • View Profile
    • Pending Else
Need help on randomly displaying images w/ hyperlinks
« Reply #6 on: November 10, 2006, 12:53:18 am »
Alright.  Thanks!  I'll keep this thread in mind for when I actually implement any randomization.

And yeah...that's plenty complex for me.

dragyn

  • Sr. Member
  • ****
  • Posts: 282
    • View Profile
    • Pending Else
Need help on randomly displaying images w/ hyperlinks
« Reply #7 on: November 10, 2006, 06:56:29 am »
Yay, implementation!  

Eventually, I'm trying to have a banner for all my characters, but for now, I only have 4.

Thanks for asking, Fes.  It's always fun to use the answer to someone else's question.


dragyn

  • Sr. Member
  • ****
  • Posts: 282
    • View Profile
    • Pending Else
Need help on randomly displaying images w/ hyperlinks
« Reply #9 on: November 13, 2006, 08:27:46 pm »
Yup!

fesworks

  • Administrator
  • *****
  • Posts: 923
    • View Profile
    • http://www.fesworks.com
Need help on randomly displaying images w/ hyperlinks
« Reply #10 on: March 02, 2007, 02:13:59 am »
New question. How can I make this go in order instead of random?

AND, like that one where it shows text instead of an image, would I be able to show an Image w/link AND text?


I'm thining about that "Xepher.net Comics" cycling image thing I was talking about. That other code is copyrighted and specially made, And I can't really get a hold of the author.

So I'm thinking about using this generator and using it in an iFrame tag, so that it can be clicked back and form within a page, without reloading the main page (also to save the loading time of the main page, the iFrame can load seperately).... unless there is a PHP way to make a reloadable field.

I know I don't know PHP code, but If I can get the basic code, I can do all the tedious work of getting banners and and the list making.

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,328
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Need help on randomly displaying images w/ hyperlinks
« Reply #11 on: March 02, 2007, 09:43:15 pm »
Define what you mean by "in order." If you've got a lot of vistors all showing up, do you want the first one to see image 1, the second see image 2, etc.? Or do you mean some way to track so that each person sees the images in order as they click new pages?

As for the second bit, you can make it output anything you want. It basically just selects a snippit of HTML to spit out. You can make that snippit anything you want, from IMG tags, to text, to links... whatever HTML you want. It's just replacing any of those $variables with the text they're set to. Add more as you need.

If you do it in an iframe, you need to make a full "page" of html around the php code... you know, the HTML and BODY tags and such.

fesworks

  • Administrator
  • *****
  • Posts: 923
    • View Profile
    • http://www.fesworks.com
Need help on randomly displaying images w/ hyperlinks
« Reply #12 on: March 03, 2007, 01:37:14 am »
Yea, I know that about the iFrame.

as far as the "order", basically the PHP "random" is a piss poor randomizer. I got a lot of repeats... I want something that will display a differentthing each time until all have been displayed... I mean the first one could be random, but then there should be a Hyperlink to click that will go to the next one in series.... and so on...

see, I'm trying to figure out a way to do a PHP version of that other Java Script one that I can't get permission to use... plus This might be easier if I can get the basic codes.

Am I making sense? I hope I am :P

Omega0

  • Newbie
  • *
  • Posts: 46
    • View Profile
    • http://omega0.xepher.net
Need help on randomly displaying images w/ hyperlinks
« Reply #13 on: March 03, 2007, 03:14:43 pm »
Is this what you want?
Code: [Select]
if( isset( $HTTP_COOKIE_VARS['BANNER_NUMBER'] ) )
{
$banner = $HTTP_COOKIE_VARS['BANNER_NUMBER'];
$banner = ($banner + 1) % 3;
}
else
{
$banner = rand(0,2);
}
setcookie( 'BANNER_NUMBER', $banner, time()+(60) );
?>
When someone first visits the page, they get a rand() banner.  Then each time they reload it after they get the next banner in series (you'd want to change the (60) in the setcookie to something longer, like (60*60*24*30)).
Demo: http://omega0.xepher.net/stuff/banners.php

If you want to be really tricky, and randomize it further, you could do something like this:
Code: [Select]
if( isset( $HTTP_COOKIE_VARS['BANNER_ARRAY'] ) ) {
$banners = split( '#', $HTTP_COOKIE_VARS['BANNER_ARRAY'] );
}
else {
$banners = array(0,1,2);
}

if( count($banners) == 1 ) {
$banner = $banners[0];
$newbanners = '0#1#2';
}
else {
shuffle( $banners );
$uns = $banners;
$banner = array_shift($banners);
$newbanners = array_shift($banners);
while( $temp = array_shift($banners) )
{
$newbanners = $newbanners . '#' . $temp;
}
}
setcookie( 'BANNER_ARRAY', $newbanners, time()+(60*60) );
What this does, is store a list of undisplayed banners in a cookie like 1#2#3.  On each load, it mixes up the list, pulls of the top one to display now and then stores the remaining choices in a cookie.  So the viewer can't get the first one again until all the others have been viewed, but he won't get them in the same order every time (but you could get a repeat like this: 1,2,3,3,1,2 since after the 1,2,3 they've all been viewed and it could very well start with 3 on the next run through of the list.  With a large list though, this becomes rare, and you could modify the code to exclude the last one when reloading $newbanners with the full list).
Demo: http://omega0.xepher.net/stuff/banners2.php

If you don't care about tracking each user individually (or care about those people who turn off cookies), you can do something like this:
Code: [Select]
$f = fopen("bannerlist.txt","r");
$banner = fgets($f);
fclose($f);
$f = fopen("bannerlist.txt","w");
$next = ($banner + 1) % 3;
fputs( $f, $next );
fclose($f);
?>
All this does is store the value of what the next banner should be in a text file and does open, read, increment, save on each load.  And you could always combine #2 and #3.

Demo: http://omega0.xepher.net/stuff/banners3.php
The text file is: http://omega0.xepher.net/stuff/bannerlist.txt

So, does any of this do what you want?

And does anyone know a simpler way for what I did in #2?  I can't believe that (in a perl-like language no less) there wouldn't be an easy array->string converter.

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,328
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Need help on randomly displaying images w/ hyperlinks
« Reply #14 on: March 04, 2007, 10:08:18 pm »
The PHP random does a pretty good randomization. Of course, with any small-ish set of options, true random IS a lot of repeats. Obviously that's not what you want though. I think Omega's suggestions here are pretty good to do what you want. He also showed you both ways (per-user/cookie based selection, and site-wide with the bannerlist.txt, which were what I was asking about in my earlier post) so just figure out which one works for your needs and try it out.

And no, I don't know of a better way to do array->string off hand, though I'm sure there must be one... there always is, no matter what function I'm trying to do. :-)