Skip to content

Instantly share code, notes, and snippets.

@lithid
Created February 1, 2013 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lithid/4692445 to your computer and use it in GitHub Desktop.
Save lithid/4692445 to your computer and use it in GitHub Desktop.
<?php
$response = array();
require_once('db_connect.php');
$db = new DB_CONNECT();
# There are 6 variables tha can be used with this api
# id page count all order search
#
# order can be used with only all or with page/count
# order=latest or order=earliest default order is always earliest first.
# cmwallpapers-api.php?id=1, Print the id number 1 and do not set any other GET variables.
if (
isset($_GET["id"]) &&
!isset($_GET["page"]) &&
!isset($_GET["count"]) &&
!isset($_GET["all"]) &&
!isset($_GET["order"]) &&
!isset($_GET["search"]) &&
!isset($_GET["featured"])
)
{
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM wallpapers WHERE id = $id") or die(mysql_error());
}
# cmwallpapers-api.php?page=1&count=6, Print page 1 and limit the page items to 6 per page.
elseif (
!isset($_GET["id"]) &&
isset($_GET["page"]) &&
isset($_GET["count"]) &&
!isset($_GET["all"]) &&
!isset($_GET["search"]) &&
!isset($_GET["featured"])
)
{
$page = $_GET['page'];
$count = $_GET['count'];
$number = ( ($page*$count) - $count );
print_r($number, $count);
if (isset($_GET["order"]))
{
$order = $_GET["order"];
if ($order == "latest")
{
$result = mysql_query("SELECT * FROM wallpapers ORDER BY id DESC LIMIT $number, $count")
or die(mysql_error());
}
else
{
$result = mysql_query("SELECT * FROM wallpapers LIMIT $number, $count") or die(mysql_error());
}
}
else
{
$result = mysql_query("SELECT * FROM wallpapers LIMIT $number, $count") or die(mysql_error());
}
}
# cmwallpapers-api.php?all, Simply print all the wallpapers.
elseif (
!isset($_GET["id"]) &&
!isset($_GET["page"]) &&
!isset($_GET["count"]) &&
isset($_GET["all"]) &&
!isset($_GET["search"]) &&
!isset($_GET["featured"])
)
{
if (isset($_GET["order"]))
{
$order = $_GET["order"];
if ($order == "latest")
{
$result = mysql_query("SELECT * FROM wallpapers ORDER BY id DESC") or die(mysql_error());
sendJsonData($result);
}
else
{
$result = mysql_query("SELECT * FROM wallpapers") or die(mysql_error());
}
}
else
{
$result = mysql_query("SELECT * FROM wallpapers") or die(mysql_error());
}
}
# cmwallpapers-api.php?search=color, Search for wallpapers with color anywhere in title or description.
elseif (
!isset($_GET["id"]) &&
!isset($_GET["page"]) &&
!isset($_GET["count"]) &&
!isset($_GET["all"]) &&
!isset($_GET["order"]) &&
isset($_GET["search"]) &&
!isset($_GET["featured"])
)
{
$search = $_GET['search'];
$result = mysql_query("
SELECT * FROM wallpapers WHERE
name LIKE '%$search%' OR
author LIKE '%$search%' OR
description LIKE '%$search%'") or die(mysql_error());
}
# cmwallpapers-api.php?search=color, Search for wallpapers with color anywhere in title or description.
elseif (
!isset($_GET["id"]) &&
!isset($_GET["page"]) &&
!isset($_GET["count"]) &&
!isset($_GET["all"]) &&
!isset($_GET["order"]) &&
!isset($_GET["search"]) &&
isset($_GET["featured"])
)
{
$result = mysql_query("SELECT * FROM featured") or die(mysql_error());
}
else
{
$response["success"] = 0;
$response["message"] = "api isset error";
}
function sendJsonData($result) {
if (!empty($result)) {
$response["wallpapers"] = array();
$var = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$wallpaper = array();
$val_array = array("id", "name", "author", "description", "preview", "size_large", "size_normal",
"size_small", "rating", "created", "value", "featured");
foreach ($val_array as $x) {
if ($row["$x"] != null) {
$wallpaper["$x"] = $row["$x"];
}
}
array_push($response["wallpapers"], $wallpaper);
}
$response["items"] = $var;
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Mysql array is empty";
echo json_encode($response);
}
exit;
}
sendJsonData($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment