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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.