Jump to content

Recommended Posts

I just want some advice and need to know if I can do the following the script which displays partially like this

 

function getMainLogo() {
global $dir, $site;

    $sFileName = getParam('sys_main_logo');
if(!file_exists($dir['mediaImages'] . $sFileName))
        return '';

return '<a href="' . BX_DOL_URL_ROOT . '"><img src="' . $site['mediaImages'] . $sFileName . '" class="mainLogo" alt="logo" /></a>';
}

function getPromoImagesArray() {
global $dir;

 

 

Can I add the javascript so its like this?

 

function getMainLogo() {
global $dir, $site;
echo '
  < script type="text/javascript > 

banners = new Array();

banners[0] = "www.weareapackage.com,weareapackage.com/images/WePackageBanner.gif,Dating for Single Parents,468,60";
banners[1] = "www.friendsordating.com,www.friendsordating.com/images/bannersketch (2).png,Free Dating for Singles,468,60";
banners[2] = "www.lovelooker.com,parentmeets.com/images/LoveLooker.gif,Adult Dating,468,60";

rand = Math.floor(Math.random() * banners.length);

b = banners[rand].split(",");

url = b[0];
img = b[1];
des = b[2];
wid = b[3];
hei = b[4];

document.write('<a href="' + url + '" target="_blank"><img src="' + img + '" width="'+ wid +'" height="'+ hei +'" border="0" alt="' + des + '" /></a>');

   < /script type="text/javascript > ;'
}
    $sFileName = getParam('sys_main_logo');
if(!file_exists($dir['mediaImages'] . $sFileName))
        return '';

return '<a href="' . BX_DOL_URL_ROOT . '"><img src="' . $site['mediaImages'] . $sFileName . '" class="mainLogo" alt="logo" /></a>';
}

function getPromoImagesArray() {
global $dir;

 

Im not familiar with adding the javascript to the middle of the code, I am not asking for a debugger I am just asking for friendly advice, I am very new to this and dont completely understand, thank you.

Link to comment
https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/
Share on other sites

You need to slash the quotes (and optionally add /n at the end of each line to leave it 'readable' when you view source in your browser) in your javascript, something like this:

echo "
  < script type=\"text/javascript\" >\n 
  \n
  banners = new Array();\n
  \n
  banners[0] = \"www.weareapackage.com,weareapackage.com/images/WePackageBanner.gif,Dating for Single Parents,468,60\";\n
  banners[1] = \"www.friendsordating.com,www.friendsordating.com/images/bannersketch (2).png,Free Dating for Singles,468,60\";\n
  banners[2] = \"www.lovelooker.com,parentmeets.com/images/LoveLooker.gif,Adult Dating,468,60\";\n
  \n
  rand = Math.floor(Math.random() * banners.length);\n
  \n
  b = banners[rand].split(\",\");\n
";

You need to slash the quotes (and optionally add /n at the end of each line to leave it 'readable' when you view source in your browser) in your javascript, something like this:

echo "
  < script type=\"text/javascript\" >\n 
  \n
  banners = new Array();\n
  \n
  banners[0] = \"www.weareapackage.com,weareapackage.com/images/WePackageBanner.gif,Dating for Single Parents,468,60\";\n
  banners[1] = \"www.friendsordating.com,www.friendsordating.com/images/bannersketch (2).png,Free Dating for Singles,468,60\";\n
  banners[2] = \"www.lovelooker.com,parentmeets.com/images/LoveLooker.gif,Adult Dating,468,60\";\n
  \n
  rand = Math.floor(Math.random() * banners.length);\n
  \n
  b = banners[rand].split(\",\");\n
";

 

Or use HEREDOC.

 

echo <<<HEREDOC
<script type="text/javascript">
	banners = new Array();
	banners[0] = "www.weareapackage.com,weareapackage.com/images/WePackageBanner.gif,Dating for Single Parents,468,60";
	banners[1] = "www.friendsordating.com,www.friendsordating.com/images/bannersketch (2).png,Free Dating for Singles,468,60";
	banners[2] = "www.lovelooker.com,parentmeets.com/images/LoveLooker.gif,Adult Dating,468,60";
  
	rand = Math.floor(Math.random() * banners.length);

	b = banners[rand].split(",");
HEREDOC;

Okay so I tried the echo and that loaded the page fine but it echoed the actual script onto the page,

 

I then tried the HEREDOC and that warped the page out completely plus the syntax theme color all went light blue.

As in:

 

echo "
  < script type=\"text/javascript\" >\n 
  \n
  banners = new Array();\n
  \n
  banners[0] = \"www.weareapackage.com,weareapackage.com/images/WePackageBanner.gif,Dating for Single Parents,468,60\";\n
  banners[1] = \"www.friendsordating.com,www.friendsordating.com/images/bannersketch (2).png,Free Dating for Singles,468,60\";\n
  banners[2] = \"www.lovelooker.com,parentmeets.com/images/LoveLooker.gif,Adult Dating,468,60\";\n
  \n
  rand = Math.floor(Math.random() * banners.length);\n
  \n
  b = banners[rand].split(\",\");\n
";
url = b[0];
img = b[1];
des = b[2];
wid = b[3];
hei = b[4];

document.write('<a href="' + url + '" target="_blank"><img src="' + img + '" width="'+ wid +'" height="'+ hei +'" border="0" alt="' + des + '" /></a>');

   < /script type="text/javascript > ;'

???

echo <<<HEREDOC
<script type="text/javascript"> 

banners = new Array();

banners[0] = "www.weareapackage.com,weareapackage.com/images/WePackageBanner.gif,Dating for Single Parents,468,60";
banners[1] = "www.friendsordating.com,www.friendsordating.com/images/bannersketch (2).png,Free Dating for Singles,468,60";
banners[2] = "www.lovelooker.com,parentmeets.com/images/LoveLooker.gif,Adult Dating,468,60";

rand = Math.floor(Math.random() * banners.length);

b = banners[rand].split(",");

url = b[0];
img = b[1];
des = b[2];
wid = b[3];
hei = b[4];

document.write('<a href="' + url + '" target="_blank"><img src="' + img + '" width="'+ wid +'" height="'+ hei +'" border="0" alt="' + des + '" /></a>');

</script>
HEREDOC;

I don't know what things like "not displaying correctly" or "banner is wrong" mean. I'm not looking at your monitor. You need to include detail in your questions.

 

Why are you even doing this in Javascript? From what I can see there is no reason to do so.

$banners = array(
array(
	'url' => 'http://www.weareapackage.com',
	'img' => 'http://www.weareapackage.com/images/WePackageBanner.gif',
	'des' => 'Dating for Single Parents',
	'wid' => 468,
	'hei' => 60
),
array(
	'url' => 'http://www.friendsordating.com',
	'img' => 'http://www.friendsordating.com/images/bannersketch (2).png',
	'des' => 'Free Dating for Singles',
	'wid' => 468,
	'hei' => 60
),
array(
	'url' => 'http://www.lovelooker.com',
	'img' => 'http://parentmeets.com/images/LoveLooker.gif',
	'des' => 'Adult Dating',
	'wid' => 468,
	'hei' => 60
)
);

$banner = $banners[array_rand($banners)];

echo '<a href="' . $banner['url'] . '" target="_blank"><img src="' . $banner['img'] . '" width="' . $banner['wid'] . '" height="' . $banner['hei'] . '" border="0" alt="' . $banner['des'] . '" /></a>';

Some people have Javascript disabled, so your banners would never show.

 

That, and Javascript just doesn't seem to be the best solution here. Javascript should be used for manipulating client-side behavior. This doesn't really fall into that category.

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.