Vyx Posted July 3, 2008 Share Posted July 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 3, 2008 Share Posted July 3, 2008 Please post the whole script. Quote Link to comment Share on other sites More sharing options...
Vyx Posted July 3, 2008 Author Share Posted July 3, 2008 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> Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 3, 2008 Share Posted July 3, 2008 <a href "link"><img src=<? include('scripts/randimg.php'); ?>" width...> change to <a href "link"><img src='scripts/randimg.php' height="" width=""></a> Quote Link to comment Share on other sites More sharing options...
trq Posted July 3, 2008 Share Posted July 3, 2008 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> Quote Link to comment Share on other sites More sharing options...
Vyx Posted July 3, 2008 Author Share Posted July 3, 2008 Awesome, thanks for the help! Quote Link to comment Share on other sites More sharing options...
Vyx Posted July 4, 2008 Author Share Posted July 4, 2008 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.