views:

139

answers:

3

i am trying to create a fade effect with the following...ive been told im nearly there apart from the passing of the json array. At the moment no images are displayed.

 //generate all the boxes
$.get('images.php',function(data){
  for (var i=0; i < totalBoxes; i++){
      var randomImage = data[Math.floor(Math.random() * data.length)];
      $('<div class="pf-box"><img class="black" src="' + randomImage['black'] + '" /><img class="colour" src="' + randomImage['colour'] + '" /></div>').hide().appendTo('#bg').fadeIn('slow').filter('.colour').css("opacity", 0);
  }
 },'json');

 //add the hover behavior to all the elements
 $('.colour').hover(function() {
   $(this).stop().fadeTo(700, 1);
 },function() {
   $(this).stop().fadeTo(700, 0);
 });

and images.php

    <?php 
   header('Content-type: application/json');
echo '[  
    {'black' : 'images/random/1.jpg', 'colour' : 'images/random/1-c.jpg'},  
    {'black' : 'images/random/2.jpg', 'colour' : 'images/random/2-c.jpg'}
]';
    ?>
+2  A: 

Don't you need to escape the quotes inside the JSON string? Otherwise the php interpreter will fail to send all of what you want, and may even barf out some errors.

Berry
I am trying to create this effect: http://www.yellostudio.co.uk/temp/index.php with the code above on http://www.yellostudio.co.uk/temp/indexV2.phpDo you know where i am going wrong?
Andy
A: 

Use randomImage.black instead of randomImage['black']

Mike Gleason jr Couturier
there's no difference between the two in JavaScript
Mike Haboustak
there is no official support for associative arrays in JavaScript.. give those points back
Mike Gleason jr Couturier
i didnt know i had removed any points sorry
Andy
no problem, I forgot the "!" telling that I wasn't really mad.. :) here's a reference: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
Mike Gleason jr Couturier
randomImage.black didnt break anything but didnt create the hover either...
Andy
"i didnt know i had removed any points sorry" Don't bother, I think it's Mr. Haboustak
Mike Gleason jr Couturier
No probs Mike i appreciate your help...do you know how i could get teh hover working?
Andy
if you do a alert(randomImage) in your loop, you have anything?
Mike Gleason jr Couturier
Setting the opacity to 0 renders the image completely transparent...
Mike Gleason jr Couturier
comes up with a load of [object object]
Andy
the transparent image should then fade in
Andy
'randomImage' in the code above isn't an array, it's an object (see the JSON). The variable 'data' is the array and that array is being indexed with a numeric index. So randomImage['black'] here is just fine (and quite common to leverage the language's dynamic nature).
Mike Haboustak
Sorry i am out of my depoth here...do you know why it isnt hovering?
Andy
Andy couldn't have downvoted you. He has only 13 points.
thephpdeveloper
thanks...Mauris do you know why this hover wont hover?
Andy
You're only applying the hover to the colour images: $('img', $('#bg'))
Mike Haboustak
i changed .colour to your $('img', $('#bg')) and it made no difference ;(
Andy
If you remove all the animation and only append a visible image... maybe we look at the wrong part of the problem?
Mike Gleason jr Couturier
? not sure i am with you
Andy
remove everything after the appendTo and remove the hide()
Mike Gleason jr Couturier
@Mike: Back to your answer: What do you want to tell us? In standard ECMAScript randomImage.black === randomImage['black'], so what?
Boldewyn
@Boldewyn: It wasn't an answer, it was a comment. And the point of the comment was that the original code change suggested by this answer is inconsequential. The two options are synonyms and both are correct.
Mike Haboustak
A: 

Your echo is failing because of the single quotes in the JSON output breaking out of the echo.

Enclose your string with different quotes so you can echo properly:

<?php
header('Content-type: application/json'); 
echo "[
    {'black' : 'images/random/1.jpg', 'colour' : 'images/random/1-c.jpg'},
    {'black' : 'images/random/2.jpg', 'colour' : 'images/random/2-c.jpg'} 
]"; 
?>

Note the use of double quotes to enclose the echo string, instead of the single quotes you've used. (if you had double quotes inside the string, then you'd reverse it and use single on the outside).

buymeasoda