0% found this document useful (0 votes)
10 views

Data Processor - PHP

This PHP code handles GET requests for movie or pokemon data. It checks parameters, retrieves and decodes the data, then sorts it in ascending or descending order before returning the results. Functions are used to retrieve, sort, and return the data.

Uploaded by

gauravpatel7777g
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Processor - PHP

This PHP code handles GET requests for movie or pokemon data. It checks parameters, retrieves and decodes the data, then sorts it in ascending or descending order before returning the results. Functions are used to retrieve, sort, and return the data.

Uploaded by

gauravpatel7777g
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php
/**
* @author Gaurav Patel
* @version 202335.00
* @package COMP 10260 Assignment 2
*
* I Gaurav Patel, 000898120, certify that this material is my original work. No
other person's work has been used without suitable acknowledgment and I have not
made my work available to anyone else.
*/

/*
The bellow condition check that is that GET is taking choice and sort and if not it
throw the error
*/

if (isset($_GET['choice']) && isset($_GET['sort'])) {

$movieChoice = $_GET['choice']; // it store the choice value inside the


moviesChoice
$storeOutput = $_GET['sort']; // it store the sort value inside the
StoreOutput

// it check that movieChoice has the pokemon or movies and if not than it send
the error

if ($movieChoice == "pokemon") {
$outputResult = pokemonData();
} elseif ($movieChoice == "movies") {
$outputResult = moviesData();
} else {
$errRes = array("error" => "Invalid request");
echo json_encode($errRes);
exit;
}

// This condition check that the order for the short is assinding for a and
descinding is d and if there is not a any of two it will throw error

if ($storeOutput == "a") {
$outputResult = orderASC($outputResult);
} elseif ($storeOutput == "d") {
$outputResult = orderDSC($outputResult);
} else {
$errRes = array("error" => "Invalid sort");
echo json_encode($errRes);
exit;
}

echo json_encode($outputResult);
} else {
$errRes = array("error" => "Missing param");
echo json_encode($errRes);
}

/**
* Method pokemonData
*
* @return void The given function read the pokemon file and than then it transfer
the data inside the array using the loop
*/
function pokemonData()
{
$outputResult = file("pokemon.txt"); // it stores the pokemon file data inside
the outputResult

$convertData = array(); // it intiliazie the array

// inside the for loop it itterate the loop as long as outputResult and then it
store the name inside name variable and it store the image inside the images
variable then it will store the name and image as the key value pair.

for ($g = 0; $g < count($outputResult); $g += 2) {


$name = $outputResult[$g];
$images = $outputResult[$g + 1];
$convertData[] = array("name" => $name, "image" => $images);
}
return $convertData;
}

//

/**
* Method moviesData
*
*The bellow function is extract the data form the movies.json and then it decode
that file and return the file.

* @return void this function dont return anything


*/
function moviesData()
{
$convertDataMovie = file_get_contents("movies.json");
$outputResultMovies = json_decode($convertDataMovie, true);
return $outputResultMovies;
}

/**
* Method orderASC
*
* the bellow function short the data in the acsending order and and then return
the outputResult. I use the short method which I learn during the
*
* .net assignment and i just use that one
*
* @param $outputResult it takes the outputResult to to take the data
* @return void this function not return nothing
*/
function orderASC($outputResult)
{
$number = count($outputResult); // it store the outputResult value in number
for ($g = 0; $g < $number - 1; $g++) {
for ($k = $g + 1; $k < $number; $k++) {
$name1 = strtolower($outputResult[$g]['name']); // it store the name in
lowercase
$name2 = strtolower($outputResult[$k]['name']); // it store in the name
which has the next index than name1
// the condition check that name1 is grater than 2 it will swwap the
name and put the name in ascending order

if ($name1 > $name2) {


$temp = $outputResult[$g];
$outputResult[$g] = $outputResult[$k];
$outputResult[$k] = $temp;
}
}
}
return $outputResult;
}

//
/**
* Method orderDSC
*
* This bellow function put all the names in the discending order. and all the
things work same as the above function.
*
* @param $outputResult
*
* @return void this function return nothing
*/
function orderDSC($outputResult)
{
$number = count($outputResult);
for ($g = 0; $g < $number - 1; $g++) {
for ($k = $g + 1; $k < $number; $k++) {
$name1 = strtolower($outputResult[$g]['name']);
$name2 = strtolower($outputResult[$k]['name']);

if ($name1 < $name2) {


$temp = $outputResult[$g];
$outputResult[$g] = $outputResult[$k];
$outputResult[$k] = $temp;
}
}
}
return $outputResult;
}
?>

You might also like