0

I want to pass mysql_fetch_array() 's result array to javascript function.

Here is a code.

 <?php
         $msDetails=  mysql_query("select * from article_master where status='complete'") or die(mysql_error());
         while ($row= mysql_fetch_array($msDetails)) {

         $co_auth_name=mysql_query("select fname from author_detail where journal_id='$jid' && article_id='$row[1]' && (correspondng_author != 'on' || correspondng_author = 'false')");
         $num=mysql_affected_rows();
         echo $num; //2


                while ($co_auth=mysql_fetch_array($co_auth_name))
                {
                         $JArray= json_encode($co_auth);;
                         echo $JArray;
                }

 ?>

 <tr>
        <td><a href="javascript:popup_viewReports('<?php echo $JArray ?>');">View</a> 
       </td>
 </tr>

 <?php
         }
 ?>

How can i pass $co_Auth to viewReports()??

Nirali Joshi
  • 1,968
  • 6
  • 30
  • 50
  • possible duplicate of [Pass a PHP array to a JavaScript function](http://stackoverflow.com/questions/4885737/pass-a-php-array-to-a-javascript-function) – UltraInstinct Apr 30 '13 at 10:26
  • Also, use something like `$JArray[] = $co_auth;`, and then use json_encode after the loop. – UltraInstinct Apr 30 '13 at 10:28

5 Answers5

1

Try this. It looks like you can perform your script with one query instead of how you've down it. I've assumed that the author_detail table's column "article_id" references the author_master table's PK "id".

<?php
// Assuming author_master's PK is "id" and author_detail's column author_id refernces it
$query = "SELECT ad.* 
    FROM article_master am
    JOIN author_detail ad ON am.id = ad.article_id 
    WHERE am.status = 'complete'
    AND ad.journal_id = '$jid'
    AND (ad.corresponding_author <> 'on' || ad.corresponding_author == 'false')";

$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)): ?>
    <tr>
        <td><a href="javascript:popup_viewReports('<?php echo $row['fname'] ?>');">View</a></td>
    </tr>
<?php endwhile; ?>
antony
  • 2,763
  • 19
  • 23
0

You're sending it as a string. Delete comas and should work.

<a href="javascript:popup_viewReports(<?php echo $JArray ?>);">

Kasyx
  • 3,170
  • 21
  • 32
0

You're assigning $JArray in a loop, and then outputting it outside the loop. Only the last value will be outputted.

Edson Medina
  • 9,862
  • 3
  • 40
  • 51
0

Okay, well first of all - whilst it's not an 'error' as such, you should look into MySQLi or PDO as all of the mysql_ functions are now deprecated - but that's just an aside.

What you're doing there is turning each row into it's own JSON-encoded array - do this instead:

while ($row = mysql_fetch_assoc($co_auth_name))
{
    $JArray[] = $row;
}
$JArray = json_encode($JArray);

And then you can just echo out the results as you are doing. That's assuming the function you're using to parse the JSON is correct of course.

Hope that helps a little.

Lukey
  • 922
  • 1
  • 7
  • 9
0
  1. As @Edson Medina said only last element will be visible
  2. You should place $JArray initalization outside of foreach look, most likely at begining of script, and assign it with i.e. null or empty string
Marek Lewandowski
  • 3,291
  • 1
  • 20
  • 26