store the mysql records to javascript array variable

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

store the mysql records to javascript array variable

Post by jaylin »

i want to store the records from mysql in the javascript. is it possible?
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

yes it is possible but not recommended for large number of records because of slow performance on page.
FYI: I already tried that. look at my post at viewtopic.php?t=34928
try AJAX
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

What are you trying to accomplish by storing records in js?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

wtf wrote:What are you trying to accomplish by storing records in js?
He probably wants people to haxxor his database. :lol:

If you want your JS communicating with the server, search the forum for AJAX.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

He/she could simply be wanting to build a little array for a dynamic select box or something. I do that kind of thing sometimes when form options change as selections are made.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

//RECIEVE A PHP ARRAY RETURN THE STRING THAT CAN 
//BE PUT INTO AJAVASCRIPT ARRAY
//RETURN FALSE IF ARRAY HAS NO VALUES
//IF $RETURN_ARRAY_KEY IS TRUE RETURN THE ARRAY KEY 
function phpArrayToJavaScriptArray($a_,$return_array_key){

	$r='';
	$cntr=0;
	

	
	if(is_array($a_)){
		
		
	
		if($return_array_key)
			$a_ = array_flip($a_);
	
		if(count($a_)==0)
			return FALSE;
	
		
	              
		foreach ($a_ as $v) {	
		
		$cntr++;	
			$r .= '"'.$v.'"';
	
			if($cntr < count($a_))//last number no ,
				$r.=', ';		
		
		}
	}
	return $r;

}
Edit: This is something I wrote a long time ago... It could use some TLC but it should do the job...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

to expand on that:

Code: Select all

<?php
function phpArrayToJavaScriptArray($a_,$return_array_key){ 

    $r=''; 
    $cntr=0; 
     

     
    if(is_array($a_)){ 
         
         
     
        if($return_array_key) 
            $a_ = array_flip($a_); 
     
        if(count($a_)==0) 
            return FALSE; 
     
         
                   
        foreach ($a_ as $v) {     
         
        $cntr++;     
            $r .= '"'.$v.'"'; 
     
            if($cntr < count($a_))//last number no , 
                $r.=', ';         
         
        } 
    } 
    return $r; 

} 
$jsarray = phpArrayToJavaScriptArray(array("hi","bye"),false);
?>
<script>
var someArr = new Array(<?=$jsarray;?>);
for(var i=0;i<someArr.length; i++)
	alert(someArr[i]);
</script>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Woot Woot Thanks Burrito...
Burrito wrote:to expand on that:

Code: Select all

<?php
function phpArrayToJavaScriptArray($a_,$return_array_key){ 

    $r=''; 
    $cntr=0; 
     

     
    if(is_array($a_)){ 
         
         
     
        if($return_array_key) 
            $a_ = array_flip($a_); 
     
        if(count($a_)==0) 
            return FALSE; 
     
         
                   
        foreach ($a_ as $v) {     
         
        $cntr++;     
            $r .= '"'.$v.'"'; 
     
            if($cntr < count($a_))//last number no , 
                $r.=', ';         
         
        } 
    } 
    return $r; 

} 
$jsarray = phpArrayToJavaScriptArray(array("hi","bye"),false);
?>
<script>
var someArr = new Array(<?=$jsarray;?>);
for(var i=0;i<someArr.length; i++)
	alert(someArr[i]);
</script>
Post Reply