• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

javascript array not accepting text from php line

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
sydneyguy
Asked:
sydneyguy
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
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
 
sydneyguyAuthor Commented:
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
 
sydneyguyAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Chris StanyonCommented:
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
 
Ray PaseurCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
sydneyguyAuthor Commented:
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
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE, ~Ray
0
 
Chris StanyonCommented:
@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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now