Please help me with this. I have been trying for hours, with all permuations and I cannot see what is wrong with this code.
I have hardcoded values to the variables image_step and $slideshow_key but they are normally dynamically assigned ( this bit works ). The usage of image_step in the assignment of cur_image_desc is not working.
<script type=“text/javascript”>
var image_step = 3;
<?php $slideshow_key = ‘gallery’; ?>
var image_name = “image_<?php echo $slideshow_key . '”'; ?>;
so from what i see… you are… trying to tack a 3 on the end of a description. + is javascript’s string concatenation character when the left side of the binary is a string.
PHP is used to process data in the server and output it to the browser. In your example you’re trying to use PHP as if it were JS. You need to use PHP to set up and output all the JS codes you’ll need. for example…
Didn’t test it, but that’s the idea.
Basically the idea is to have PHP write the code for you.
Look at the source code of the page and see if looks good.
The 3 is just an example. This piece of code is in a loop and the 3 is like a subscript but tagged onto the end of a variable name. These variables are assigned from a php array that holds descriptions in another part of the code. Somebody said that I couldn’t assign a php array to a javascript array. I am trying to assign descriptions to sliding images. I have the sliding images working but what should be the simple part is proving not to be so.
well someone lied to you about not being able to make a javascript array from a php array. You certainly can, it’s not something you can simply -assign-, but you can certainly output a PHP array into javascript.
Thanks, I see the idea. I must admit that my code looks a mess at the moment; I’ve hacked it around so much to try and get it working.
However, the problem persists. The variable ‘image_step’ cannot be a php variable ( so I’ve been led to believe ). It is set by a javascript loop that is invoked when a timer expires, hence the mongrel syntax.
Thanks, I suspected as much, and now I will experiment down this road. There are lots of examples on the internet where a php array is being assigned to individual javascript variables. It must set a lot of people off on the wrong track!
It is pure Javascript with no php but it uses individual variables rather than an array. There were others doing similar things that took data from php arrays and converted them to individual Javascript variables.
Somebody’s just sent me a one-liner that I will need to digest.
would be helpful if we could have a look at the rest of the code. There’s no way to call PHP within a Javascript loop. PHP only gets fired once as the page loads from top to bottom and wont get fired over and over again from inside a JS loop.
You have to setup all the variables required for your script ahead of time.
That’s right. The only way to involve PHP from JavaScript is by using Ajax to get a response from a PHP script, and to then use a callback event to continue on with the Javascript code.
Because PHP runs on the server-side and JavaScript on the client-side - it’s just not possible to intermingle the two languages together.
UT503_GALLERY_SLIDE.INC.php
Author: Steve O'Brien Apr 2011
V1.1 Original
Display Gallery as a slideshow.
This script is to display a series of images as a scrolling slideshow.
The images are clumped together in a gallery.
A gallery can have parameters for image sizes and cycle speed etc.
a gallery can also be referenced by a slideshow that also has the same parameters.
In this instance the parameters from the slideshow will override.
There are also default parameters.
$slideshow_key is a unique parameter for each set of images.
This parameter is tagged onto each element to make it unique.
This has to be done because this script can be called more than once from the same page.
*/
if ( !isset($slideshow_key) or $slideshow_key == '' ) {
$slideshow_key = 'gallery';
}
// Get gallery images - invokes a MySql cursor
include 'UT250_FILE_DETAILS.INC.PHP'; // Get files related to gallery
$img_number = 0;
$file_idx = 0;
$image_size = array();
while ( $file_idx < $inc_file_count ) {
/*
Reads details of each image into a php array
*/
$fupl_id = $inc_fupl_id[$file_idx];
$filename = $inc_filename[$file_idx];
$media_name = $inc_filename[$file_idx];
$file_desc = $inc_file_desc[$file_idx];
$file_type = $inc_file_type[$file_idx];
$thumbnail = $inc_thumbnail_id[$file_idx];
$file_img_x = $inc_img_x[$file_idx];
$file_img_y = $inc_img_y[$file_idx];
/*
This function returns the actual size for each image based upon supplied
required sizes whilst maintaining the correct aspect ratio.
the parameters are: filename, height, width, maximum size.
*/
$image_size = image_size ( $inc_filename[$file_idx],
$img_y,
$img_x,
$img_x );
$height[$file_idx] = $image_size[1];
$width[$file_idx] = $image_size[0];
$file_idx = $file_idx + 1;
}
?>
<script type=“text/javascript”>
<?php
$image_indx = 0;
$image_name = 'image_' . $slideshow_key;
while ( $image_indx < $inc_file_count ) {
/*
This loop cycles through the php array and sets up separate Javascript
variables for each. It can eventually be combined with the preceding loop.
*/
$image_instance = $image_name . '_' . $image_indx;
echo ' var ' . $image_instance . ' = new Image(); ';
echo $image_instance . '.src = "' . $inc_filename[$image_indx] . '";';
echo $image_instance . '.width = "' . $width[$image_indx] . '";';
echo $image_instance . '.height = "' . $height[$image_indx] . '";';
echo ' var image_desc_' . $slideshow_key . '_' . $image_indx . ' = "' . $inc_file_desc[$image_indx] . '";';
$image_indx = $image_indx + 1;
}
echo ' var image_name; ';
// Get image display string
$alt = '';
?>
I want to help with this, but JavaScript tends to require a different way of thinking than PHP does.
Can you give us a test page that demonstrates the problem, or can you provide the parts that would allow us to easily set up our own test that’s close to how you intend to use the code?