Hello Everyone,

I've been working on a small pet project of mine.  It involves link
management for my site.  I want to be able to tell the program how many
links to display from each of my link files.  The program is then to go
to that individual link file, randomly pick out how ever many links I've
told it to get and then display a description of each link.  The
description is to be hyperlinked to the actual URL of that link.

I have a directory that contains 5 text files.  Those 5 files each
contain 10 links.  Each line in a file has two elements.  The first
being a url, the second being a description.  The two elements are
delimited by a comma.  Here is what they would look like:
http://www.File1.com/Link1,File 1 Link 1
http://www.File1.com/Link2,File 1 Link 2
http://www.File1.com/Link3,File 1 Link 3

My php code is actually two programs.  The first program asks for my
input.  Basically it's a list of the file names with a text box next to
each.  In that text box I can choose how many links to display from each
file.  So let's say I choose to display 2 links from the first file, 0
links form the second file, 3 links from the third file, 0 links from
the 4th file and 5 links from the fifth file.  The program then writes
the following to a simple text file:
2
0
3
0
5

The second program does all of the fun stuff.  The first step being to
open that text file and read the values:
//open result file and read admin count into array
$filepointer = fopen ($filename, "r");
$array_admin_count = file ($filename);
fclose ($filepointer);

The next step then opens the directory which contains the 5 files of
links and reads each file name:
//open directory and read file names
$open = opendir ($dir);
while ($files = readdir ($open))
  {
 if ($files != "." && $files != "..")
 {
  $filesnames = $dir.$files;
 $array_of_files[] = $filesnames;
 }
  }
closedir ($open);

The final part is pretty beefy.  This is where my problem is.  This part
is to open the individual link files, generate the proper amount of
random links and then display the description which is hyperlinked to
the url.  It works if you only choose to display either 0 links from a
file or 1 link from a file.  If you choose anything over 1 random link,
it displays the proper information, but it's mashed together.  Here is
the code for the final section of part two:
//open files and grab random links
for ($counter = 0; $counter < count($array_admin_count); $counter++)
 {
 $array_admin_count[$counter] = rtrim($array_admin_count[$counter]);

 if ($array_admin_count[$counter] > 0)
 {
 //Initialize:
  $res_string = '';
  $res = array();
 //expose url's in file
 $fileopen = fopen ($array_of_files[$counter], "r");
 $array_of_links = file ($array_of_files[$counter]);
 fclose ($fileopen);
 //choose $number random lines from file
  $keys = array_rand($array_of_links,$array_admin_count[$counter]);
  //store random lines into $res array
    if(is_array($keys))
    {
    foreach($keys as $key)
    { $res[] = $array_of_links[$key]; }
    }else{
   $res[] = $array_of_links[$keys];
   }
 //if randomize is on, randomize result array
    if($randomize_results==1)
    {
  shuffle($res);
  }
  //convert result array to string
    $res_string = implode("",$res);
  $getlink = explode(",", $res_string);
    print ("<a href='$getlink[0]'>$getlink[1]</a><br>");


Here's what I can figure out:
Choosing 0 random links works for obvious reasons.  Choosing to display
1 random link works because the array $res will look like this:
http://www.File1.com/Link1,File 1 Link 1
The program then creates $res_string.  Then the program explodes
$res_string based on the comma delimiter into the array $getlink.  Then
the program displays the two elements of $getlink in the proper manner.

If you choose to display more then 1 random link the program gets
buggy.  Let's say you choose to display 2 random links.  When the
program gets down to the $res array, the value of $res is:
http://www.File1.com/Link1,File 1 Link 1 http://www.File1.com/Link2,File
1 Link 2

Once this happens, the next few lines of code $res_string (by implode)
and the $getlink (by explode) creates havoc.  I've been up all night
trying to figure out a solution.  I can't seem to arrive at one.  Might
any of you have any idea what I can do to resolve this array issue?

Sorry this email is so long and my description is probably confusing.

Thanks in advance,
Roger

PS
if you wish to see the problem first hand, go here, and choose 1 link
from each file.  Then go back to it and choose more then 1 and see what
happens:
 http://www.g-i-w-s.com/php/test31.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to