Page 1 of 1

Inserting JavaScript for image rollover into a PHP header

Posted: Wed Jul 18, 2007 7:19 pm
by assassinave
In my not so infinite knowledge i know I can use Dreamweaver to create image rollovers. They work great when the page is HTML, but it doesn't translate easily for me to PHP.

How can i add in the Javascript and the <body> tag that is needed for action into my header.php?

I really should just spend the time learning this, but steady as she goes.

Thanks.

Posted: Wed Jul 18, 2007 8:39 pm
by feyd
Typically, you just add it. There shouldn't be anything special.. but it's hard to say without seeing what you have in header.php.

Posted: Wed Jul 18, 2007 9:46 pm
by Phoenixheart
Nah... I don't think header.php should be called a PHP header :D
About image rollovers, they are browser scripts only, therefore normal DHTML and/or Javascript should handle them fine. If you want to use php (for dynamic image names for example), then just use printf() or echo().
Also, if you have time, take a look at Smarty :D

Follow up code

Posted: Wed Jul 18, 2007 11:31 pm
by assassinave
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Contents:

1. Php Code
2. JavaScript and <body> tag
3. HTML that take commands from JS

Sorry if this is too much. If there is a better way to implement image rollovers in a PHP file that can be easily changed by a PHP novice like myself I'm willing to use those as well. Maybe i am just missing an echo tag or something. It may or may not be helpful to know that this is part of a CRE Loaded template. I know i'll throw myself off the cliff for using it.





[b]1. Here is the header.php[/b]

Code: Select all

<?php
/*
  $Id: header.php,v 1.1.1.1 2004/03/04 23:42:24 ccwjr Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
define('DIR_WS_TEMPLATE_IMAGES', 'templates/Original_B2B/images/');

// WebMakers.com Added: Down for Maintenance
// Hide header if not to show
if (DOWN_FOR_MAINTENANCE_HEADER_OFF =='false') {

if (SITE_WIDTH!='100%') {
?>




<div id="header">
	<div id="home_button"><img src="templates/Crazy/images/home_button.gif" border="0" usemap="#Map" />
			<map name="Map" id="Map"><area shape="rect" coords="7,5,143,78" href="<?php echo tep_href_link(FILENAME_DEFAULT); ?>" /></map>
	</div>
	
	<div id="cart_text"><a href="login.php">Log In</a>&nbsp;&nbsp;&#8224&nbsp;&nbsp;<a href="shopping_cart.php">View Cart</a>&nbsp;&nbsp;&#8224&nbsp;&nbsp;<a href="checkout_shipping.php">Checkout</a></div>
</div>
<?php
}
?>

<div id="nav">
	<div id="nav_button"><a href="template_int.html"><img src="templates/Crazy/images/product.jpg" border="0"></a></div>
	<div id="nav_button"><a href="http://crazy.com/dev/b2b/templates/Crazy/wholesale.html"><img src="templates/Crazy/images/wholesale.jpg" border="0"></a></div>
	<div id="nav_button"><a href="http://crazy.com/dev/b2b/templates/Crazyabout.html"><img src="templates/Crazy/images/about.jpg" border="0"></a></div>
	<div id="nav_button"><img src="templates/Crazy/images/fundraise.jpg"></div>
</div>



<div id="breadcrumbs">
	<div><?php echo $breadcrumb->trail(' &raquo; ');?></div>
</div>

<?php
}
?>
<!-- header_eof //-->

2. JS Script that make the buttons work

Code: Select all

<script type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>


<body onload="MM_preloadImages('images/product_over.jpg','images/wholesale_over.jpg','images/about_over.jpg')">

3. Here is the code i need to put into the document for the rollover images

Code: Select all

<div id="nav">
			<div id="nav_button"><a href="http://Crazy.com/dev/b2b/index.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('product','','images/product_over.jpg',1)"><img src="images/product.jpg" name="product" width="69" height="18" border="0" id="product" /></a></div>
			<div id="nav_button"><a href="http://Crazy.com/dev/b2b/templates/Crazy/wholesale.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('wholesale','','images/wholesale_over.jpg',1)"><img src="images/wholesale.jpg" name="wholesale" width="78" height="18" border="0" id="wholesale" /></a></div>
			<div id="nav_button"><a href="http://Crazy.com/dev/b2b/templates/Crazy/about.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('about','','images/about_over.jpg',1)"><img src="images/about.jpg" name="about" width="44" height="18" border="0" id="about" /></a></div>   </div>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 18, 2007 11:34 pm
by Benjamin
Look for a file called main page or something similar. You would place the js in there between the <head> tags.