Solved

javascript array not accepting text from php line

Posted on 2013-11-25
9
394 Views
Last Modified: 2013-11-26
have some javascript that is working fine until i add one line
it builds five arrays that hold data from out of a table and just grabs the field and pops it in that array,

if i change the $item->tissue (this is text) to $item->userid (its a number ) it will collect the information
tissue is available to be grabbed, but for some reason it does not like using text in this position cannot see why it will not work

***** the below line does not work
echo ("      price_list4[".$curcatnum."][".$itemnum."] = ".$item->tissue."; ");
***** the below line does work
echo ("      price_list4[".$curcatnum."][".$itemnum."] = ".$item->userid."; ");

see the image for the display of whats in the .$item

var price_list=new Array();
var price_list1=new Array();
var price_list2=new Array();
var price_list3=new Array();
var price_list4=new Array();



// IT ALREADY HAS THE DATA BY THIS POINT
// LOOK AT ITEM  
function initprice(){
<?php
 
      $curcatnum = -1;
      $itemnum = -1;
      $curcat = '';
      // THIS IS THE READ POINT
      foreach ($this->items as $item) {
            $itemnum++;            
      
          // IF CURENT CATEGORY DOESNOT= CATEGORY
            
            if ($curcat != $item->category) {
                  $curcatnum++;
                  $itemnum = 0;
                     $curcat = $item->category;
                echo ('price_list['.$curcatnum.'] = new Array();'."\n");
                echo ('price_list1['.$curcatnum.'] = new Array();'."\n");
                echo ('price_list2['.$curcatnum.'] = new Array();'."\n");
                echo ('price_list3['.$curcatnum.'] = new Array();'."\n");
                echo ('price_list4['.$curcatnum.'] = new Array();'."\n");
               
            //    price_list4[0] = price_list1[$curcatnum];
            };
            echo ("      price_list[".$curcatnum."][".$itemnum."] = ".$item->cost."; ");
            echo ("      price_list1[".$curcatnum."][".$itemnum."] = ".$item->cost1."; ");
            echo ("      price_list2[".$curcatnum."][".$itemnum."] = ".$item->cost2."; ");
            echo ("      price_list3[".$curcatnum."][".$itemnum."] = ".$item->cost3."; ");

***** the below line does not work            
            echo ("      price_list4[".$curcatnum."][".$itemnum."] = ".$item->tissue."; ");
            
      //alert(".$item->userid.");

            $userval = $item->tissue;
      }
?>
}


javascriptarray
0
Comment
Question by:sydneyguy
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 166 total points
ID: 39676899
There is no obvious reason for that line to fail in what you have showen.  When you say it "does not work", what is the error message or what are the symptoms that are shown?  It would be helpful if you show the javascript text that is generated in the "View Source" from the browser.
0
 

Author Comment

by:sydneyguy
ID: 39677220
no error message, the code runs up till i put that line in, at this time you select a + button and it increments a number and then add the increment to the total, with the line out the number increments and the total is added to.
put the line in and the number increments and but no total is acted upon.
i also cannot see what is wrong, when i run the de bugger over it in php i can see the values and there there , change it to a number from the items, and it works change it back to text value and it stops. no errors are displayed
looking at the page it looks ok, great idea a must say. good thinking

below is a drop out around the array

function show_desc(iid) {
	if (iid.style.display == '') iid.style.display='none'; else iid.style.display='';
};

var price_list=new Array();
var price_list1=new Array();
var price_list2=new Array();
var price_list3=new Array();
var price_list4=new Array();



