Jump to content

[SOLVED] PHP filling javascript array


jsoeurt

Recommended Posts

Hello,

 

I'm using nrsTable ( http://nrstable.sourceforge.net/ ) to show messages for the user that is logged on. The messages are stored in a mysql database. I've got a little problem with putting the data in the 2-dimensional arrays for nrsTable.

 

This is what I've got so far:

 

<script language="JavaScript" src="natcompare.js"></script>
  <script language="JavaScript" src="nrs_table.js"></script>




<table border=0 cellspacing=5 cellpadding=5>
<tr>
	<td>
		<table id="inbox" border=0 cellspacing=0 cellpadding=0 width="550"></table>
	</td>

</tr>
</table>

<script language="JavaScript">
var header = new Array("From", "Subject", "Writo-count");
var data = new Array(




<?php

$id=$_SESSION['id'];

mysql_connect("localhost", "XXXXXXXXXX", "XXXXXXXXX") or die(mysql_error());
mysql_select_db("xxxxxxx") or die(mysql_error());

$query  = "SELECT * FROM messages WHERE `to` = $id";

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

while($record = mysql_fetch_object($result)){
$from=$record->from;
$subject=$record->subject;
$counter=$record->counter;
$msg_id=$record->id;

echo "new Array ( \"$from\", \"$subject\", \"$counter\"),";
}


   mysql_close($conn);

?>



new Array ( "---------", "---------", "---------")

);

var links = new Array(	"alert('Row1');",
					"alert('Row2');",
					"alert('Row3');",
					"alert('Row4');",
					"alert('Row5');",
					"alert('Row6');",
					"alert('Row7');",
					"alert('Row8');",
					"alert('Row9');",
					"alert('Row10');"
				);


nrsTable.setup(
{
table_name: "inbox",
table_header: header,
table_data: data,
row_links: links,
foot_headers: true,
up_icon: "img/up.gif",
down_icon: "img/down.gif",
prev_icon: "img/left.gif",
next_icon: "img/right.gif",
rew_icon: "img/first.gif",
fwd_icon: "img/last.gif",
header_color: "#53c2f4",
footer_color: "#53c2f4",
even_cell_color: "#b9b9ac",
odd_cell_color: "#d4fbf6",
hover_color: "#ccff99",
natural_compare: true,
page_nav: true,
rows_per_page: 10,
padding: 3,
natural_compare: true,
disable_sorting: new Array(1,3)
}
);
</script>

 

It shows the user a nice list of messages available but I also want to add a value to the link-array. So if you click the first message-subject it opens up a link like this "readmessage?msg_id=$msg_id" instead of doing an alert("row1");.

 

Also I know that the way I'm filling the array right now isn't very pretty.

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/80733-solved-php-filling-javascript-array/
Share on other sites

one way would be to store the msg_id in an array the loop it for the second part.

$msg_id=$record->id;

to

$msg_id[]=$record->id;

 

Then add

echo 'var links = new Array( ';

foreach($msg_id as $id) {

echo '"<a href=\"readmessage?msg_id=' . $msg_id . '\">Read ' . $msg_id . '</a>",';

}

echo ');';

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.