Jump to content

PHP array to JS array


Snape

Recommended Posts

Hello. Hope you can help me.

I have this code:

<?php

$test=array("5","6,","8,");

$senteces=array();

$news=array();

 

$con = sasql_connect("some;some;some;some;some");

if (!$con)

  {

  echo "sasql_connect failed\n";

  } else {

      echo "connect succed";

}

 

$sel="SELECT text, sentence FROM vijesti WHERE check_news = 1";

 

$upit = sasql_query($con,$sel);

 

while ($row=sasql_fetch_array($upit)) {

$news[]=$row['text'];

$sentences[]=$row['sentence'];

}

 

sasql_close($con);

?>

 

<?php

$i=0;                      // here i'm echoing PHP news and senteces array values. this is fine.

do {

echo $news[$i];

echo $sentences[$i]."</br>";

$i=$i+1;

}

while($i<count($vijesti));

?>

 

<html>

 

<head>

 

   

<script type="text/javascript">

 

function ispis() {

    var jsArray = ["<?php echo join("\", \"", $news); ?>"];  //this is wrong, i cant get why,??? with this code it is fine var jsArray = ["<?php echo join("\", \"", $test); ?>"];

alert(jsArray[0]);    //alerting first element...

}

 

</script>

</head>

<body>

<input type="button" value="Alert" onclick="ispis()">  //some html for testing

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/249638-php-array-to-js-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.