Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Okey where to start....
I am building my movie library.And i have two pages, index and movie.html.
Index.html will a page where will display movie items, each item, will have a name, a picture, score, short summary and director and screenplay names.And, also i will have a name from author, if the movie is based from book. All of that is taken from JSON file, that i have created locally.


In other html page, movie.html, i am planning to have more fancy design with more information. Like:wins, synopsis,cast and their characters, etc.

But here is the problem i am facing.

What I have tried:

I have this so far in index.html

JavaScript
$( document ).ready( function () {

    $.getJSON( "js/appjson.json", function ( data ) {
        for ( var i = 0; i < data.length; i++ ) {

            for ( var key in data[ i ] ) {

                if ( key === "novel" ) {
                    $( '#jsonLoad' ).append( '<a href="movies.html?id='%20+%20data[%20i%20].id%20+%20'" class="itemsHolder">' +
                    "<div class="titleHolder">" +
                    "<h2>" + data[ i ].name + "</h2>" +
                    "</div>" +
                    "<div class="novelAuthor">" +  "<p class="NovelTxt">" + "Novel by" + " " + data[ i ].novel +"</p>" + "</div> " +
                    "<div class="posterHolder">" + data[ i ].posterPath + "</div>" +
                    "<div class="summaryShort">" + data[ i ].summary + "</div>" +
                    "<div class="raiting"><p>" + data[ i ].imdb + "</p></div><div class="genderMovie"> " + data[ i ].gender + "</div> " +
                    "<div class="directorNdScreen">" + 'Directed by ' + " <p class="director">" + data[ i ].director + '</p>' + '  ' + ' Screenplay by ' + "<p class="screenplay">" + data[ i ].screenplay + "</p>" + "</div>"


                    + "</a>" )
                }

            }
            if(!data[i].novel){
                $( '#jsonLoad' ).append( '<a href="movies.html?id='%20+%20data[%20i%20].id%20+%20'" class="itemsHolder">' +
                "<div class="titleHolder">" +
                "<h2>" + data[ i ].name + "</h2>" +
                "</div>" +
                "<div class="posterHolder">" + data[ i ].posterPath + "</div>" +
                "<div class="summaryShort">" + data[ i ].summary + "</div>" +
                "<div class="raiting"><p>" + data[ i ].imdb + "</p></div><div class="genderMovie"> " + data[ i ].gender + "</div> " +
                "<div class="directorNdScreen">" + 'Director by ' + " <p class="director">" + data[ i ].director + '</p>' + '  ' + ' Screenplay by ' + "<p class="screenplay">" + data[ i ].screenplay + "</p>" + "</div>"


                + "</a>" )
            }

        }
    } )

} );


Lets say i have a JSON file like this
JavaScript
[
  {
    "id":1,
    "name": "Godzilla",
    "imageStill" :" ",
  },
{
    "id":2,
    "name": "Transformers 1",
    "movieStill" : " ",
  }
]


And when i click an item from index.html, in movie.html, i want to have one image.
The problem i have is that i have two images. Because, is taking images from both objects.
JavaScript
$( document ).ready( function () {

    $.getJSON( "js/appjson.json", function ( data ) {


            for ( var i = 0; i < data.length; i++ ) {

                    $( '.MovieInfo' ).append(
                        "<div class="imgStill">" + data[ i ].movieStill + "</div>"
                    )



        }
    } );


} );
Posted
Updated 25-Sep-16 0:37am
v2

1 solution

If you have a look at your code, you will see that you are using every object in your JSON document. That is why you get 2 images and both the elements are being used in your HTML web page.
JavaScript
for ( var i = 0; i < data.length; i++ ) {
    $( '.MovieInfo' ).append(
        "<div class="imgStill">" + data[ i ].movieStill + "</div>"
    ) 
}

You need to change the code to the following,
JavaScript
for ( var i = 0; i < data.length; i++ ) {
    // Only use the required value. 
    if(data[i].id == idRequired) {
        $( '.MovieInfo' ).append(
            "<div class="imgStill">" + data[i].movieStill + "</div>"
        );
    }
}

This will check the condition by passing the required id as, idRequired to the function call. Then that id will be used to either enter the data to the HTML page, or skip adding anything at all, such as showing a "Not found" message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900