PDA

View Full Version : random link problem


doogi
06-10-03, 01:52 PM
Hi there:)
it's my first thread here so first i'd like to say hello to everyone:)

i have pictures with links to other sites on my page. After each reloading script shows 5 random pics with links.
I have 3 tables with 5 pics on my so 15 pics is used at one time. i have a 45 pics in a txt file which can be teken by script.
My problem is that i have 2 or even 3 same pics after each reloading. what can i do to have no doubled pics?
the scripts looks like below:
<?
$ile = 5;
$file = file("pics.txt");
echo "<table width=\"600\"><tr>";
for($i=0;$i<$ile;$i++)
{
srand((double)microtime()*1000000);
$link = $file[array_rand($file)];
$sp = explode("||", $link);
if($sp[0]<>"")
{
echo "<td align=middle><a
href=\"".$sp[2]."\"><img src=\"".$sp[0]."\" border=3></a><br><font
style=\"font-weight: bold; font-size: 11px; font-family: Tahoma\">".$sp[1]."<br></font></td>";
}
}
echo "</tr></table>";
?>

Help me pls:)
doogi

phpkid
06-10-03, 03:22 PM
Hm...
First thing.

If you have to put PHP code, put it between [ PHP ] and [/ PHP ] (without spaces) so it will be color coded! see my code below!


<?

srand((double)microtime()*1000000);
$ile = 5;
$file_names = file("pics.txt");
echo "<table width=\"600\"><tr>";

$picked_file = array_rand($file_names,5); //Ask it to pick 5 random entries!
foreach($picked_file as $file)
{
$link = $file_names[$file];
$sp = explode("||", $link);
if($sp[0]<>"")
{
echo "<td align=middle><a href=\"".$sp[2]."\"><img src=\"".$sp[0]."\" border=3></a><br>
<font style=\"font-weight: bold; font-size: 11px; font-family: Tahoma\">".$sp[1]."<br></font></td>";
}
}
echo "</tr></table>";

?>



See if the code solves your problem! :)

Regards,
JD

Cagez
06-10-03, 03:26 PM
<?php

$ile = 5;
// I used this line to test it
// $file = array(1,2,3,4,5,6,7,8,9,0);
$file = file('pics.txt');

echo '<table width="600"><tr>';
echo "\n";

srand((double)microtime()*1000000);
$link = array_rand($file, $ile);

for($i=0; $i<$ile; $i++)
{
$sp = explode('||', $link[$i]);
if($sp[0] <> '')
{
echo '<td align=middle>';
echo "\n";

echo '<a href="'.$sp[2].'"><img src="'.$sp[0].'" border=3></a><br>';
echo "\n";

echo '<font style="font-weight: bold; font-size: 11px; font-family: Tahoma">'.$sp[1].'<br></font>';
echo "\n";

echo '</td>';
echo "\n";
}
}

echo '</tr></table>';
echo "\n";

?>

I think that should work. :)

Edit: Ah, phpkid already replied :D

doogi
06-10-03, 05:35 PM
Thanks for your support guys:)

doogi
06-10-03, 05:47 PM
Thanks for your support guys:)

phpkid - it works fine but only in one table /in one table i don't have doubled images with links anymore/ but still i have same pictures in second or third table from time to time /i mean that pics for example 1.jpg is never doubled in one table but can be again in others tables in the same time.

Cagez - i filled my page with your script but it looks that script takes nothing from txt file, <a href=""> - no link taken and <img src="11" border=3> - number taken but extension .jpg not.
sample below:
<table width="600"><tr>
<td align=middle>
<a href=""><img src="11" border=3></a><br>
<font style="font-weight: bold; font-size: 11px; font-family: Tahoma"><br></font>
</td>
<td align=middle>
<a href=""><img src="15" border=3></a><br>
<font style="font-weight: bold; font-size: 11px; font-family: Tahoma"><br></font>
</td>
<td align=middle>
<a href=""><img src="20" border=3></a><br>
<font style="font-weight: bold; font-size: 11px; font-family: Tahoma"><br></font>
</td>
<td align=middle>
<a href=""><img src="46" border=3></a><br>
<font style="font-weight: bold; font-size: 11px; font-family: Tahoma"><br></font>
</td>
<td align=middle>
<a href=""><img src="48" border=3></a><br>
<font style="font-weight: bold; font-size: 11px; font-family: Tahoma"><br></font>
</td>
</tr></table>