// IT ALREADY HAS THE DATA BY THIS POINT
// LOOK AT ITEM  
function initprice(){
price_list[0] = new Array();
price_list1[0] = new Array();
price_list2[0] = new Array();
price_list3[0] = new Array();
price_list4[0] = new Array();
	price_list[0][0] = 300.00; 	price_list1[0][0] = 25.00; 	price_list2[0][0] = 135.00; 	price_list3[0][0] = 30.00; 	price_list4[0][0] = skin; 	price_list[0][1] = 145.00; 	price_list1[0][1] = 30.00; 	price_list2[0][1] = 135.00; 	price_list3[0][1] = 30.00; 	price_list4[0][1] = skin; 	price_list[0][2] = 125.00; 	price_list1[0][2] = 25.00; 	price_list2[0][2] = 155.00; 	price_list3[0][2] = 35.00; 	price_list4[0][2] = skin; 	price_list[0][3] = 145.00; 	price_list1[0][3] = 30.00; 	price_list2[0][3] = 155.00; 	price_list3[0][3] = 35.00; 	price_list4[0][3] = skin; price_list[1] = new Array();
price_list1[1] = new Array();
price_list2[1] = new Array();
price_list3[1] = new Array();
price_list4[1] = new Array();
	price_list[1][0] = 125.00; 	price_list1[1][0] = 25.00; 	price_list2[1][0] = 135.00; 	price_list3[1][0] = 30.00; 	price_list4[1][0] = skin; 	price_list[1][1] = 145.00; 	price_list1[1][1] = 30.00; 	price_list2[1][1] = 135.00; 	price_list3[1][1] = 30.00; 	price_list4[1][1] = skin; 	price_list[1][2] = 125.00; 	price_list1[1][2] = 25.00; 	price_list2[1][2] = 155.00; 	price_list3[1][2] = 35.00; 	price_list4[1][2] = skin; 	price_list[1][3] = 145.00; 	price_list1[1][3] = 30.00; 	price_list2[1][3] = 155.00; 	price_list3[1][3] = 35.00; 	price_list4[1][3] = skin; price_list[2] = new Array();
price_list1[2] = new Array();
price_list2[2] = new Array();
price_list3[2] = new Array();
price_list4[2] = new Array();
	price_list[2][0] = 125.00; 	price_list1[2][0] = 25.00; 	price_list2[2][0] = 0.00; 	price_list3[2][0] = 0.00; 	price_list4[2][0] = skin; 	price_list[2][1] = 145.00; 	price_list1[2][1] = 30.00; 	price_list2[2][1] = 0.00; 	price_list3[2][1] = 0.00; 	price_list4[2][1] = skin; 	price_list[2][2] = 125.00; 	price_list1[2][2] = 25.00; 	price_list2[2][2] = 0.00; 	price_list3[2][2] = 0.00; 	price_list4[2][2] = skin; 	price_list[2][3] = 145.00; 	price_list1[2][3] = 30.00; 	price_list2[2][3] = 0.00; 	price_list3[2][3] = 0.00; 	price_list4[2][3] = skin; 	price_list[2][4] = 138.00; 	price_list1[2][4] = 28.00; 	price_list2[2][4] = 0.00; 	price_list3[2][4] = 0.00; 	price_list4[2][4] = skin; 	price_list[2][5] = 160.00; 	price_list1[2][5] = 33.00; 	price_list2[2][5] = 0.00; 	price_list3[2][5] = 0.00; 	price_list4[2][5] = skin; 	price_list[2][6] = 138.00; 	price_list1[2][6] = 28.00; 	price_list2[2][6] = 0.00; 	price_list3[2][6] = 0.00; 	price_list4[2][6] = skin; 	price_list[2][7] = 160.00; 	price_list1[2][7] = 33.00; 	price_list2[2][7] = 0.00; 	price_list3[2][7] = 0.00; 	price_list4[2][7] = skin; price_list[3] = new Array();
price_list1[3] = new Array();
price_list2[3] = new Array();
price_list3[3] = new Array();
price_list4[3] = new Array();
	price_list[3][0] = 835.00; 	price_list1[3][0] = 710.00; 	price_list2[3][0] = 626.00; 	price_list3[3][0] = 0.00; 	price_list4[3][0] =  Primary Cells; 	price_list[3][1] = 990.00; 	price_list1[3][1] = 842.00; 	price_list2[3][1] = 742.00; 	price_list3[3][1] = 0.00; 	price_list4[3][1] =  Primary Cells; 	price_list[3][2] = 1070.00; 	price_list1[3][2] = 910.00; 	price_list2[3][2] = 802.00; 	price_list3[3][2] = 0.00; 	price_list4[3][2] =  Primary Cells; 	price_list[3][3] = 1030.00; 	price_list1[3][3] = 876.00; 	price_list2[3][3] = 772.00; 	price_list3[3][3] = 0.00; 	price_list4[3][3] = Primary Cells; }

