473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Problem with array

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
11 2379
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
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
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
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
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
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
yes php works ok. But maybe there are some options i need to set?
Thanks
Leszek
Feb 5 '06 #8
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
0
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
3
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.