Jump to content

Populating a multidimensional array in JS with PHP


spenceddd

Recommended Posts

Hi,

 

I am trying to work out how to populate a multi-dimensioanl array in javascript with php.

 

The php array $rows looks like this:

 

Array (

[0] => Array (

[id] => 105 [name] => Golf 3d model [creationDate] => 04/10/07 [dateCreated] => 31/01/2010 21:03 [dateModified] => [shortDesc] => This Golf was modelled as an experimental project to work on my experience using Maya. [longDesc] => [relatedItem01] => 0 [relatedItem02] => 0 [relatedItem03] => 0 [img01] => Golf_3d_model.jpg [img02] => -1 [img03] => -1 [img04] => -1 [img05] => -1 [download] => => -1 [logo] => -1 [keywords] => )

[1] => Array (

[id] => 103 [name] => AM Awards Pitch Visual 2010 [creationDate] => 04/12/2009 [dateCreated] => 20/01/2010 17:40 [dateModified] => [shortDesc] => Visuals created for a pitch to win an Awards night event. [longDesc] => Visuals created for a pitch to win an Awards night event. [relatedItem01] => 0 [relatedItem02] => 0 [relatedItem03] => 0 [img01] => AMAwardsCloseUp.jpg [img02] => AMAwardsWide01.jpg [img03] => -1 [img04] => AMAwardsCloseUp02.jpg [img05] => -1 [download] => => CrossrailTrainingVideo.flv [logo] => -1 [keywords] => awards evening visualisation visualization )

[2] => Array (

[id] => 104 [name] => ARM 2010 pre-visualisation [creationDate] => dd/mm/yy [dateCreated] => 26/01/2010 17:18 [dateModified] => [shortDesc] => These visuals were part of a large set of visuals at an early stage of the project. [longDesc] => These visuals were part of a large set of visuals at an early stage of the project. [relatedItem01] => 0 [relatedItem02] => 0 [relatedItem03] => 0 [img01] => ARM2010_OutsideOfMarquee01.jpg [img02] => ARM2010InsideMarquee01.jpg [img03] => ARM2010_Marquee_Lid_off01.jpg [img04] => ARM2010InsideMarquee02.jpg [img05] => -1 [download] => => -1 [logo] => -1 [keywords] => )

[3] => Array (

[id] => 100 [name] => Triangulus Logo [creationDate] => 04/12/10 [dateCreated] => 02/01/2010 13:08 [dateModified] => [shortDesc] => This is the Triangulus logo, a new Vendor Management Consultancy. Concept to Completion. [longDesc] => [relatedItem01] => 103 [relatedItem02] => 100 [relatedItem03] => 100 [img01] => Triangulus-Final-Logo.jpg [img02] => Trianulgus Logo Initial Concept.jpg [img03] => -1 [img04] => -1 [img05] => -1 [download] => => TriangulusLogo.flv [logo] => -1 [keywords] =>

)

)

 

and so far my code to transfer to js looks like this:

 

<?
foreach($rows as $key => $value)
{
echo "js_array[$key] = $value;\n";
foreach($rows[$value] as $key => $value)
{
	echo "js_array[$key] = $value;\n";
}

}
?>

 

Could anyone please help me to complete the code I need to get this working?

 

 

Thanks for any help on this.

 

Spencer

Link to comment
Share on other sites

Thanks for your suggestion maca but I don't really understand how that works. How would I then be able to access the whole array in js?

 

The reason I am doing this is because I want to only query the db when the page is loaded. After that I would like to be able to manipulate the data clientside with out having to go back to the server...

Link to comment
Share on other sites

Thanks for your suggestion maca but I don't really understand how that works. How would I then be able to access the whole array in js?

 

The reason I am doing this is because I want to only query the db when the page is loaded. After that I would like to be able to manipulate the data clientside with out having to go back to the server...

 

You need to look into json then, it stands for javascript object notation and can provide you with simple Javascript objects built directly from strings.

Link to comment
Share on other sites

Heres a little example of how it works:

<?php
// Setting simple multi-array up
$someArray['a']['1'] = 'cell: a1';
$someArray['a']['2'] = 'cell: a2';
$someArray['a']['3'] = 'cell: a3';
$someArray['b']['1'] = 'cell: b1';
$someArray['b']['2'] = 'cell: b2';
$someArray['b']['3'] = 'cell: b3';
$someArray['c']['1'] = 'cell: c1';
$someArray['c']['2'] = 'cell: c2';
$someArray['c']['3'] = 'cell: c3';

// Converts the array into a JSON string
$json_string = json_encode($someArray);
?>
<html>
<head>
<title>JSON Example</title>
</head>
<body>
<script type="text/javascript">
var someArray = <?php echo $json_string; ?>; /* Sets someArray */

/* Examples: Access the array in JS */
document.write('someArray[\'a\'][\'1\'] = ' + someArray['a']['1'] + '<br />'); 
document.write('someArray[\'b\'][\'2\'] = ' + someArray['b']['2'] + '<br />');
document.write('someArray[\'c\'][\'3\'] = ' + someArray['c']['3'] + '<br />');
</script>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.