Page 1 of 1
PHP coding help
Posted: Fri Jun 20, 2014 5:07 pm
by pearlbells
Hi all ,
I have to complete an assignment on following task .
On request to your script (index.php), output the contents of the database (including data from all tables) in one of several formats.
The data should be output in a meaningfully structured response that properly represents the data.
Assume this is a stand-alone API and that this data is being requested by a 3rd party external system for whatever purposes they
see fit.
Your output format will depend on the 'lang' parameter of the request:
• lang=json - JSON
• lang=xml - XML
E.g. A request to the URL index.php?lang=json should receive the output in JSON format
I have created a php coding for that. I am attaching that in a test folder as zip file.
It would be really helpful if someone review it and give me a feedback.
Thanks in advance
Pearl
Re: PHP coding help
Posted: Fri Jun 20, 2014 6:17 pm
by Celauran
You'd do better to post the code in the forums directly. Nobody is going to download some random zip file.
Re: PHP coding help
Posted: Sat Jun 21, 2014 12:35 pm
by pearlbells
I am pasting the main coding.
langmodel.php
Code: Select all
<?php
class LangModel extends Model {
public function __construct() {
parent::__construct();
}
public function getPlanets() {
$sql = "select planets.id as id,planets.name as planet,planets.diameter as diameter,
planets.mass as mass,planets.rings as rings ,planets.position as position,types.type as type,
satellites.id as satelliteId,satellites.name as satelliteName,satellites.radius as radius,
gases.formula as gases,gases.id as gasesid
from planets inner join types on planets.type_id = types.id
left join satellites on planets.id = satellites.planet_id
left join planets_gases
on planets.id = planets_gases.planet_id
left join gases
on gases.id = planets_gases.gas_id
order by planets.id,satelliteid,gasesid
";
$this->_setSql($sql);
$planets = $this->getAll();
$data = array();
foreach($planets as $planet) {
if(!isset($data[$planet['id']])) {
$data[$planet['id']] = array (
'name'=>$planet['planet'],
'diameter'=>$planet['diameter'],
'mass'=>$planet['mass'],
'rings'=>$planet['rings'],
'position'=>$planet['position'],
'type'=>$planet['type'],
'gas'=>array(),
'satellite'=>array()
);
}
if(isset($planet['satelliteId'])){
$data[$planet['id']]['satellite'][$planet['satelliteId']] = array (
'satelliteid' => $planet['satelliteId'],
'satelliteName' => $planet['satelliteName'],
'radius' => $planet['radius']
);
}
if(isset($planet['gasesid'])){
$data[$planet['id']]['gas'][$planet['gasesid']] = array (
'gasesid' => $planet['gasesid'],
'gases' => $planet['gases']
);
}
}
return $data;
}
}
?>
Contoller.php
Code: Select all
<?php
class LangController extends Controller {
public function __construct($model, $action) {
parent::__construct($model, $action);
$this->_setModel($model);
}
public function index() {
try {
$this->_view->set('title','Get JSON and XML Format');
return $this->_view->output();
}
catch(Exception $e) {
echo "Application error".$e->getMessage();
}
}
public function json() {
try {
$filename = 'json.txt';
$data = $this->_model->getPlanets();
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename='".$filename."'");
echo json_encode($data);
}
catch(Exception $e) {
echo "Application error".$e->getMessage();
}
}
public function xml() {
try {
$filename = 'test.xml';
$data = $this->_model->getPlanets();
$xml = $this->getXml($data);
header('application/rss+xml; charset=utf-8');
header("Content-Disposition: attachment; filename='".$filename."'");
echo $xml;
}
catch(Exception $e) {
echo "Application error".$e->getMessage();
}
}
private function getXml($rowset) {
$xmlDoc = new DOMDocument('1.0','UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("planets"));
$xmlDoc->formatOutput = true;
foreach($rowset as $row){
$planetTag = $root->appendChild($xmlDoc->createElement("planet"));
$planetTag->appendChild($xmlDoc->createElement("name", trim($row['name'])));
$planetTag->appendChild($xmlDoc->createElement("diameter", trim($row['diameter'])));
$planetTag->appendChild($xmlDoc->createElement("mass", trim($row['mass'])));
$planetTag->appendChild($xmlDoc->createElement("rings", trim($row['rings'])));
$planetTag->appendChild($xmlDoc->createElement("type", trim($row['type'])));
$planetTag->appendChild($xmlDoc->createElement("position", trim($row['position'])));
if(!empty($row['satellite'])) {
$satellitesTag = $planetTag->appendChild($xmlDoc->createElement("satellites"));
foreach($row['satellite'] as $satelliteParam) {
$satelliteTag = $satellitesTag->appendChild($xmlDoc->createElement("satellite"));
$satelliteTag->appendChild($xmlDoc->createElement("satelliteName", trim($satelliteParam['satelliteName'])));
$satelliteTag->appendChild($xmlDoc->createElement("radius", trim($satelliteParam['radius'])));
}
}
if(!empty($row['gas'])) {
$gasesTag = $planetTag->appendChild($xmlDoc->createElement("gases"));
foreach($row['gas'] as $gasParam) {
$gasesTag->appendChild($xmlDoc->createElement("gas", trim($gasParam['gases'])));
}
}
}
return $xmlDoc->saveXML();
}
}
?>
Please check and give me a feedback
Pearl
Re: PHP coding help
Posted: Sat Jun 21, 2014 12:47 pm
by pearlbells
Hi all ,
I am looking to get an advise on how to approach a technical test.
1) How to organize the coding
2) what are main files to use.
3) how to apply OOP concept .etc
Please advise.
Pearl
Re: PHP coding help
Posted: Sat Jun 21, 2014 1:07 pm
by Celauran
I'm not sure I see the connection between LangModel and getPlanets. Is that really the best name for your model? LangModel's constructor seems a little redundant since all it does is call the parent constructor. LangController's constructor may be the same (does its parent constructor not call _setModel?). Writing JSON to a text file download seems a little strange, but I'm not sure what your use case is. Why not output JSON directly with the appropriate header?