Jump to content

How to insert php script as link


Vyx

Recommended Posts

Sorry for this silly question but I've just started using php for the 1st time. (today)

 

I have a random image script that a friend put together to help me learn php.  If I just insert it by itself it comes up fine.  The problem is when trying to make the image that the script shows a link.  ie. a link back to the homepage as this is a header image.

 

What I have now is just the simple html tags: <a href "link"><img src=<? include('scripts/randimg.php'); ?>" width...></a>

 

This however doesn't work :(  Can someone tell me what I'm doing wrong?

 

Thanks,

Vyx

Link to comment
Share on other sites

Here is the php script (sorry, lots of comments for learning purposes):

<?

//Directory the images are stored relative to root.
$imgdir = '/home/aenigma/public_html/vyx/media/images/header';


//Path to images folder
$dirpath = '/media/images/header/';


//Create an array with the list of files as each value.
//Keep in mind, the first two values listed for a directory 
//will always be: "." and ".." UNLESS you change the 
//sort order.
$dirlist = scandir($imgdir);


//Count the number of items in the array "dirlist"
//Basically, count how many files are in the directory.
//We subtract 1 from this because it counts the "0" value 
//in teh array as 1.
$dircount = count($dirlist) - 1;


//Get a random number between 2 and the size of the array.
$randnum = mt_rand(2,$dircount);


//When you create an array, every entry into the array 
//is assigned a value 0-128. When we create a random number 
//between 2 and the maximum number of values in the array, 
//we're essentially picking which value to call from teh array.
//This next part uses our random value as the entry to call from
//the array.
$imgname = $dirpath . $dirlist[$randnum];

//Now we just tell php to show the image.
echo "<img src='$imgname'></img>";

?>

 

Here is the HTML:

<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr> 
	<td><a href="http://link.com"><img src="<? include('scripts/randimg.php'); ?>" width="600" height="150" border="0"></a></td>
    </tr>

Link to comment
Share on other sites

Just starting with php? Heres a tip, use <?php tags not <?

 

Anyway, notice how your script actually echos <img src='$imgname'></img>, hence there is no need to write it again.

 

<td><a href="http://link.com"><?php include('scripts/randimg.php'); ?> width="600" height="150" border="0"></a></td>

Link to comment
Share on other sites

Sorry to bring this back up but my problem isn't actually solved :*(

 

I used

<td><a href="http://link.com"><?php include('scripts/randimg.php'); ?></a></td>

 

This works beautifully save the fact that I can't insert height, width, and specifically border tags.

 

I tried inserting them directly into the script file but it says

Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in /home/aenigma/public_html/vyx/scripts/randimg.php on line 63

 

I also tried using the suggestion posted by grimmier and thorpe but couldn't get either of these to work.

 

Any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.