Forum Moderators: coopster

Message Too Old, No Replies

Random array not working.

variable keeps on repeating itself...

         

sauce

8:45 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



I'm trying to pull links from a file display on my page bottoms using a PHP script... The problem is the random variable(link url) keeps repeating...

Here is the script:

include("key.php");

$resource = 1

$resource_links = realpath("../");

$rel = count($resourcelink);

if($rel < $resource){

$resource = $rel;
}

for ($x = 0; $x < $resource; $x++) {
mt_srand((double)microtime()*1000000);
$RandomNumber = rand (1, count($resourcelink)-1);
print("
<p align=\"center\">
<table width=\"100%\" border=\"0\"><tr><td valign=\"top\"><p><a href=\"/". $resourcelink[$RandomNumber]."\">$key1</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key2</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key3</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key4</a></p></td><td width=\"10\">&nbsp;</td><td valign=\"top\"><p><a href=\"/". $resourcelink[$RandomNumber]."\">$key5</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key6</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key7</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key8</a></p></td><td width=\"10\">&nbsp;</td><td valign=\"top\"><p><a href=\"/". $resourcelink[$RandomNumber]."\">$key9</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key10</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key11</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key12</a> (67)</p></td></tr></table>

</p>");
}

}

It's displaying like <a href="resourcelink#1>$key1</a>
<a href="resourcelink#1>$key2</a>... the resourcelink(or URL) is the same for every link when I want it to be random...

I'm sorry of all these stupid questions today... it's just been a hard one...

Netter

10:57 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



man, it's hard to understand what you are doing... but this is how I see it:

I guess key.php has something like $key1='keywords 1'; $key2='keyword 2'; $key3='keyword 3'; etc.

and $resourcelink is an array like $resourcelink[0]='link.html'; $resourcelink[1]='anotherlink.html';... with different links on your website.

then all you need to do is:

// first we check if resourcelink is an array and it contains at least one element
if(is_array($resourcelink)&&count($resourcelink)) {
echo '<table><tr><td>';
shuffle($resourcelink); // shuffles our array
for($i=1;$i<=count($resourcelink);$i++) {
echo '<p><a href="'.$resourcelink[$i].'">'.${'key'.$i}.'</a></p>';
}
echo '</td></tr></table>';
}

this should work, but as for me it's kinda messy with these $keyN variables. what if you have more resourcelink elements then $keyN variables etc.? for that case you should also do check in a loop like
for($i=1;$i<=count($resourcelink)&&isset(${'key'.$i});$i++)...
but it's not good. get rid of $keyN variables and replace it with another array.

the solution above is not ideal because the task is planned the wrong way ;)

ahmedtheking

11:05 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok firstly, what's in key.php? Secondly, it seems like you're trying to make some random links, but I don't understand why you are using random numbers? Explain what your trying to do, include keys.php and add comments to every stage so that we can understand better! Thanks :D

sauce

11:07 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



I left some of that script out... here is basically the whole script with the $key variable's included from the other script...

Included from key.php ...............

$key1 = anchortext1
$key2 = anchortext2
$key3 = anchortext3
$key4 = anchortext4
$key5 = anchortext5
$key6 = anchortext6
$key7 = anchortext7
$key8 = anchortext8
$key9 = anchortext9
$key10 = anchortext10
$key11 = anchortext11
$key12 = anchortext12
........................

//Opens the list of URLS
$filename = "urls.txt";
$handle = fopen($filename, "rb");
$keyword_list = fread($handle, filesize($filename));
fclose($handle);

$resource = 1

$resource_links = realpath("../");

//Grabs all the URLs of a certain directory but not the stats page
if ($handle = opendir($resource_links)) {

while (false!== ($file_res = readdir($handle))) {

if($file_res == "stats"
){

echo "";
}
else{

$resource_name_1 = str_replace("-", " ", $file_res);
$resource_name_2 = str_replace(".php", " ", $resource_name_1);
$resource_name_3 = ucwords($resource_name_2);
$resource_name = rtrim($resource_name_3);
$file_res = str_replace("", "/", $file_res);

$resourcelink[] = $file_res;

}
}

closedir($handle);

$rel = count($resourcelink);

if($rel < $resource){

$resource = $rel;
}

for ($x = 0; $x < $resource; $x++) {
mt_srand((double)microtime()*1000000);
$RandomNumber = rand (1, count($resourcelink)-1);
print("
<p align=\"center\">
<table width=\"100%\" border=\"0\"><tr><td valign=\"top\"><p><a href=\"/". $resourcelink[$RandomNumber]."\">$key1</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key2</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key3</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key4</a></p></td><td width=\"10\">&nbsp;</td><td valign=\"top\"><p><a href=\"/". $resourcelink[$RandomNumber]."\">$key5</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key6</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key7</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key8</a></p></td><td width=\"10\">&nbsp;</td><td valign=\"top\"><p><a href=\"/". $resourcelink[$RandomNumber]."\">$key9</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key10</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key11</a></p>
<p><a href=\"/". $resourcelink[$RandomNumber]."\">$key12</a></p></td></tr></table>

</p>");
}

Make anymore sense what I'm trying to do?

Netter

11:36 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



the script I wrote for you does it all as you wanted it.