...

View Full Version : PHP in Javascript (or the otherway)



germanic42
05-12-2004, 04:53 PM
I am looking for an easy way to add image links to my web site. I'd like to add the image name, file name and type to a database then have PHP output them on the page. BUT I would like the output to be entered in a Javascript to utilize what the Javascript has to offer.

The end result of the Javascript is:

<a href="test.gif" onClick="return enlarge('test.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

I need the PHP output to go where both test.gif(s) are in the above code and, of course, to be repeated as many times as there are database entries for that type of image.

The end result would be:

<a href="test.gif" onClick="return enlarge('test.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

<a href="second.gif" onClick="return enlarge('second.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

<a href="another.gif" onClick="return enlarge('another.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

etc.

Thanks,

Spookster
05-12-2004, 05:38 PM
Just use PHP to echo out anything you want used in the script:






<a href="test.gif" onClick="return enlarge('<?php echo $imagevariable; ?>',event,'center', 594, 350)"><strong>Test Image</strong></a>







If you need to output a bunch of links to images then set up a loop and have PHP output either the entire link or just echo the parts of the link that correspond to the data retrieved from your database query.

germanic42
05-13-2004, 04:02 AM
Thanks,

So far I have the following coding:



<?php

$dbh=mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database",$dbh);
$sql = "SELECT * From areas where type1 = 'divident'";
$result = mysql_query($sql, $dbh) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)){
$type1 = $newArray['type1'];
$type2 = $newArray['type2'];
$image = $newArray['image'];
$file = $newArray['file'];

}
?>

<p><a href="<?php echo $file; ?>" onClick="return enlarge('<?php echo $file; ?>',event,'center', 450, 250)"><strong>
<?php echo $image; ?/strong></a></p>



-----

This will output exactly what I need. My problem now is how to make the loop so all the entries are listed. What would you suggest?

Thanks, (as you can see, I'm very new to this. The last time I programed, computers were the size of a room and you had to use punch cards for input)

Spookster
05-13-2004, 04:11 AM
What you need to do is loop through the recordset returned from your query.




<?php

$dbh=mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database",$dbh);
$sql = "SELECT * From areas where type1 = 'divident'";
$result = mysql_query($sql, $dbh) or die(mysql_error());

while($row = mysql_fetch_array($result)){
?>
<p><a href="<?php echo $row['fieldname']; ?>" onClick="return enlarge('<?php echo $row['fieldname']; ?>',event,'center', 450, 250)"><strong>
<?php echo $image; ?/strong></a></p>

<?php
}//end while loop
?>



Just change "fieldname" to whatever the column name is in your database table that you want to use.

germanic42
05-13-2004, 08:56 PM
Thanks, I've been able to make everything work fine. Now I have to disect what you've done so I can understand the mechanics.

By doing it this way, I will save a lot of time. I am currently redesigning my web site that is over 4000 pages. Here is a finished page:

http://www.oldnewark.com/areas/divident.php

Thanks again,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum