|
Posted by rynato on 07/23/07 17:43
here is the PHP function:
=======================
function image($src,$width,$height,$alt=null,$title=null,$class=null,
$id=null,$name=null,$js=null) {
$httpTest = substr($src,0,4);
if ($httpTest != 'http') { // don't extract the file
extension if this is an absolute path
$ext = substr($src, strrpos($src, '.') + 1); // get the file
extension and use it as the subdirectory name
$ext = strtolower($ext); // convert to lower case
if ($ext == 'jpeg')
$path = 'jpg';
else
$path = $ext;
}
$img = '<img ';
if ($httpTest == 'http')
$img .= 'src="' . $src . '" ';
else
$img .= 'src="' . $path . '/' . $src . '" ';
$img .= 'width="' . $width . '" ';
$img .= 'height="' . $height . '" ';
$img .= 'alt="' . $alt . '" ';
if ($title)
$img .= 'title=" ' . $title . ' " ';
if ($class)
$img .= 'class="' . $class . '" ';
if ($id)
$img .= 'id="' . $id . '" ';
if ($name)
$img .= 'name="' . $name . '" ';
if ($js)
$img .= $js . ' ';
$img .= "/>\n";
return $img;
}
========
as you can see, any javascript is appended to the end of the tag, then
it gets closed with />
here is the bit of code which called the function:
==============
$query = "SELECT src,width,height,alt,title,class,id,name FROM img
WHERE fid = '$fid'";
$result = mysql_query($query);
$loop = mysql_num_rows($result);
for ($i=0;$i<$loop;$i++) {
$a_row = mysql_fetch_row($result);
$img[$i] = image($a_row[0],$a_row[1],$a_row[2],$a_row[3],$a_row[4],
$a_row[5],$a_row[6],$a_row[7],$a_row[8]);
}
==============
that's it. Very straightforward. When that didn't work, I tried this:
==============
$query = "SELECT src,width,height,alt,title,class,id,name FROM img
WHERE fid = '$fid'";
$result = mysql_query($query);
$loop = mysql_num_rows($result);
for ($i=0;$i<$loop;$i++) {
$a_row = mysql_fetch_row($result);
$a_row[8] = null;
$img[$i] = image($a_row[0],$a_row[1],$a_row[2],$a_row[3],$a_row[4],
$a_row[5],$a_row[6],$a_row[7],$a_row[8]);
if ($i == 0) // in the first tag, insert the
javascript
$img[0] = str_replace(' />',' onLoad="preloadImages()" />',
$img[0]);
}
==============
same result as if it had retrieved the js from the table - it comes
out as <img src="foo" title="foo" onLoad="
just chokes at the quote...
Navigation:
[Reply to this message]
|