jokerfool Posted May 16, 2012 Share Posted May 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/ Share on other sites More sharing options...
scootstah Posted May 16, 2012 Share Posted May 16, 2012 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346094 Share on other sites More sharing options...
jokerfool Posted May 16, 2012 Author Share Posted May 16, 2012 Then why does this produce a T-String error? Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346096 Share on other sites More sharing options...
Jamdog Posted May 16, 2012 Share Posted May 16, 2012 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 "; Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346100 Share on other sites More sharing options...
scootstah Posted May 16, 2012 Share Posted May 16, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346106 Share on other sites More sharing options...
jokerfool Posted May 16, 2012 Author Share Posted May 16, 2012 ok let me play and see what errors or anything good, gimme a sec. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346108 Share on other sites More sharing options...
jokerfool Posted May 16, 2012 Author Share Posted May 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346113 Share on other sites More sharing options...
scootstah Posted May 16, 2012 Share Posted May 16, 2012 Because you have unclosed tags. It was just a snippet, you need to put the rest of the Javascript in there. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346115 Share on other sites More sharing options...
jokerfool Posted May 16, 2012 Author Share Posted May 16, 2012 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 > ;' ??? Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346117 Share on other sites More sharing options...
jokerfool Posted May 16, 2012 Author Share Posted May 16, 2012 Hmmm not that, i get this error: Parse error: syntax error, unexpected '=' in /home/nawacl/public_html/site/inc/design.inc.php on line 225 Refers to this line: b = banners[rand].split(\",\");\n Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346123 Share on other sites More sharing options...
scootstah Posted May 16, 2012 Share Posted May 16, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346145 Share on other sites More sharing options...
jokerfool Posted May 17, 2012 Author Share Posted May 17, 2012 Okay that works, now I have an issue with the banner not displaying correctly and the link to the banner is wrong as well, where am I going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346195 Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346201 Share on other sites More sharing options...
jokerfool Posted May 17, 2012 Author Share Posted May 17, 2012 Because I don't understand any other way to do this hence coming here and getting advice. When the website loads the banner appears in the top left without an image and the url is not correct. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346204 Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 I just gave you another (working) way to do it, with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346207 Share on other sites More sharing options...
jokerfool Posted May 17, 2012 Author Share Posted May 17, 2012 I have implemented your version and thank you. Why shouldn't I do it in javascript, is there a reason? Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346216 Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262630-adding-javascript-to-php-can-i-do-this/#findComment-1346218 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.