mxbrainwrong Posted February 22, 2012 Share Posted February 22, 2012 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 <script>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); } populateArray1(sqlData1, coordinates); populateArray2(sqlData2, coordinates); return();} //Function to create date ordered array with coordinatesfunction populateArray1 (sqlData1, coordinates){ for(i=0; i<sqlData1.length; i++){ for(j=0; j<(sqlData1.length-1); j++){ sqlData1.push(coordinates[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 coordinatesfunction populateArray2 (sqlData2, coordinates){ for(i=0; i<sqlData2.length; i++){ for(j=0; j<(sqlData2.length-1); j++){ sqlData2.push(coordinates[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.</script> Link to comment Share on other sites More sharing options...
ShadowMage Posted February 22, 2012 Share Posted February 22, 2012 document.write(pruneArrays(sqlData1, sqlData2, coordinates)); This line is not going to print anything. It's printing the return value of pruneArrays, which returns nothing. Link to comment Share on other sites More sharing options...
mxbrainwrong Posted February 22, 2012 Author Share Posted February 22, 2012 Please ignore my previous burblings. Solved by condensing the code as below: //Function to make arrays the same length function pruneArrays(sqlData1,sqlData2, coordinates){ for(i=(coordinates.length-sqlData1.length); i>0; i--){ coordinates.pop(coordinates); } var galDate = (populateArray(sqlData1, coordinates)); var galSize = (populateArray(sqlData2, coordinates)); return [galDate, galSize];} //Function to create date ordered array with coordinatesfunction populateArray (sqlData, coordinates){ for(i=0; i<sqlData.length; i++){ for(j=0; j<(sqlData.length-1); j++){ sqlData.push(coordinates[j]); } }return (sqlData);} and putting the call to the function in the body of the page. Apologies for the false alarm / time wasted. CheersStef Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.