By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Problem with array

P: n/a
Hi
I' m trying convert a php array to a javascript array like this:

var danevar1 = "<? print $dane1; ?>";
var danearray1= new Array();
danearray1 = danevar1.split(",");

var suma1=0;
for(var i=0; i<danearray1.length; i++){
suma1=suma1+danearray1[i];
}
alert(suma1);
if(suma1==0){
alert("False");
}

It seems that something is wrong in line:
var danevar1 = "<? print $dane1; ?>";

because this is what im getting from alert(suma1):
0"<? print $dane1; ?>

what am I doing wrong?
What should i change to make it work?

Thanks
Leszek
Feb 5 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Leszek wrote:
I' m trying convert a php array to a javascript array like this:

var danevar1 = "<? print $dane1; ?>";
var danearray1= new Array();
This line is unnecessary. Implementations of ECMAScript Ed. 3 and below are
not strictly typed, and the Array object is already created by the next
line.
danearray1 = danevar1.split(",");

var suma1=0;
for(var i=0; i<danearray1.length; i++){
for (var i = 0, len = danearray1.length; i < len; i++)

is more efficient.
{
suma1=suma1+danearray1[i];
Each element of this array is a string value, so string concatenation will
occur here, not addition.
}
alert(suma1);
if(suma1==0){
alert("False");
}
Your indentation certainly allows for improvement.
It seems that something is wrong in line:
var danevar1 = "<? print $dane1; ?>";

because this is what im getting from alert(suma1):
0"<? print $dane1; ?>

what am I doing wrong?
What should i change to make it work?


This is not really a J(ava)Script problem, you are off topic here.

If the above is exactly what the alert box displays, then you have
short_open_tag=off in your php.ini (which is a Good Thing). Use
`<?php ... ?>' to fix that.

However, if `$dane' is a reference to your PHP array, you do not know PHP
well enough anyway. The string representation of an array there is not a
comma-separated list of its elements, it is "Array". In that case you
should use the PHP implode() function and create the ECMAScript-conforming
array explicitly:

var a = [<?php echo implode(',', $dane1); ?>];

If every element of your PHP array is a value which string representation
can be interpreted as one of the ECMAScript number type (or a non-string
type for that matter), the loop does the addition (with implicit type
conversion if necessary). (If not, a syntax error does occur here.)

However, a better method to calculate the sum of array element's values
has been suggested to you shortly before in the thread starting with
news:dr**********@news.onet.pl already (I would appreciate it if you
continued threads unless the main problem changed significantly.)
PointedEars
Feb 5 '06 #2

P: n/a
In article <ds**********@news.onet.pl>, le*******@poczta.onet.pl says...
Hi
I' m trying convert a php array to a javascript array like this:

var danevar1 = "<? print $dane1; ?>";
var danearray1= new Array();
danearray1 = danevar1.split(",");

var suma1=0;
for(var i=0; i<danearray1.length; i++){
suma1=suma1+danearray1[i];
}
alert(suma1);
if(suma1==0){
alert("False");
}

It seems that something is wrong in line:
var danevar1 = "<? print $dane1; ?>";

because this is what im getting from alert(suma1):
0"<? print $dane1; ?>


The PHP isn't executing. Why not just get PHP to fill the JS array for
you, rather than what you're tring to do now?

URL?

--

Hywel
http://kibo.org.uk/
Feb 5 '06 #3

P: n/a
Leszek wrote:
Hi
I' m trying convert a php array to a javascript array like
this:

var danevar1 = "<? print $dane1; ?>";
var danearray1= new Array();
danearray1 = danevar1.split(",");
There is no need to create a new Array if you are then going to
immediately replace it with the Array returned from the -
String.prototype.split - method.

var danevar1 = "<? print $dane1; ?>";
var danearray1 = danevar1.split(",");

- would do, as would:-

var danearray1 = ("<? print $dane1; ?>").split(",");
var suma1=0;
for(var i=0; i<danearray1.length; i++){
suma1=suma1+danearray1[i];
The result of the - String.prototype.split - is an array of strings.
When you use the + operator where either of its operands is a string the
operation performed is string concatenation (with non-string operands
being type-converted into strings) not numeric addition. So as you will
be concatenating every string value in the - danearray1 - all this
process will do is the equivalent of taking the original string assigned
to - danevar1 -, removing the commas and appending it to the string
equivalent of the number zero. I.E., the equivalent of:-

var suma1 = 0 + ("<? print $dane1; ?>").replace(/,/g, '');

Though it is unlikely that you wanted to do that. More likely you wanted
the mathematical sum of the numeric equivalents of the string values in
the - danearray1 - array. To do that you would need to type-convert each
string value into a numeric value prior to the addition. There are many
ways of ding this; the unary + operator is the shortest and most
efficient type-converting from string to number method. Calling the
number constructor as a function with the string as its argument is
probably the most self-documenting method. E.G:-

var numValue = +stringValue;

var numValue = Number(stringValue);

However, Other mathematical operators, subtraction, division and
multiplication, type-convert string operators to numbers as a matter of
course so an alternative to summing the values in a string array might
be to start with zero, subtract each value in tern and then negate the
result:-

var suma1=0;
for(var i=0; i<danearray1.length; i++){
suma1 = suma1 - danearray1[i];
//Or shortened to - suma1 -= danearray1[i];
}
suma1 = -suma1;
}
alert(suma1);
if(suma1==0){
alert("False");
}

It seems that something is wrong in line:
var danevar1 = "<? print $dane1; ?>";

because this is what im getting from alert(suma1):
0"<? print $dane1; ?>
If - <? print $dane1; ?> - is appearing in the result then your PHP is
not acting as you expect and inserting the value of $dane1 into the
javascript string literal. That would be a PHP question better asked
elsewhere.

However, even if the PHP is not acting there is no good reason to find a
quote mark between the zero and <? print $dane1; ?> in the output.
what am I doing wrong?
What should i change to make it work?


The above and whatever is also wrong with your PHP.

Richard.
Feb 5 '06 #4

P: n/a
I tried with
var danevar1 = '<?php echo $dane1; ?>';

but still i'm getting the same result after alert(suma1):

0<?php echo $dane1; ?>
this is how I make $dane:
<?php
$pomocnicza='zamow';
if(isset($_POST['submit2'])){
$danearray1=$_POST['$daneres'][$_POST['fchoice']][$pomocnicza];
if(isset($danearray1)){
$dane1= implode(",",$danearray1);}
}
?>

Is there a function that can change string value to an integer?

Feb 5 '06 #5

P: n/a
In article <ds**********@news.onet.pl>, le*******@poczta.onet.pl says...
I tried with
var danevar1 = '<?php echo $dane1; ?>';

but still i'm getting the same result after alert(suma1):

0<?php echo $dane1; ?>
this is how I make $dane:
<?php
$pomocnicza='zamow';
if(isset($_POST['submit2'])){
$danearray1=$_POST['$daneres'][$_POST['fchoice']][$pomocnicza];
if(isset($danearray1)){
$dane1= implode(",",$danearray1);}
}
?>

Is there a function that can change string value to an integer?


Presumably you're replying to something? Quote properly.

--

Hywel
http://kibo.org.uk/
Feb 5 '06 #6

P: n/a
VK

Leszek wrote:
Hi
I' m trying convert a php array to a javascript array like this:

var danevar1 = "<? print $dane1; ?>";
var danearray1= new Array();
danearray1 = danevar1.split(",");

var suma1=0;
for(var i=0; i<danearray1.length; i++){
suma1=suma1+danearray1[i];
}
alert(suma1);
if(suma1==0){
alert("False");
}

It seems that something is wrong in line:
var danevar1 = "<? print $dane1; ?>";

because this is what im getting from alert(suma1):
0"<? print $dane1; ?>

what am I doing wrong?
What should i change to make it work?


<? print $dane1; ?> doesn't go through the PHP parser, it stays exactly
as it is: a string "<? print $dane1; ?>". Do you have PHP installed
properly on you computer?

Feb 5 '06 #7

P: n/a
yes php works ok. But maybe there are some options i need to set?
Thanks
Leszek
Feb 5 '06 #8

P: n/a
VK

Leszek wrote:
yes php works ok. But maybe there are some options i need to set?
Thanks
Leszek


Please do not be upset on possibly stupid question, but where is your
page showing the alert and where is your PHP parser? I mean are you
working on the same machine or are you getting a PHP page from your
server to handle it with JavaScript on the client side?

Feb 5 '06 #9

P: n/a
Leszek wrote:
yes php works ok. But maybe there are some options i need to set?


Read my posting, and ask your PHP questions where they are on topic,
not here.
PointedEars
Feb 5 '06 #10

P: n/a
On 2006-02-05, Leszek <le*******@poczta.onet.pl> wrote:
Hi
I' m trying convert a php array to a javascript array like this:

var danevar1 = "<? print $dane1; ?>";
if $dane1 is an array this produces

var danevar1 = "Array";
var danearray1= new Array();
danearray1 = danevar1.split(",");

var suma1=0;
for(var i=0; i<danearray1.length; i++){
suma1=suma1+danearray1[i];
}
alert(suma1);
if(suma1==0){
alert("False");
}

It's much easier to do the translation in PHP (fairly general solution:)

var danearray=[["<?
echo implode('"],["',preg_replace('/"/','\\"',$dane1));
?>"]];

for arrays containing only numbers the following will suffice:

var danearray=[[<? echo implode("],[",$dane1); ?>]];
because this is what im getting from alert(suma1):
0"<? print $dane1; ?>

what am I doing wrong?


That looks like PHP isn't running...

Bye.
Jasen
Feb 6 '06 #11

P: n/a
On 2006-02-05, Leszek <le*******@poczta.onet.pl> wrote:
I tried with
var danevar1 = '<?php echo $dane1; ?>';

but still i'm getting the same result after alert(suma1):

0<?php echo $dane1; ?>
this is how I make $dane:
<?php
$pomocnicza='zamow';
if(isset($_POST['submit2'])){
$danearray1=$_POST['$daneres'][$_POST['fchoice']][$pomocnicza];
if(isset($danearray1)){
$dane1= implode(",",$danearray1);}
}
?>
Is there a function that can change string value to an integer?


+

as in

(+"6")

but your problem seems to be with the PHP
check the source that your browser sees from the server...
Bye.
Jasen
Feb 6 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.