• 0

[PHP] Adding dynamic Image Alt Text


Question

The following code has been passed around a bit lately however, I was wondering if it was possible to add Dynamic Image Alt Text as well onto this. Currently All image generated have the Alt text of "Sponsor"

<!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=utf-8" />
<title>Sponsor Rotator</title>
<style type="text/css">
	body, img, a, a:active, a:hover, a:visited {
    outline-width: 0px;
	border-width: 0px;
	display: block;
	margin: 0px auto;
	padding: 0px;
	background-color: #eff0f0;
	}
</style>
</head>

<body>
<?php
$advertisers = array(
        array(
        'weighting' => 1,
        'image'         => 'Image Location',
        'url'   => 'URL Location'
        ),
        array(
        'weighting' => 1,
        'image'         => 'Image Location',
        'url'   => 'URL Location'
        ),
        array(
        'weighting' => 1,
        'image'         => 'Image Location',
        'url'   => 'URL Location'
        ),
        array(
        'weighting' => 1,
        'image'         => 'Image Location',
        'url'   => 'URL Location'
        ),
);

$weighted_advertisers = array();

foreach($advertisers as $advertiser){
        for($i = 0; $i <= $advertiser['weighting']; $i++){
        array_push($weighted_advertisers, $advertiser);
        }
}

$random_weighted_advertiser = $weighted_advertisers[array_rand($weighted_advertisers)];

printf('<a href="#" onclick="parent.location = \'%s\'; return false;"><img src="%s" alt="Sponsor" /></a>',$random_weighted_advertiser['url'],$random_weighted_advertiser['image']);

?>
</body>
</html>

I assume all i would have to do is in each array set do the following

        
        array(
        'weighting' => 1,
        'image'         => 'Image Location',
        'url'   => 'URL Location',
        'alt' =>'Sponsor Name'
        ),

Then once at the bottom do the following,

printf('<a href="#" onclick="parent.location = \'%s\'; return false;"><img src="%s" alt="%s" /></a>',$random_weighted_advertiser['url'],$random_weighted_advertiser['image'],$random_weighted_advertiser['alt']);

However, I have not tested this yet, Would this work?

Also I would like the Link to open in a new window how would I do that with this code?

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0
However, I have not tested this yet, Would this work?

:blink:

You compile this thread instead of testing the code?

Link to comment
Share on other sites

  • 0

I think it'd work... however as the previous poster said, it would've been quicker to test than to post haha

Link to comment
Share on other sites

  • 0

The reason I asked rather than tested is due to me not currently having access to test the code, and being very curious if the code will work.

Just tested it however, it works in IE however, not Chrome, or Firefox. I think that is fine honestly. Just need to get it working with opening in a new window.

Link to comment
Share on other sites

  • 0

Whats the connection between a server side code working/not working and type of browser ? None really. So your post doesn't make much sense.

the connection between the two is how the browser renders the output

Also the Alt information seams to only work randomly between all browsers i have tested.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.