function check_event(checknum,plusmin){
	var sum_ = new Array();
	var price_treshold = 25;
	var amtx;
	var amtlft;
	var amtbought;
	var amtstore;
	var elamt;
	var j;
		var jj = 0;

Open in new window

0
 

Author Comment

by:sydneyguy
ID: 39677230
this here does not seem right especially the end
price_list4[3][3] = 466; } where is the rest

      price_list[3][0] = 835.00;       price_list1[3][0] = 710.00;       price_list2[3][0] = 626.00;       price_list3[3][0] = 0.00;       price_list4[3][0] = 466;       price_list[3][1] = 990.00;       price_list1[3][1] = 842.00;       price_list2[3][1] = 742.00;       price_list3[3][1] = 0.00;       price_list4[3][1] = 466;       price_list[3][2] = 1070.00;       price_list1[3][2] = 910.00;       price_list2[3][2] = 802.00;       price_list3[3][2] = 0.00;       price_list4[3][2] = 466;       price_list[3][3] = 1030.00;       price_list1[3][3] = 876.00;       price_list2[3][3] = 772.00;       price_list3[3][3] = 0.00;       price_list4[3][3] = 466; }
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 167 total points
ID: 39677264
If the value of $item->tissues is a string, rather than a number, then it needs wrapping in quotes. Look closely at the code below - the $item=>tissue is wrapped in single quotes.

echo ("price_list4[".$curcatnum."][".$itemnum."] = '".$item->tissue."'; ");

Open in new window

When concatenating string like this in PHP it's often easier to use printf instead of echo:

printf("price_list[%d][%d] = '%s'", $curcatnum, $itemnum, $item->tissue);

Open in new window

Have a read on the DOCs:

http://www.w3schools.com/php/func_string_printf.asp
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 167 total points
ID: 39677421
The image captioned javascriptarray doesn't look like JavaScript - it appears to be a PHP object instance of the default class (StdClass).  If I were working on this application, I would try to isolate the PHP side of the problem from the JavaScript side of the problem.  PHP runs on the server and generates the HTML document, along with the JS and CSS.  Once you know you've got the right data in the PHP variables, you can start writing it into the document that will get sent to the client.

With this many variables and two different syntaxes (PHP and JS) you may find that it pays to be very, very meticulous about the quote marks and concatenation operators.  The PHP meaning for quotes is given in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39678521
I think Chris is right.  In...

price_list4[0][0] = skin;

'skin' will be considered the name of a variable, not a 'value' because it is not quoted.  And of course, there is no such variable.
0
 

Author Closing Comment

by:sydneyguy
ID: 39679283
it is javascript with php wrapped around it

thanks for the input went through the code and came up with the line below that works fine

echo (" price_list4[".$curcatnum."][".$itemnum."] = '".$item->tissue."'; ");

thanks for all the help its now up and running thanks every one
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39679342
Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39679383
@sydneyguy

When you say you 'went through the code and came up with the following' - I think what you you actually meant to say was 'I copied Chris's code exactly and it worked!!'.

Just for future reference, here on EE, it's considered good practice to accept the answer that actually answers your question...call me old fashioned but I'm funny like that!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now