Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Store SQL Data in PHP Array (and convert to javascript)

    Hi folks,
    I'm trying to create a two dimensional javascript array from the contents of an sql database.

    The original non-database driven array I was working with looked something like this:

    var galleries = new Array();
    // Order: id, size, date (yyyymmdd), name, description, thumb image, left px, top px, z-index
    galleriesOriginal[0] = [0, 12,"20111012","Cats","A gallery of beautiful cat pictures","images/catThumb.jpg",480, 230, 42];
    galleriesOriginal[1] = [1, 10,"19990703","Dogs","A gallery of beautiful dog pictures","images/dogThumb",580, 230, 100];
    galleriesOriginal[2] = [2, 7,"19970401","Marmolsets","A gallery of beautiful marmoset pictures","images/marmosetThumb.jpg", 580, 330, 100];
    galleriesOriginal[3] = [3, 1,"20080610","Anvils","A gallery of beautiful anvil pictures","images/anvilThumb.jpg", 380, 430, 100];
    galleriesOriginal[4] = [4, 13,"20101212","Xylophones","A gallery of beautiful xylophone pictures","images/xylophoneThumb.jpg", 680, 430, 100];
    etc

    ... and this is the format I'm looking to recreate (staying away from key/value pairs if possible - is this even possible in PHP?)

    I've worked out how to convert a php array to javascript (easy using json_encode($array)). The problem I'm having is rendering it in the requitred format. I'm pretty new to all this so its' highly likely that the answer will be obvious and simple but I've come up against a brick wall and would appreciate some help.

    The relevant code I've written so far is as follows but it just ain't happening:

    $result = mysql_query("SELECT * FROM exampleTable ORDER BY ID") or die('No table found');
    while ($row = mysql_fetch_array($result)){
    $array[] = $row;
    }

    echo 'var array = ' .json_encode($array).';';



    Was thinking of something along the lines of:

    $result = mysql_query("SELECT * FROM exampleTable ORDER BY ID") or die('No table found');
    $array = array();

    for($i=0; $i<=4;$i++){
    for($ii=0; $ii<=4;$ii++){
    $array[] = array();
    $array[$i][$ii] = array($row[$i]$col[$ii]);
    }
    }

    echo 'var array = ' .json_encode($array).';';


    ... but this is leading to all sorts of problems. In short I'm having problems with the syntax around the for loop for generating the array and after many hours trawling the net for clues am no closer to getting it to work. Like I say, any help would be much appreciated.

    Many thanks
    Stef
    Last edited by mxbrainwrong; 02-15-2012 at 06:26 PM.


 

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
  •