Current location: Hot Scripts Forums » Programming Languages » PHP » random link problem


random link problem

Reply
  #1 (permalink)  
Old 06-10-03, 01:52 PM
doogi doogi is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
random link problem

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-10-03, 03:22 PM
phpkid phpkid is offline
Hot Moderator ;)
 
Join Date: Jun 2003
Location: India
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
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!

PHP Code:

<?

    
    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
__________________
http://www.phpkid.org
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-10-03, 03:26 PM
Cagez's Avatar
Cagez Cagez is offline
Eeew, dirty!
 
Join Date: Jun 2003
Location: Toronto, Ontario
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-10-03, 05:35 PM
doogi doogi is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your support guys
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-10-03, 05:47 PM
doogi doogi is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-10-03, 08:54 PM
Cagez's Avatar
Cagez Cagez is offline
Eeew, dirty!
 
Join Date: Jun 2003
Location: Toronto, Ontario
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-11-03, 12:55 AM
The Wolf's Avatar
The Wolf The Wolf is offline
CMS Developer
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
I would have a file called 'links.txt' that contains:
Code:
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 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-11-03, 08:04 AM
Cagez's Avatar
Cagez Cagez is offline
Eeew, dirty!
 
Join Date: Jun 2003
Location: Toronto, Ontario
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
No, that would get each image out of the file, he only wants 5, and to get them randomly.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 06-11-03, 03:57 PM
doogi doogi is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 06-11-03, 04:03 PM
Cagez's Avatar
Cagez Cagez is offline
Eeew, dirty!
 
Join Date: Jun 2003
Location: Toronto, Ontario
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Different type of link counter mdhall Script Requests 2 10-10-03 02:19 PM
Make a query that makes a field a link to the entire row mdhall PHP 8 10-09-03 07:18 PM
file download problem ukyankee Perl 6 10-04-03 11:39 PM
session problem afkaar PHP 0 08-27-03 05:43 AM


All times are GMT -5. The time now is 09:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.