Jump to content

Archived

This topic is now archived and is closed to further replies.

Gruzin

Need some random help

Recommended Posts

hi guys, I hope u can help me:
I've got some links in external php file and I want the sript to pick up this links in random way. please, help. Thanks

Share this post


Link to post
Share on other sites
can you explain a bit more mate? please....

when it picks them up what is it doing withit?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I've done it, but I need to display all the links and I want this links to be linked???

Share this post


Link to post
Share on other sites
so basically u want all the links in a random way that link to another website when u click on them?

Share this post


Link to post
Share on other sites
yes, I want this links to be linked to the web-sites. I'am trying to make a descriptions for the links.... I mean when the link will be displayed I want some description under it

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Thanks a lot you realy helped me:) can I make all this links to be displayed? Thanks

Share this post


Link to post
Share on other sites
To show all links use this:

[code]<?php
for($i=1; $i <= count($LinkArr); $i++){
echo "<a href='". $LinkArr[$i] ."'>". $LinkArr[$i] ."</a><br>•". $LinkArrDesc[$i] ."<br><br>";
}
?>[/code]

Share this post


Link to post
Share on other sites
Thank you very much!!! You are so Kind:)

Share this post


Link to post
Share on other sites
And one more thing please:) Can we display only first 20 links in random way?

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

×

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.