Viewed   67 times

Please provide help on how to assign php array values to javascript array

----------- Using below php code I am storing data in php array -----------

<?php
$str_query="SELECT title,description FROM tablename ORDER BY title";
$rs_sch=GetRecordset($str_query); 

$int_count=0;
$schd_arr = array();
while(!$rs_sch->EOF())
{
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");
$rs_sch->MoveNext();
$int_count++;
}
?> 

----- Using below javascript code I am trying to store php array data into javascript array -----

Please let me know what and how to write variable at below 2 mentioned locations so my code can work.

<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
{
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;
}
</script>

Thank you in advance, KRA

 Answers

1

You can't loop like that, you need to loop the PHP array and push into javascript array:

<script type="text/javascript" language="javascript">
    var pausecontent = new Array();
    <?php foreach($schd_arr as $key => $val){ ?>
        pausecontent.push('<?php echo $val; ?>');
    <?php } ?>
</script>
Saturday, August 27, 2022
5

There you go, complete solution:

<?php

$myarray = array(45,3,56,7,21);

//create a copy and sort
$myarray_copy = $myarray;
sort($myarray_copy);

//reverses key and values
$myarray_copy = array_flip($myarray_copy);

//create result by using keys from sorted values + 1
foreach($myarray as $val)
    $myarray2[] = $myarray_copy[$val]+1;

//print final array
print_r($myarray2);

/*
Output:

Array ( [0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3 )
*/
?>
Monday, December 26, 2022
 
ammar26
 
5

The line:

var dataToSend = {'name' : val, 'ans[]' : <?php echo $answers; ?> } ;

will print:

var dataToSend = {'name' : val, 'ans[]' : Array } ;

which creates a javascript syntax semantic error (ans = empty string will be posted). Change to:

var dataToSend = {'name' : val, 'ans[]' : <?php echo json_encode($answers); ?> } ;

which prints:

var dataToSend = {'name' : val, 'ans[]' : [13,5,6,11] } ;
Tuesday, November 8, 2022
 
2

If you don't want to use javascript, you can handle it via php. Take a look at this lib: http://code.google.com/p/php-mobile-detect/. And then you could do something like:

<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

if ($detect->isMobile()) {
    header('Location: yourpage.php');
    exit(0);
}
Friday, October 21, 2022
1

Cookies are not the way to transfer variables between client and server. you should append key/variables pairs to your request URL using either a get (querystring) or post method.

jQuery ajax example;

$.get('http://www.myphpserver.com/script.php?row_id=' + NewCookieValue);
Monday, October 31, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :