Jump to content

Need some random help


Gruzin

Recommended Posts

One way is to hold all the links in an array
[code]<?php
$LinkArr[1] = "http://www.google.com";
$LinkArr[2] = "http://www.yahoo.com";
$LinkArr[3] = "http://www.msn.com";
$LinkArr[4] = "http://www.phpfreakes.com";
?>[/code]

And then use rand() to grab one of those randomly
[code]<?php
$LinkToShow = rand(1, count($LinkArr));
?>[/code]

and then show the randomly chosen link
[code]<?php
echo $LinkArr[$LinkToShow];
?>[/code]
Link to comment
Share on other sites

[code]<?php
/* The array to hold the links */
$LinkArr[1] = "http://www.google.com";
$LinkArr[2] = "http://www.yahoo.com";
$LinkArr[3] = "http://www.msn.com";
$LinkArr[4] = "http://www.phpfreakes.com";

/* The array to hold the descritions for the links */
$LinkArrDesc[1] = "This is a descrition for google!";
$LinkArrDesc[2] = "This is a descrition for Yahoo!";
$LinkArrDesc[3] = "This is a descrition for MSN!";
$LinkArrDesc[4] = "This is a descrition for PHPFreaks!";

/* Grab a link randomly */
$LinkToShow = rand(1, count($LinkArr));

/* Now show the link as a clickable link and the descritpion under it */
echo "<a href='". $LinkArr[$LinkToShow] ."'>". $LinkArr[$LinkToShow] ."</a><br>". $LinkArrDesc[$LinkToShow];

?>[/code]
Link to comment
Share on other sites

Yep. This code will show the first 20 randomly, or all of them in random order if the ammount of links is under or equal to 20.
[code]<?php
if(count($LinkArr) > 20)
$Max = 20;
else
$Max = count($LinkArr);

for($i=1; $i <= $Max; $i++){
$LinkToShow = rand(1, count($LinkArr));
echo "<a href='". $LinkArr[$LinkToShow] ."'>". $LinkArr[$LinkToShow] ."</a><br>•". $LinkArrDesc[$LinkToShow] ."<br><br>";
}
?>[/code]
Link to comment
Share on other sites

Please also add the code that let's users get the random link from a database ok cheers mate.

example


explode("",$links);

foreach($links AS $LinkArr);

/* The array to hold the links */
$LinkArr[1] = "http://www.google.com";
$LinkArr[2] = "http://www.yahoo.com";
$LinkArr[3] = "http://www.msn.com";
$LinkArr[4] = "http://www.phpfreakes.com";


somthink like that if the links are in the database

This code needs to be corrected theo cheers.
Link to comment
Share on other sites

Thanks guys I've done it with your help, but I've got a little problem:
http://www.3d.caucasus.net
here is my draft site, on the left u can see this linkbox (random links), so there is a problem for me on the first line of this link box, link is shown 2 times and not seperated.... what can I do to make this first line look like below ones?
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.