Jump to content

Multiple Random title & link


syed544

Recommended Posts

I have a text file, named as "random.txt" with the content as like this:

 

  Quote
<a href=link1.html>title-1</a>

<a href=link2.html>title-2</a>

<a href=link3.html>title-3</a>

<a href=link4.html>title-4</a>

 

Now in my php file, I want to randomly select & display two titles (with respective link) as like this:

<?
$textfile ="random.txt";
$items = file("$textfile");
shuffle ($items);

echo "RandLink1 : " . $items[0];
echo "<br>RandLink2 : " . $items[1];
?>

 

***

The result is showing as required by me. But I want to load the text file (random.txt) as like this only :

  Quote
link1 , title-1

link2 , title-2

link3 , title-3

link4 , title-4

 

If I load this text file as like above (writing only the link and title names in the file), then what will be the php code to display two random titles on the result page?

 

Thanks for your kind help.

 

Link to comment
https://forums.phpfreaks.com/topic/247119-multiple-random-title-link/
Share on other sites

<?php

$links[] = "<a href=link1.html>title-1</a>";

$links[] = "<a href=link2.html>title-2</a>";

$links[] = "<a href=link3.html>title-3</a>";

$links[] = "<a href=link4.html>title-4</a>";

  //$dbarray = mysql_fetch_array($result);

/// //////////////////////////////////////////echo 'number of elements '.count($links).'<br>';

  //////print_r($links);

  $rand_titles=array_rand($links,2);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title><?php echo $rand_titles[0] ;?> <?php echo $rand_titles[1] ;?></title>

</head>

 

<body>

</body>

</html>

 

 

Please mark as Solved if so.

 

Regards

Hi mjahkoh, Thanks for replying.

but unfortunately I dont need this solution which u have suggested. I think there's some misinterpretation.

I request you to please read again my query.

 

I want to use a text file "random.txt" for providing article title names (not website names) with their respective links.

and instead of providing title+link in this format:

  Quote
<a href=link1.html>title-1</a>

I want to add the content of random.txt file as like below:

  Quote
link1 , title-1

link2 , title-2

link3 , title-3

link4 , title-4

 

then, what will be the php code to display two random texts (article title with its link) in the body of the html file?

Hopefully this helps.

 

<?php 

// This is just like using $lines = file('random.txt');
$lines = array(
'link1 , title-1',
'link2 , title-2',
'link3 , title-3',
'link4 , title-4'
);

// Shuffle the array
shuffle($lines);

// Grab the first two array entries
$pieces = array_slice($lines,0,2);
// Loop through those pieces
foreach( $pieces as $piece ) {
// $piece now holds 'linkx , title-x'
// explode() turns the string into an array, divided by ' , '
// list grabs array[0] and array[1] and puts them into the variables
list( $link, $title ) = explode( ' , ', $piece );
echo '(a href="'.$link.'")'.$title.'(/a)<br>';
}

?>

Archived

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

×
×
  • Create New...

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.