what to do now?:)
i hope that you understood what i mean, i'm asking beacuse sometimes i don't understand myself:)

regards
doogi

Cagez
06-10-03, 08:54 PM
<?php

$ile = 3;
// I used this line to test it
// $file = array(1,2,3,4,5,6,7,8,9,0);
$file = file("pics.txt");

echo '<table width="600"><tr>';
echo "\n";

srand((double)microtime()*1000000);
$keys = array_rand($file, $ile);

for($i=0; $i<$ile; $i++)
{
$sp = explode('||', $file[$keys[$i]]);
if($sp[0] != '')
{
echo '<td align=middle>';
echo "\n";

echo '<a href="'.$sp[2].'"><img src="'.$sp[0].'" border=3></a><br>';
echo "\n";

echo '<font style="font-weight: bold; font-size: 11px; font-family: Tahoma">'.$sp[1].'<br></font>';
echo "\n";

echo '</td>';
echo "\n";
}
}

echo '</tr></table>';
echo "\n";

?>
Doh! I was using array_rand wrong ;) This should work now.

The Wolf
06-11-03, 12:55 AM
I would have a file called 'links.txt' that contains:
http://www.yoursite.com|/path/to/image.jpg|This is the alt info
http://www.yoursite.net|/path/to/image.jpg|This is the alt info

and a php file 'links.php' that contains this code:
<?php

$file = file_get_contents("./links.txt");
$line = split("\n", $file);

foreach ($line as $l) {
$text = split("\|", $l);
print "<a href=\"$text[0]\"><img alt=\"$text[2]\" src=\"$text[1]\" border=\"0\"></a>";
}

?>

Thats all you need, apart from a layout to include 'links.php' into.

Cagez
06-11-03, 08:04 AM
No, that would get each image out of the file, he only wants 5, and to get them randomly.

