Jump to content

[SOLVED] Randomized linked images (urls in .txt)


Chuck D

Recommended Posts

Ok, I need help finding a simple way to get links from a text file and displaying them all linked to an image, and possible randomizing the order too... if you need more info, or can help, it would be much appreciated!

 

I am trying to make it easier on myself because the links change often and it would just save me a lot of time just edite\ing a list instead of a bunch of html :(

 

something like:

 

link1  link2  link3  link4

 

then on refresh:

 

link3  link1  link4  link2

 

and have the list like:

 

link1

link2

link3

link4

 

I don't think it should be too difficult but I am new to php and cannot find what I am looking for on php.net. thanks again!

Link to comment
Share on other sites

heres a quick one

<?php
//file of links
$filename = "links.txt";
$link = array();

$handle = @fopen($filename, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096); // read line by line
        $link[] = $buffer; //store in array
    }
    fclose($handle);
}
shuffle($link); // mix up the array
foreach($link as $l) //loop and print the links
{
echo "<a href=$l>$l</a> | ";
}

?> 

 

links.txt

http://www.phpfreaks.com/articles.php
http://www.phpfreaks.com/tutorials.php
http://www.phpfreaks.com/quickcode.php
http://www.phpfreaks.com/forums/index.php/topic,138161.0.html

 

 

normally i would point you to the freelance section but i had a great day ;)

 

this code will probably need cleaning up, but its a start

 

:)

 

 

EDIT: Added some comments in the code

Link to comment
Share on other sites

ugh...

 

<a href=whatever.com<br>>whatever.com</a>

 

and its not doing that on the last one so im assuming its addinf a br because the txt has them on new lines... but i dont know how to fix

 

what i think its doing is adding the new line onto the end of the link and thats conflicting with the code making it like this:

 

<a href=whatever.com
>whatever.com
</a>

Link to comment
Share on other sites

i dont have any html in my text file and i copied the code just as you wrote it, its just that it putting the line break after the links, is theyre an array that will make it not read the line break or take it off?... its not any of my coding that has a break code

Link to comment
Share on other sites

Ahhhh

 

ok remove the line breaks from the array..

 

add them on the display part

 

<?php
//file of links
$filename = "links.txt";
$link = array();

$handle = @fopen($filename, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096); // read line by line
        $link[] = $buffer; //store in array // <--Keep as is
    }
    fclose($handle);
}
shuffle($link); // mix up the array
foreach($link as $l) //loop and print the links
{
echo "<a href=$l>$l</a> <br> "; //<---------DISPLAY <BR> added
}

?> 

Link to comment
Share on other sites

<?php
//file of links
$filename = "links.txt";
$link = array();

$handle = @fopen($filename, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096); // read line by line
        $link[] = $buffer; //store in array // <--Keep as is
    }
    fclose($handle);
}
shuffle($link); // mix up the array
foreach($link as $l) //loop and print the links
{
echo "<a href=$l target=_blank><img src=image.gif width=132 height=20 border=1></a>";
}

?> 

Link to comment
Share on other sites

Hummm

 

this worked for me (images across the top)

<?php
$filename = "links.txt";
$link = array();

$handle = @fopen($filename, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        $link[] = $buffer;
    }
    fclose($handle);
}
shuffle($link);
foreach($link as $l)
{
echo "<a href=$l target=_blank><img src=image.gif width=132 height=20 border=1></a>";
}

?> 

 

So did this (image going to down)

 

<?php
$filename = "links.txt";
$link = array();

$handle = @fopen($filename, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        $link[] = $buffer;
    }
    fclose($handle);
}
shuffle($link);
foreach($link as $l)
{
echo "<a href=$l target=_blank><img src=image.gif width=132 height=20 border=1></a><br />";
}


?> 

 

 

can you try the link file i have attached

 

[attachment deleted by admin]

Link to comment
Share on other sites

OK final code

 

<?php
$filename = "links.txt";
$link = array();

$handle = @fopen($filename, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        $link[] = trim($buffer); //added trim
    }
    fclose($handle);
}
shuffle($link);
foreach($link as $l)
{
echo "<a href=$l target=_blank><img src=image.gif width=132 height=20 border=1></a>";
}

?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.