KRA
KRA

Reputation: 381

How to assign PHP array values to JavaScript array

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

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>

Upvotes: 19

Views: 72280

Answers (6)

Farid
Farid

Reputation: 168

You can do this without a php loop. you can use php "json_encode" and parse it:

<script type="text/javascript">
    var pausecontent = [];
    pausecontent = '<?php echo json_encode($schd_arr);?>';
    pausecontent = JSON.parse(pausecontent);
</script>

Upvotes: 0

Ravi Kumar Yadav
Ravi Kumar Yadav

Reputation: 339

I had the same problem doing this but finally found the best solution

    <script>       
    var arr=[];
    arr.push('<?php
             include('conn.php');
        $query = "SELECT *FROM c group by name";
        $res = mysqli_query($conn,$query) or die(mysqli_error($conn));

        while($data = mysqli_fetch_array($res))
        {


             echo $data["name"];
        }
             ?>');
        </script>

Upvotes: 1

Balaji Courier
Balaji Courier

Reputation: 1

Above both answers in good.

  1. Iterate through the array you got as a result of your DB query and add each value to a new PHP variable,
  2. Add a counter and increment it during each iteration to make sure that you’re not adding a comma after the last element,
  3. If the count of the elements in the array is more than 1, create the array as usual, otherwise create a new array with 1 element and assign your PHP array value to the index 0.

Upvotes: 0

Hkachhia
Hkachhia

Reputation: 4539

You can directly use the json_encode function. It is easy to use and produces valid javascript so is very stable:

<script type="text/javascript">
    var pausecontent = <?php echo json_encode($php_array); ?>;
</script>

Upvotes: 66

11684
11684

Reputation: 7507

I think you'd best get the array to your javascript first. That would be something like this:

var theVariableYouWantTheArrayIn = <?php echo json_encode($theArrayYouWantInJavascript); ?>

After that it is a normal js array, so you can use properties like .length which will presumably make the loop much easier.

Upvotes: 3

Mihai Iorga
Mihai Iorga

Reputation: 39704

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>

Upvotes: 52

Related Questions