0

I was trying to get datas from the database and put them into the array in Javascript but Javascript is not working in PHP command area.

Here is the whole PHP codes;

<?php
    mysql_connect("mysql.metropolia.fi","localhost","") or die("ERROR!!");
    mysql_select_db("localhost") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB"); 


    $sql = mysql_query("SELECT * FROM METEKSAN_HABER_CUBUGU");

    $haber = 'haber';
    $list = array();
    $i=0;

    while($rows = mysql_fetch_assoc($sql)){

        $list[] = $rows[$haber];
        $i++;
    }

    echo $i;
    echo '<script type="text/javascript">
          var yazi=new Array();';
    echo $i;

    for ($k = 0 ; $k < $i ; $k++){

        echo 'yazi['.$k.']="<a href="duyuru.php">'.$list[$k].'</a>';

    }
    echo '</script>';   
?>

But when it comes to;

echo '<script type="text/javascript">
      var yazi=new Array();';

this command line, the problem begins. Though I write 'echo $i;' after that command, I get nothing on the screen but I get the result if I write before that command. So, it means that everything works well before that command. What you think about the problem ? Why can't I starting the Javascript command ? Am I writing something wrong ? Please give me a hand.

Thanks.

UPDATE;

I opened the web source and yeah it exactly seems there is a problem. So, I think it's better to ask that how can I write

<script type="text/javascript">
                /*Example message arrays for the two demo scrollers*/

                var yazi=new Array() 
                yazi[0]='<a href="duyuru.php">METEKSAN Savunma, Yeni Dönemin Örnek Oyuncusu Olmaya Hazır</a>' 
                yazi[1]='<a href="duyuru.php">METEKSAN Savunma Bloomberg TVde</a>' 
</script>

this Javascript code in PHP ??

You can see my output at http://users.metropolia.fi/~buraku/Meteksan/index.php

Burak
  • 133
  • 2
  • 12
  • Maybe you forgot to close the `script` tag ? – webNeat Jan 17 '15 at 22:28
  • 1
    You say you get nothing on the screen; it's probably because you are viewing it as HTML and the contents of a ` – elixenide Jan 17 '15 at 22:30
  • Please can you show your current output – Phil Jan 17 '15 at 22:30
  • http://users.metropolia.fi/~buraku/Meteksan/index.php you can all see what's happening currently. I already view it on page source. It's not possible to view it as HTML if it's PHP. @Ed Cottrell – Burak Jan 17 '15 at 22:31
  • @Burak PHP generates HTML output when you use it to generate a web page. The point is that if you look at the source for the address you just posted, you will see this: `script type="text/javascript"> var yazi=new Array();2yazi[0]="METEKSAN Savunma, Yeni D�nemin �rnek Oyuncusu Olmaya Haz?ryazi[1]="METEKSAN Savunma Bloomberg TVde` That's invalid syntax, for sure --- you need to drop the `echo $i` and watch what you're doing with `'` and `"` --- but the output is there. – elixenide Jan 17 '15 at 22:35
  • Thanks a lot @EdCottrell . So can you please tell me how to do it right ? Appreciate. – Burak Jan 17 '15 at 22:46
  • @Burak Jeremy Castelli's answer will do exactly what you want. – elixenide Jan 17 '15 at 22:47
  • **@Burak By the way, if those are your real credentials, your database is about to get hosed. Change them immediately!** And on an unrelated note: Please [don't use `mysql_*`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1); the `mysql_*` functions are outdated, [deprecated](http://us3.php.net/manual/en/intro.mysql.php), and insecure. Use [`MySQLi`](http://us3.php.net/manual/en/book.mysqli.php) or [`PDO`](http://us3.php.net/manual/en/intro.pdo.php) instead. – elixenide Jan 17 '15 at 22:58

3 Answers3

2

try something like this

while($rows = mysql_fetch_assoc($sql)){
    $list[] = '<a href="duyuru.php">'.$rows[$haber].'</a>';
}
$js_array = json_encode($list);
echo "<script>var yazi = ". $js_array . ";</script>";
jeremy castelli
  • 1,232
  • 10
  • 26
  • This is the correct answer. It will avoid the `'`/`"` inconsistencies and eliminate the messy loop. – elixenide Jan 17 '15 at 22:48
  • I'm really sorry but I couldn't adapt this answer to my code and don't know where to put it. Could you help me please ? @EdCottrell – Burak Jan 17 '15 at 22:53
  • I'm really sorry but I couldn't adapt this answer to my code and don't know where to put it. Could you help me please ? @jeremycastelli – Burak Jan 17 '15 at 22:53
1

It seems you are executing it currently in your browser? Then you should find your second output when opening page source, because your browser tries to executes the output as JS code. If you execute it on cli, everything should work as expected.

EDIT based on your comment: Bullshit i wrote before, obviously. Viewing line 122 of your current html shows me a problem with your quotation marks. try the following:

for ($k = 0 ; $k < $i ; $k++){
    echo 'yazi['.$k.']=\'<a href="duyuru.php">'.$list[$k].'</a>\';';
}

In the end you should try to avoid using this kind of js rendering at all. The json_encode proposal of jeremy is the correct way to go.

apinnecke
  • 51
  • 1
  • 3
1

You may have much more compact code:

....
$list = array()
while($rows = mysql_fetch_assoc($sql)) {
    $list[] = $rows[$haber];
}

echo '<script type="text/javascript">' . "\n";

echo 'var yazi=';

echo json_encode($list,JSON_HEX_APOS | JSON_HEX_QUOT);
echo ";\n";

echo '</script>' . "\n";   

What is this doing:

  1. There's no need to count the added elements in $i, count($array) will give you the cutrrent number.. But it's not needed anyway.
  2. Put some newlines behind the echo, better readable source
  3. json_encode will format an JSON array from your php array, which can be directly used as source code.
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44