Page 1 of 1

store the mysql records to javascript array variable

Posted: Wed Nov 30, 2005 11:20 pm
by jaylin
i want to store the records from mysql in the javascript. is it possible?

Posted: Wed Nov 30, 2005 11:50 pm
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

Posted: Tue Dec 06, 2005 10:13 am
by wtf
What are you trying to accomplish by storing records in js?

Posted: Tue Dec 06, 2005 10:21 am
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.

Posted: Tue Dec 06, 2005 10:31 am
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.

Posted: Tue Dec 06, 2005 11:58 am
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...

Posted: Tue Dec 06, 2005 12:05 pm
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>

Posted: Tue Dec 06, 2005 12:11 pm
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>