Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2012, 05:04 PM   PM User | #1
Cyberpops
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Cyberpops is an unknown quantity at this point
passing array from php to javascript

trying to pass several sql query array to further work in JS:

PHP Code:
<?php
$x 
mysql_query("SELECT p_max  FROM dsd_price");
$y mysql_query("SELECT p_min  FROM dsd_price");
$z mysql_query("SELECT p_price  FROM dsd_price");
while (
$x_array mysql_fetch_array($x)){$max[] = $x_array['p_max']; }
while (
$y_array mysql_fetch_array($y)){$min[] = $y_array['p_min']; }
while (
$z_array mysql_fetch_array($z)){$price[] = $z_array['p_price'];}
?>
JS:

Code:
<script type="text/javascript">
function price() {
var max = <?php echo json_encode($max); ?>;  
var min = <?php echo json_encode($min);?>;
var price = <?php echo json_encode($price);?>;
document.write(price + " " + min +" "+ max );
}
</script>
Result:
Quote:
[object Object],[object Object],[object Object],11,15,17 [object Object],[object Object],[object Object],1,6,11 [object Object],[object Object],[object Object],1,10,15
So the problem, as u can see is that it adds those [object, Object] values to my array.
Cyberpops is offline   Reply With Quote
Old 10-07-2012, 07:14 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Cyberpops View Post
trying to pass several sql query array to further work in JS:
[PHP]
So the problem, as u can see is that it adds those [object, Object] values to my array.
you are adding those in in php.
i don't think you need the 3 while statements.
you should be able to do something like :
Code:
       echo json_encode(mysqli_fetch_all(MYSQLI_NUM));
and just dump the whole result set at once. if you want the columns renamed, do it in sql using "dbName AS myName" in the SELECT.


without going into the reasons why document.write() is bad, you can view your data by using JSON.stringify() instead of coercing the objects to a string.

this is the same as php's differences between echoing a var_dump($x) -vs- just echo $x.

try something like this to get a peek:
Code:
myData= [price , min , max ]

document.write(
  '<pre>' + 
   JSON.stringiyfy(myData, null, "\t" )
)

or use firebug to inspect the variables in real-time without "printing" the data by replacing document.write() with console.log() or console.dir().

there are also several template libs out there than make it trivial to turn json into html using nice clean syntax. those can make debugging easier by offering advices in their error-handling routines.


cheers
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-07-2012, 07:49 PM   PM User | #3
Cyberpops
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Cyberpops is an unknown quantity at this point
The point is, that full JS looks like as such:

Code:
function price() {
 var frm=document.dsd_form;
 var a=frm.conPrice;
 var b=frm.OverP;
 
 var max = <?php echo json_encode($max); ?>;  
 var min = <?php echo json_encode($min);?>;
 var price = <?php echo json_encode($price);?>;

 for (counter=(min.length / 2); counter<(min.length + 1); counter++){
		
		if(a.value >= min[counter]){
			if(a.value <= max[counter]){
				b.value=min[counter]+ " " + max[counter] + " " + price[counter];
			}
			else{b.value = 0;}
		}
	}
}
so, writing a number into conPrice input field, I want OverP field to change its value to a price following a logical statement.
Cyberpops is offline   Reply With Quote
Old 10-08-2012, 01:52 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
from inputs give strings, use Number(a.value) instead of just a.value...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-08-2012, 10:39 PM   PM User | #5
Cyberpops
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Cyberpops is an unknown quantity at this point
All works now, thanx
Cyberpops is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, json, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:04 AM.


Advertisement
Log in to turn off these ads.