...

View Full Version : Rotating a link and .gif



chrisk1152
04-01-2008, 01:49 AM
Hello,

I am working on a site that has a game section and I would like to have a link to one game on the front page. Currently I have to change it manually each time that I want something different.

My question is how do I insert a rotating random link that shows the .Gif file and links to the right page?

Thanks for your help,

Chris

Fumigator
04-01-2008, 02:19 AM
Assuming all the images are in a separate folder, use the glob() function to build an array that contains a list of the filenames in that folder. Then use the array_rand() function to randomly select an image from that array. Then create the HTML tags to display the selected image.



$imageArray = glob("/images/*.gif");
$selectedImage = array_rand($imageArray);
print "<img src=\"$selectedImage\">\n"; //

chrisk1152
04-01-2008, 02:31 AM
Yes but that only allows me to see the picture but that does not have a link to it. I want to basicly have the picture and the link that will send it to the right page the game is on?

Here is a link to the site. http://www.restoredsolia.org

I want to keep it looking like it does now but have the image in the middle rotate with a link to the game image it goes to.

Fumigator
04-01-2008, 03:26 AM
Oh yeah, you did mention that part, didn't you. I forgot about the link thing. I'm not thinking to cleverly right now so the only solution I can think of is store the image filename and the link URL in a file or in a database and go out and grab it when you select the image.

Mikroz
04-01-2008, 11:22 AM
Yeah I would also suggest the same thing Fumigator already said, using a database making a table with id, img, url then just randomize the id and take the row results.

For a txt file maybe using something like id|img|url and then using Explode (http://fi.php.net/manual/en/function.explode.php) function to array the results from each |

chrisk1152
04-01-2008, 09:19 PM
Can you suggest a place to goto so I can see how that is supposed to go? I am fairly new to PHP and and still trying to figure things out. Is there a site that I could see sample coding?

Arnaud
04-01-2008, 09:32 PM
I don't have much time now and don't know your coding experience but let's make it quick...

You create a database table with fields:
- ID (INT, autoincrement, primary)
- IMAGE (VARCHAR)
- LINK (VARCHAR)

ID will be the reference number
IMAGE = the relative path to your image file (images/filexy.jpg)
LINK = the link you want to add to the image
etc.

then, as Fumigator says, query your database and paste the results into array(s) and use array_rand to randomize the display

sorry no time to give you code examples... good luck

chrisk1152
04-01-2008, 10:03 PM
Thank you that helped alot. I think that I can figure it out from there http://codingforums.com/images/smilies/thumbsup.gif
:thumbsup:

Fumigator
04-01-2008, 10:08 PM
Can I suggest using a file instead of a database? Since this is such a straight-forward problem to solve, I think it will be helpful for you to see how things can be done in the simplest way possible.

You may decide later to use a database, but there's nothing wrong with learning both ways-- makes you a better programmer. Gives you skillz, and chicks dig skillz :p

Anyways here's the idea. In your file you will store the image filename, a separator character (the pipe | should do nicely) and then the URL for that image. Here's an example:



pacman.gif|http://www.pacmanglory.com
mspacman.gif|http://www.mspacman.com
spaceinvaders.gif|http://www.spaceinvadersfanatics.org
asteroids.gif|http://www.asteroidlovers.com


Create this file by hand and put it in the directory your PHP script will run.

Your script now needs to do the following:


Read the file into an array
Pick a random element from the array
Separate the image filename from the URL link
Build the HTML to display the and provide the link.




//Read the file into an array
$gamesArray = file("gamesfile.txt");

//Pick a random element from the array
$selectedGame = array_rand($gamesArray);

//Separate the image filename from the URL link using the explode()function
//This puts the image into $selectedArray[0]
//and puts the URL into $selectedArray[1]
$selectedArray = explode("|", $selectedGame);

//Get fancy by finding the dimensions of the image
$imgData = getimagesize($selectedArray[0]);

//Build the HTML
print "<a href=\"{$selectedArray[1]}\">\n";
print "<img src=\"{$selectedArray[0]}\" {$imgData[3]} title=\"Game image\" alt=\"Game image\">\n";
print "</a>\n";

chrisk1152
04-01-2008, 10:42 PM
Ok so I can't figure it out. having trouble making the tables. Can someone point me to a site that can show me how to make tables so I can get this done?

chrisk1152
04-01-2008, 10:46 PM
Fumigator thank you so much that is exactly what I was looking for. That will make it so much simpler.

I would still like to learn how to do the database way also for other things I have planned.

StupidRalph
04-01-2008, 10:46 PM
Reread the comment above your most current. Fumigator shows you how to do what you're attempting using a text file.

Guess we were typing at the same time :P

chrisk1152
04-01-2008, 11:10 PM
I get this error when I use that coding:

Parse error: syntax error, unexpected T_VARIABLE in /home/content/n/a/v/navadia/html/welcome.php on line 35

what is wrong with it?

chrisk1152
04-01-2008, 11:20 PM
here is the code from line 35 on:

<p align="center" class="style1">
<?php$//Read the file into an array
$gamesArray = file("gamesfile.txt");

//Pick a random element from the array
$selectedGame = array_rand($gamesArray);

//Separate the image filename from the URL link using the explode()function
//This puts the image into $selectedArray[0]
//and puts the URL into $selectedArray[1]
$selectedArray = explode("|", $selectedGame);

//Get fancy by finding the dimensions of the image
$imgData = getimagesize($selectedArray[0]);

//Build the HTML
print "<a href=\"{$selectedArray[1]}\">\n";
print "<img src=\"{$selectedArray[0]}\" {$imgData[3]} title=\"Game image\" alt=\"Game image\">\n";
print "</a>\n"; ?>&nbsp;</p>

chrisk1152
04-01-2008, 11:20 PM
am I just doing this wrong... I really am new to php.

Fumigator
04-01-2008, 11:36 PM
You have an errant dollar sign that is making PHP think there's a variable there where there's not. Remove that stray dollar sign.

Just a note, I gave you code to get started with; it may not work perfectly but gives you the general idea.

Arnaud
04-02-2008, 09:43 AM
This

<?php$//Read the file into an array

Change to:

<?php
//Read the file into an array
$gamesArray = file("gamesfile.txt");
...

chrisk1152
04-02-2008, 04:57 PM
http://www.restoredsolia.org/welcome.php

Now I'm getting links that go nowhere...
here is the updated script:

<?php
//Read the file into an array
$gamesArray = file("gamesfile.txt");

//Pick a random element from the array
$selectedGame = array_rand($gamesArray);

//Separate the image filename from the URL link using the explode()function
//This puts the image into $selectedArray[0]
//and puts the URL into $selectedArray[1]
$selectedArray = explode("|", $selectedGame);

//Get fancy by finding the dimensions of the image
$imgData = getimagesize($selectedArray[0]);

//Build the HTML
print "<a href=\"{$selectedArray[1]}\">\n";
print "<img src=\"{$selectedArray[0]}\" {$imgData[3]} title=\"Game image\" alt=\"Game image\">\n";
print "</a>\n";
?>

Also how do I get it to target an iframe? if i add the target="iframe" line it throws up another error.

I would like to thank everyone who has helped so far.

Fumigator
04-02-2008, 07:36 PM
Do a "view source" on the page and figure out what's missing. Your file is created, right?

chrisk1152
04-02-2008, 09:31 PM
Here is what is says for a view source:

<br />
<b>Warning</b>: getimagesize(0) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: No such file or directory in <b>/home/content/n/a/v/navadia/html/welcome.php</b> on line <b>51</b><br />

<a href="">
<img src="0" title="Game image" alt="Game image">
</a>
</p>

I'm not sure why it's doing this

Fumigator
04-02-2008, 10:16 PM
Your link's not working for me...

Anyways like I said, the code I provided was just a starting point, it may need some corrections to get working fully. You've got a problem with the value you're passing to the getimagesize() function.

What I can see is, it doesn't appear that you have a file named gamesfile.txt with the correctly formatted data in it that this script is expecting.

Arnaud
04-03-2008, 10:09 AM
Looks like it's working, isn't it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum