www.webdeveloper.com
Results 1 to 2 of 2

Thread: javascript function not returning value

  1. #1
    Join Date
    Feb 2012
    Location
    London
    Posts
    8

    javascript function not returning value

    Hi,
    Apologies if I'm doing something very obviously stupid here, I'm fairly new to this sort of thing.

    What I'm basically trying to do is crop a two dimensional array of coordinates to the same length as another array called from an sql database using php and then made useable in javascript via json_encode. The good news is that I've managed to accomplish this. The code below actually does this twice with the database information called in different orders so that I have two two-dimensional javascript arrays, each with an x and y coodinate tagged onto the end of each row.

    (For anyone who's interested in the whys and wherefores, the database contains information abpout different image galleries (title, description, thumbnail etc). I'm going to be using these two arrays along with jquery animate to allow visitors to the site to change the order in which thumbnails are displayed based on date gallery updated or gallery size).

    This all sort of works fine. There are a couple of functions, 'prune' which is the parent function (I think I'm using the terminology correctly) and two similar but separate 'populateArray' functions (1 and 2).

    The trouble is that the two inner functions don't seem to want to return a value. I can access the two arrays from within the populateArray functions but as soon as I try to return the values (as 'galDate' and 'galSize') I get nothing.

    Code is laid out below.

    Like I say, apologies if I'm making a really basic mistake here but I'm completely stumped and could do with a nod in the right direction.

    Thanks in advance.
    Stef

    Code:
    var sqlData1 = (<?php print json_encode($sqlData1);?>);
    var sqlData2 = (<?php print json_encode($sqlData2);?>);
    
    var coordinates = [
    ['100px','100px'],
    ['100px','200px'],
    ['100px','300px'],
    ['200px','100px'],
    ['200px','200px'],
    ['200px','300px'], 
    ['300px','100px'],
    ['300px','200px'],
    ['300px','300px'],
    ['400px','100px'],
    ['400px','200px'],
    ['400px','300px'],
    ['500px','100px'],
    ['500px','200px'],
    ['500px','300px']
    ]; 
    
    //Function to make arrays the same length    
    function pruneArrays(sqlData1,sqlData2, coordinates){
        for(i=(coordinates.length-sqlData1.length); i>0; i--){
          coordinates.pop(coordinates[i]);    
        }
      populateArray1(sqlData1, coordinates);
      populateArray2(sqlData2, coordinates);
      return();
    }
      
    //Function to create date ordered array with coordinates
    function populateArray1 (sqlData1, coordinates){
      for(i=0; i<sqlData1.length; i++){
          for(j=0; j<(sqlData1.length-1); j++){
            sqlData1[i].push(coordinates[i][j]);
          }
      }
      var galDate = sqlData1;  
      //document.write(galDate); - if this line is active 'galDate' displays no problem
      //document.write ("<br /><br />");
      return (galDate);
    } 
    
    //Function to create size ordered array with coordinates
    function populateArray2 (sqlData2, coordinates){
      for(i=0; i<sqlData2.length; i++){
          for(j=0; j<(sqlData2.length-1); j++){
            sqlData2[i].push(coordinates[i][j]);
          }
      }
      var galSize = sqlData2;
      //document.write(galSize);  - if this line is active 'galSize' displays no problem
      return (galSize);
    }   
      
    document.write(pruneArrays(sqlData1, sqlData2, coordinates));
    // The line above however, doesn't return anything.

  2. #2
    Join Date
    Feb 2012
    Location
    London
    Posts
    8

    Solved

    Please ignore my previous burblings. Solved by condensing the code as below:
    Code:
    //Function to make arrays the same length    
    function pruneArrays(sqlData1,sqlData2, coordinates){
        for(i=(coordinates.length-sqlData1.length); i>0; i--){
          coordinates.pop(coordinates[i]);    
        }
      
      var galDate = (populateArray(sqlData1, coordinates)); 
      var galSize = (populateArray(sqlData2, coordinates));
      return [galDate, galSize];
    }
      
    //Function to create date ordered array with coordinates
    function populateArray (sqlData, coordinates){
      for(i=0; i<sqlData.length; i++){
          for(j=0; j<(sqlData.length-1); j++){
            sqlData[i].push(coordinates[i][j]);
          }
      }
    return (sqlData);
    and putting the call to the function in separate script tags in the page body.

    Stef

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles