Member Avatar for Who me?

Hi,

I new to PHP. I've searched all over, but with no luck.

My objective is to build an array of variables that contain HTML links. (One html link per variable). Then I want to output the variables, (containing the links), so that a new random variable loads each time the page loads.

I'm having problems with:

[1] formatting a link so that it can be inside a variable.
[2] including a random variable inside of an HTML link so that the variable concatenates with the rest of the HTML link to form the completed URL.

My code look like this - at the moment :-)

<?php 
	$var1 = "<a href="http://link1.com">Link 1</a>";
	$var2 = "<a href="http://link2.com">Link 2</a>"";
	$var3 = "<a href="http://link3.com">Link 3</a>"";
	echo ""; $var1; $var2; $var3;
?>			
<?php
	$the_array = array( "$var1", "$var2", "$var3", "$var3" ); //<--Note the extra "item3"
	shuffle($the_array); // Shuffles the array
	echo $the_array[0]; // Becomes the new shuffled array			
	$random_number = rand(0,($array_count-1)); // Outputs a random variable from the shuffled array.		
	$random = "$the_array[0]";
?>

I am trying to output the above as follows:

<ul>
<li><?php echo '<a href="http://Link1.com">Link 1</a>.'; ?></li>        
<li><?php echo '<a href="http://Link2.com">Link 2</a>.'; ?></li> 
<li><?php echo '<a href="http://Link3.com">Link 3</a>.'; ?></li> 
</ul>

I've also tried to concatenate the output something like this:

<ul>
<li><?php echo '<a href="http://.$varA.Link1.com">Link 1</a>.'; ?></li>        
<li><?php echo '<a href="http://.$varB.Link2.com">Link 2</a>.'; ?></li> 
<li><?php echo '<a href="http://.$varC.Link3.com">Link 3</a>.'; ?></li> 
</ul>

The idea of the concatenation is to get around having to store the whole 'http://...etc' in the original variables. None of the above worked for me.

I appreciate the help.

Recommended Answers

All 4 Replies

Member Avatar for diafol

this looks like a dog's dinner. I can't see exactly what you're trying to do here. Anyway:

$links = array(
 array("url"=>"...first.php","label"=>"first"),
 array("url"=>"...second.php","label"=>"second"),
 array("url"=>"...third.php","label"=>"third"),
 array("url"=>"...fourth.php","label"=>"fourth"),
);
shuffle($links);
echo "<ul>";
foreach($links as $link){
	echo "<li><a href=\"http://{$link['url']}\">{$link['label']}</a></li>";	
}
echo "</ul>";

like that? the array can be made to be non-associative if you want:

array("...first.php","first"),

and

echo "<li><a href=\"http://{$link[0]}\">{$link[1]}</a></li>";

where '...' is the path, e.g. 'www.example.com/path/'

Member Avatar for Who me?

This is a great help, but I see one problem - (for me that is).

The above code outputs a list of bulleted links and rotates all of the links on each page load. (I will definitely use this later.)

For now, I need one link to display with each page load, and not the entire list. Each page load must present a new link as taken from the variables at random.

Thank you.

<?
	$links = array(
	 array("url"=>"...first.php","label"=>"first"),
	 array("url"=>"...second.php","label"=>"second"),
	 array("url"=>"...third.php","label"=>"third"),
	 array("url"=>"...fourth.php","label"=>"fourth")
	);
	$link = $links[array_rand($links)];	
	echo "<ul>";
	echo "<li><a href=\"{$link['url']}\">{$link['label']}</a></li>";	
	echo "</ul>";
?>
Member Avatar for Who me?

Thank you,

This works exactly the way I needed.

Really appreciate it. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.