/    Sign up×
Community /Pin to ProfileBookmark

PHP Looping to create Javascript multi-dimensional variable

[code=php]<script type=”text/javascript” src=”sylvester.js”></script>
<script type=”text/javascript”>
var M = $M([
<?php
for($i=0;$i<100;$i++)
{
echo “[“;
for($j=0;$j<100;$j++)
{
if ($j<99) echo “$j,”;
else if($j==99) echo “$j”;
}

if($i<99) echo “],”;
else if ($i==99) echo “]”;
echo chr(10);
}
?>
]);
[/code]

I need to build a 100×100 array. I’m using the sylvester plug-in to make a matrix. The code executes fine and when i go view source from the browser the 100 rows and columns are defined just fine. I think that the document is first loaded then the php loops are executed on the server. Is there any way i can load a multi-dimensional array with PHP. I must do it that way, cause later i’ll need to query the databases in the loops above.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jan 19.2010 — You have to create the PHP array as you go along. Bearing in mind it's been a while since I've used PHP, I think this works.[code=php]
<?php
$M = array();
for($i=0;$i<100;$i++)
{
echo "[";
$M[$i] = [];
for($j=0;$j<100;$j++)
{ $M[$i][] = $j;
if ($j<99) echo "$j,";
else if($j==99) echo "$j";
}

if($i<99) echo "],";
else if ($i==99) echo "]";
echo chr(10);
}
?>
[/code]
Copy linkTweet thisAlerts:
@kwaxauthorJan 20.2010 — You have to create the PHP array as you go along. Bearing in mind it's been a while since I've used PHP, I think this works.[code=php]
<?php
$M = array();
for($i=0;$i<100;$i++)
{
echo "[";
$M[$i] = [];
for($j=0;$j<100;$j++)
{ $M[$i][] = $j;
if ($j<99) echo "$j,";
else if($j==99) echo "$j";
}

if($i<99) echo "],";
else if ($i==99) echo "]";
echo chr(10);
}
?>
[/code]
[/QUOTE]


$M is not a php variable in my case

it's a way to declare a matrix in javascript using the sylvester plug-in.
Copy linkTweet thisAlerts:
@Declan1991Jan 20.2010 — What happens is, PHP is excuted on the server, and then the text file is sent to the browser to be executed. If you want to be able to use the array you've created in PHP, you'll have to create it separately from the JavaScript one.
Copy linkTweet thisAlerts:
@kwaxauthorJan 20.2010 — What happens is, PHP is excuted on the server, and then the text file is sent to the browser to be executed. If you want to be able to use the array you've created in PHP, you'll have to create it separately from the JavaScript one.[/QUOTE]

Precisely how do i do that? Do you have any examples?
Copy linkTweet thisAlerts:
@kwaxauthorJan 20.2010 — [B]<SCRIPT>[/B] tag:
[CODE]<script language="javascript" src="dostuff.php"></script>[/CODE]

[B]PHP[/B] file:
[code=php]<?php
echo "var M = $M([";
echo chr(10);
for($i=0;$i<100;$i++)
{
echo "[";
for($j=0;$j<100;$j++)
{
if ($j<99) echo "$j,";
else if($j==99) echo "$j";
}

if($i<99) echo "],";
else if ($i==99) echo "]";
echo chr(10);
}
echo "]);";
?>
[/code]

I'v put the php code to generate the javascript array in a separate file and added the link to the file in the src attribute of the script tag. Works!!!

I guess that this is not valid according to the XHTML syntax, sort of a trick.
Copy linkTweet thisAlerts:
@criterion9Jan 20.2010 — This would be valid Xhtml if you changed the script tag:
<i>
</i>&lt;script type="text/javascript" src="dostuff.php"&gt;&lt;/script&gt;
Copy linkTweet thisAlerts:
@kwaxauthorJan 20.2010 — This would be valid Xhtml if you changed the script tag:
<i>
</i>&lt;script type="text/javascript" src="dostuff.php"&gt;&lt;/script&gt;
[/QUOTE]

Thanks a lot, it works that way too.
Copy linkTweet thisAlerts:
@Declan1991Jan 20.2010 — Thanks a lot, it works that way too.[/QUOTE]
I'm a bit confused. I don't think I understood your original question if malformed XHTML was the answer. Anyway, I'm delighted it's working now.
Copy linkTweet thisAlerts:
@kwaxauthorJan 21.2010 — I'm a bit confused. I don't think I understood your original question if malformed XHTML was the answer. Anyway, I'm delighted it's working now.[/QUOTE]
That wasn't the original question. It was just a second problem that i encountered when i fixed the main one.
Copy linkTweet thisAlerts:
@toicontienJan 24.2010 — You may be able to create a multi dimensional array in PHP, then use the json_encode() function to generate a JavaScript string in JSON format, which could be used by JavaScript to create a native JavaScript object.
×

Success!

Help @kwax spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...