note 116775 deleted from function.json-encode by cmb
| From: | cmb@php.net | Date: | Mon, 21 Nov 2016 19:44:57 +0000 |
| Subject: | note 116775 deleted from function.json-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207314@lists.php.net to get a copy of this message | ||
Note Submitter: bibilkattathara15 at gmail dot com
----
The json_encode() does'nt gave you correct json I have written a code that can give a valid
json
//DatabaseConnectionClass.php
abstract class DatabaseConnectionClass {
protected $link;
protected function __construct()
{
$this->link=mysqli_connect('localhost','root','','realestate')
or die('Unable to connect to database');
}
}
//JSON.php
include 'DatabaseConnectionClass.php';
class JSON extends DatabaseConnectionClass
{
private $recordCount;
function __construct()
{
// Used for Connecting to Database
parent::__construct();
$this->recordCount=0;
}
private function jsonFileWriter($buffer_string,$mode)
{
// This function is to write data onto a file.
$jsonFile=fopen("data.json",$mode);
fwrite($jsonFile, $buffer_string);
fclose($jsonFile);
}
public function generateJSON()
{
/* Used to generate data for Angular JS JSON File*/
/*Refer ExportJson.class in phpMyAdmin*/
$res=$this->link->query('SELECT
id,district,taluk,category,type,description,amount
FROM tbl_info WHERE 1');
while ($row=$res->fetch_assoc())
{
$this->recordCount=$this->recordCount+1;
if($this->recordCount==1)
{
$buffer='[';
$this->jsonFileWriter($buffer,'w');
}
else
{
$buffer = ', ';
$this->jsonFileWriter($buffer,'a');
}
$json=json_encode($row, JSON_PRETTY_PRINT, 512);
$this->jsonFileWriter($json,'a');
}
$buffer = '] ';
$this->jsonFileWriter($buffer,'a');
}
}