[thelist] PHP + Javascript

Andrew Maynes andrew at uk-webdeveloper.co.uk
Thu Aug 1 08:20:01 CDT 2002


Hi all, apologises for the length of this.  I thought the code attached would
help solve my problem.

The following code displays images from a csv file

$filearray = file ("admin/databases/groups.csv");
			while (list ($no, $line) = each ($filearray)) {
			$s = split (";", $line);
			if ($s[0] == "MAIN GROUP") echo "<a href=\"products.php?m=". substr (md5
($s[1]), 0, 10) ."\"><img src=\"$s[1].gif\" alt=\"$s[1]\" border=\"0\"></a>";
							  }
							  unset ($filearray, $no, $line, $s);

The image name the name of the "MAIN GROUP" within the csv file.

What I would like to do is create a mouseover effect by incorporating some
javascript?

I now have this:

<script language="JavaScript">
<!--

function WM_imageSwap(daImage, daSrc){
  var objStr,obj;


  // Check to make sure that images are supported in the DOM.
  if(document.images){
    // Check to see whether you are using a name, number, or object
    if (typeof(daImage) == 'string') {
      // This whole objStr nonesense is here solely to gain compatability
      // with ie3 for the mac.
      objStr = 'document.' + daImage;
      obj = eval(objStr);
      obj.src = daSrc;
    } else if ((typeof(daImage) == 'object') && daImage && daImage.src) {
      daImage.src = daSrc;
    }
  }
}

// -->
</script>
<?php

function hyperlink($url, $caption, $variables = NULL)
{
	$url = '<a href = "' . $url;
    if (is_array($variables)) {
        $firstTimeThrough = TRUE;
        foreach ($variables as $key => $value) {
            if ($firstTimeThrough === TRUE) {
                $urlQueryString .= "?" . $key . "=" . urlencode($value);
                $firstTimeThrough = FALSE;
            } else {
                $urlQueryString .= "&" . $key . "=" . urlencode($value);
            }; // end if
        }; // end foreach loop
        $url .= $urlQueryString;
    }; // end if
    $url .= '">' . $caption . "</a>";

    return $url;
}; // end func

function image($src, $alt = NULL, $border = 0, $rollOver = FALSE,
$rolloverImageSrc = NULL)
{
    $imageCode = '<img src = "' . $src . '"';

    if (! is_null($alt)) {
        $imageCode .= ' alt = "' . $alt . '"';
    }; // end if

    $imageCode .= ' border = "' . $border . '"';
    if ($rollOver === TRUE) {
        $name = md5(uniqid(rand(),1));
        // we add a name so we can pass a name to the javascript function
        $imageCode .= ' name = "' . $name . '"';
        $imageCode .= ' onMouseOver = "WM_imageSwap(document.images[' . "'" .
$name . "'], '" .
            $rolloverImageSrc . "');" . '">';
    }; // end if
    return $imageCode;
}; // end func

$variables['zoo'] = "the worlds best chocolate";
$variables['1zoo'] = "the worlds best chocolate";




$filearray = file ("admin/databases/groups.csv");
while (list ($no, $line) = each ($filearray)) {
    $s = split (";", $line);
    $name = $s[1];
    $gifName = $name . ".gif";
    $rollOverName = $name . "_over.gif";
    if ($s[0] == "MAIN GROUP") {
        $variables['m'] = substr (md5 ($name), 0, 10);
        echo hyperlink("products.php", image($gifName, $name, 0, TRUE,
$rollOverName), $variables);
    }; // end if
}; // end while loop

?>


Once the page is loaded there is an alignment problem which I am putting down to
the _over images not be pre_loaded, so I am putting the code in for that old
chestnut.  The problem is and the question is:

Can someone spot why the return to the original imagine isn't working, the swap
back image?

this is the link
http://www.greenwichorganic.co.uk/test_shop/index.php

Thank you
Andrew


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.380 / Virus Database: 213 - Release Date: 7/24/02




More information about the thelist mailing list