doogi
06-11-03, 03:57 PM
same again:(
it shows pictures now but doubled again.
i'm not sure if i explained well what i mean, so again:

5 random pictures from pics.txt file

something else , other links and so on


5 random pictures from pics.txt file

something else , other links and so on

5 random pictures from pics.txt file

something else , other links and so on

that's like my page looks like and i want no picture doubled on a page

after every script i tried i always have some pictures doubled, not in one line but in one page
so i wonder if it's possible or only my wishes

doogi

Cagez
06-11-03, 04:03 PM
So, for example, accross 3 pages you want random images on all of them without duplicated on any?

Hm, thats a little more difficult... You have to track which ones the user has seen and which s/he hasnt.

doogi
06-11-03, 04:12 PM
hmm everything what is more complicated then html is a difficult for me:)
if my idea isn't possible in php my second idea is to make pics1.txt, pics2.txt and pics3.txt and divide all pictures
but that way pictures from pics3.txt will never exist in a line one:(

btw i hava also exact same problem with llinks with descriptions which are among pictures


doogi

ChristGuy
06-11-03, 06:07 PM
Greetingz...

I've gotten the following code to work...

Basically put it into showads.php and include('showads.php') where you need the pics to show.


<?php

// Check if already assigned
if (!isset($DoneImages))
{
$DoneImages = array();
}

$ile = 5;
$file = file("pics.txt");

echo '<table width="600"><tr>';
echo "\n";

srand((double)microtime()*1000000);

for($i=0; $i<$ile; $i++)
{
// Pic Random Numbers that arn't in $DoneImages
do
{
$Pic = Rand(0, Count($file) - 1); // Had to add -1 so that there would always be $ile per row...
}while (in_array($Pic, $DoneImages));

// Add currently selected random pic to $DoneImage
$DoneImages[] = $Pic;

$sp = explode('||', $file[$Pic]);
if($sp[0] != '')
{
echo '<td align=middle>';
echo "\n";

echo '<a href="'.$sp[2].'"><img src="'.$sp[0].'" border=3></a><br>';
echo "\n";

echo '<font style="font-weight: bold; font-size: 11px; font-family: Tahoma">'.$sp[1].'<br></font>';
echo "\n";

echo '</td>';
echo "\n";
}
}

echo '</tr></table>';
echo "\n";
?>


Hope this help....

The Wolf
06-12-03, 05:22 AM
I have some nicer code...

It randomly loads from the file, but only takes one entry once.

This is the file it reads from:
http://www.corephp.org|/links/corephp_01.png|Core PHP.org
http://www.yoursite.net|/links/corephp_02.png|Core PHP.org
http://www.yoursite.com|/links/corephp_03.png|Core PHP.org
http://www.yoursite.net|/links/corephp_04.png|Core PHP.org
http://www.yoursite.com|/links/corephp_05.png|Core PHP.org
http://www.yoursite.net|/links/corephp_06.png|Core PHP.org

and this is the code:
<?php

# Read the text file and split it into lines:
$file = file_get_contents("./links.txt");
$line = split("\n", $file);

# Init some vars:
$keys = array();

# Make a random list of numbers with
# no duplicates and display the info
# from the text file:
while (count($keys) <= 1) {
$rand = rand(0, count($line) - 1);
if (!in_array($rand, $keys)) {
# Add the key to the $key var:
$keys[] = $rand;

# Split the text, then display it:
$text = split("\|", $line[$rand]);
print "<a href=\"$text[0]\"><img alt=\"$text[2]\" src=\"$text[1]\" border=\"0\"></a><br />";
}
}

?>

Good luck, hope mine works for you.

doogi
06-12-03, 05:55 PM
thx guys for your big help:)

The Wolf-somehow it doesn;t work for me. It can be a problem with me because almost everyhing what i can do with php is copy paste:) but thx again


ChristGuy - thank you very much it's probably i've been looking for
, anyway it works , nothing doubled .
could you do same think with my links.txt?

<?
//start of the script
$ile = 25;
$file = file("links.txt");
for($i=0;$i<$ile;$i++)
{
srand((double)microtime()*1000000);
$link = $file[array_rand($file)];
$sp = explode("||", $link);
if($sp[0]<>"")
{
echo "".$sp[0]." <a href=\"".$sp[1]."\">".$sp[2]."</a><br>";
}
}
//end of the script
?>


btw.
i use notpad to write txt file but too much infos fot it.
can i use wordpad to create txt.
does lenght of txt file wit all links make a visible difference in loading of the page?

thx
doogi

The Wolf
06-13-03, 02:59 AM
Damn, ok. at least you got it working, the length of the txt file would make a small diff, but only very small...

ChristGuy
06-13-03, 06:10 PM
Greetinz...

try the following..

<?
if (!isset($DoneImages))
{
$DoneImages = array();
}

//start of the script
$ile = 25;
$file = file("links.txt");

for($i=0;$i<$ile;$i++)
{
srand((double)microtime()*1000000);

do
{
$Pic = rand(0, Count($file) - 1);
} while (in_array($Pic, $DoneImages));

$DoneImage[] = $Pics;

$sp = explode("||", $file[$Pic]);

if($sp[0]<>"")
{
echo "".$sp[0]." <a href=\"".$sp[1]."\">".$sp[2]."</a><br>";
}
}
//end of the script
?>

doogi
06-15-03, 04:29 AM
it works:)
thank you very much again:)

best regards
doogi

ChristGuy
06-15-03, 07:43 AM
Glad it worked....

About editing text files... you can use Wordpad... Just save as text..

BTW.. I use EditPad Lite http://www.editpadlite.com/

Kinda like notepad on steroids....