1

I retrieve image paths with the function get_all();. This get_all function retrieves images as an object. This object has the attributes name, source_path and date. I want my javascript to add images to a div. I have the following:

The instantiate.php includes files like Jquery and another JS file.

<?php
    require_once("../../include/instantiate.php");    
    $photos = Photos::get_all();
    $JSPhotos;
    foreach($photos as $photo) { $JSPhotos + $photo->source_path; }
?>

<script type="text/javascript">
    $(document).ready(function() {
        var photos = <?php echo json_encode($JSPhotos); ?>;
        for(var i = 0; i <= 10; i++)
        {
            create_image("../"+photos[i]);
        }
    });

This does not work. Anyone got a solution? Solution in Jeroen's Post!

New Problem;

In the create_image function I set the class and src of the image element. When you click such an image I want an alert box to show up. I have checked if the class is set correctly, and I concluded that all images did have the classname "imgid". So, any idea why this dont work?

Script in the javascript part:

$(".imgid").click(function() {
    alert("hey");
});
7
  • whats the error you're getting? Commented Mar 11, 2014 at 21:33
  • Appearently the variable "photos" in the javascript part is "null". Commented Mar 11, 2014 at 21:34
  • thats because your just doing $JSPhotos +. theres nothing assigned to the variable Commented Mar 11, 2014 at 21:35
  • You should pass an actual object-graph to json_encode - what is $JSPhotos + $photo->source_path supposed to do? It's sure not creating an array. Commented Mar 11, 2014 at 21:36
  • 2
    @user2014780 Perhaps, although it might also include too much data. There isn't a problem with (and I actually recommend doing so) creating an intermediate structure .. if it's actually created correctly. Commented Mar 11, 2014 at 21:37

1 Answer 1

1

You are not assigning anything to your variable.

You probably want:

$JSPhotos = array();
foreach($photos as $photo) {
  $JSPhotos[] = $photo->source_path;
}

Or something similar.

5
  • Thanks. I have done exactly that, but unfortunately I get broken images now. I have checked the contents of the photos variable in JS and its all "null". Commented Mar 11, 2014 at 21:43
  • @user2014780 Check your $photos and $JSPhotos variables. What do they contain (do a var_dump())? Commented Mar 11, 2014 at 21:45
  • Thanks, it works now. Can't remember what the problem was though (short memory). But right now I have created a function that doesnt work. Can you please check the OP? I am sure you solve it in a second haha! Commented Mar 11, 2014 at 22:27
  • @user2014780 You would need to show the generated html or the complete function. Either way, I would recommend posting a new question, preferably with an online example to test with. Commented Mar 11, 2014 at 22:32
  • @user2014780 By the way, you probably need event delegation as the elements are probably not yet added to the page when you add your click-handler. Commented Mar 11, 2014 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.