...

View Full Version : PHP variables to Javascript variables - quick question



pavinder
05-06-2006, 10:38 AM
I have a program written in Javascript and fully functioning which takes a user-selected directory name and then displays all the photos in that directory in a certain format.

I am now wanting to expand the program to allow users to optionally enter their own strings and the program will use all the photos from www.flickr.com which use that string as a tag.

I have a PHP interface to flickr (called PHPflickr) which collects all the relevant photo urls. I now need to get these images back into my Javascript so I can process them using the existing functions (rather than rewrite all my functions in PHP code and have two sets of functions in the program).

I found on another thread a means to do this for a date variable:

var jsArray = new Array(
<?php
$length = count($monthDataArray);
for ($i = 0; $i < $length; $i++)
{
echo '"' . addslashes($monthDataArray[$i]) . '"' . (($i < $length - 1) ? ',' : '') . "\n";
}
?>
but being completely new to PHP I'm not sure what "addslashes" might be or what formatting will be necessary if I am starting with an array of urls.

I tried:

<script type="text/javascript">
<!--

var maxxarray = new Array(
<?php
$length = count($maximgsarray);
for ($i = 0; $i < $length; $i++)
{
echo '"' . $maximgsarray[$i] . '"' . (($i < $length - 1) ? ',' : '') . "\n";
}
?>

for (i=1; i <= 12; i++)
{
document.write(maxxarray[i]+"<br>");
}
//-->
</script>
inside my page at:
http://www.moonmilk.org/worlds/testflickr.php
but the routine above does not seem to be working. No output.

Could some kind soul advise me where I am going wrong? Each array element is a url in the form:
"http://static.flickr.com/filename.jpg"

My full page code is:

<html>
<head>
<title>PHP flickr images test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="hey there">

<script type="text/javascript">
<!--

var maximgct=8;
var maximgsarray=new Array(maximgct);

//-->
</script>

</head>
<body>

<?php
require_once("phpFlickr/phpFlickr.php");
$maximgs = 12;
// Create new phpFlickr object
$f = new phpFlickr("fbc2211a8778e91cd26d3de027eac206");

// echo "maximgcount is $maximgs total";
echo "<p>photos tagged with \"cloud\" and maximum of $maximgs<br>\n";
// Search by the single tag "cloud"
$photos_red = $f->photos_search(array("tags"=>"cloud", "sort"=>"relevance", "per_page"=>$maximgs));
foreach ($photos_red['photo'] as $photo) {
// Build image and link tags for each photo
$val1= "http://www.flickr.com/photos/$photo[owner]/$photo[id]";
echo "<a href=http://www.flickr.com/photos/$photo[owner]/$photo[id]>";
$val2= $f->buildPhotoURL($photo, "thumbnail");
echo "<img border='0' alt='$photo[title]' " . "src=" . $f->buildPhotoURL($photo, "thumbnail") . ">";
echo "</a>";
$i++;
$maximgsarray[i]=$val1;
echo "<br>$maximgsarray[i]<br>$val2<br>";
// If it reaches the 11th photo, insert a line break
if ($i % 10 == 0) {
echo "<br>\n";}
}
echo "</p>\n";
?>

<script type="text/javascript">
<!--

var maxxarray = new Array(
<?php
$length = count($maximgsarray);
for ($i = 0; $i < $length; $i++)
{
echo '"' . $maximgsarray[$i] . '"' . (($i < $length - 1) ? ',' : '') . "\n";
}
?>

for (i=1; i <= 12; i++)
{
document.write(maxxarray[i]+"<br>");
}
//-->
</script>

</body>
</html>

Many thanks.

pavinder
05-07-2006, 05:30 AM
Don't worry....problem resolved:

A missing "$" sign and missing "'" marks when assigning the variables.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum