Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Rotating a link and .gif

    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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

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

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #5
    New Coder
    Join Date
    Mar 2008
    Location
    Finland
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post
    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 function to array the results from each |

  • #6
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    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

  • Users who have thanked Arnaud for this post:

    chrisk1152 (04-01-2008)

  • #8
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you that helped alot. I think that I can figure it out from there http://codingforums.com/images/smilies/thumbsup.gif

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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

    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:

    Code:
    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:

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


    PHP Code:
    //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"
    Last edited by Fumigator; 04-01-2008 at 10:10 PM. Reason: fixed explode() parms

  • Users who have thanked Fumigator for this post:

    chrisk1152 (04-01-2008)

  • #10
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #11
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #12
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Reread the comment above your most current. Fumigator shows you how to do what you're attempting using a text file.

    Edit: Guess we were typing at the same time :P
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #13
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?
    Last edited by chrisk1152; 04-01-2008 at 11:22 PM. Reason: posted wrong line #

  • #14
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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>

  • #15
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    am I just doing this wrong... I really am new to php.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •