|
|
Data formatting by using database data and stringsWe will be sending data back to the main page by using Json formatted strings. These strings will contain number of Jason data value pairs taken from database table along with some other data.
We will create data value pairs and post them back to main script. Here is the way we generate data formats for main script to use.
We have not discussed the mysql data collection part here, only the data formatting is shown. See the while loop collecting the data from the table. Here the data coming from mysql table is formatted and after the while loop some common data like messages, last record etc are posted here.
$str= "{ \"data\" : [";
while($nt=mysql_fetch_array($t)){
$str=$str."{\"id\" : \"$nt[id]\", \"name\" : \"$nt[name]\", \"myclass\" : \"$nt[class]\", \"mark\" : \"$nt[mark]\"},";
//$str=$str."{\"myclass\" : \"$nt[class]\"},";
}
$str=substr($str,0,(strLen($str)-1));
if(($endrecord) < $nume ){$end="yes";}
else{$end="no";}
if(($endrecord) > $limit ){$startrecord="yes";}
else{$startrecord="no";}
$str=$str."],\"value\" : [{\"endrecord\" : $endrecord,\"limit\" : $limit,\"end\" : \"$end\",\"startrecord\" : \"$startrecord\",\"message\" : \"$message\"}]}";
To access the data at our receiving file we have to use JavaScript array and here is some sample code to receive the data.
var myObject = eval('(' + httpxml.responseText + ')');
var msg=myObject.value[0].message
To see the example on this code is used, you can read our Ajax based PHP MySQL Paging script
|
|
| |
|
|
|
|
|