Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code migrated from using direct database calls to using the rest-api. #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 41 additions & 67 deletions db.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,38 @@


try{



// Database details


$h = "agnelvishal2.cm6dgizwvuku.us-east-2.rds.amazonaws.com";
$u = "public";
$p = "onlySelectAccess";
$db = mysqli_connect($h, $u, $p,"agnelvishal");
if (mysqli_connect_errno($db)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$limit=100;
//$output=$_POST["output"];

$fromDate=$_POST["fromDate"];
$toDate=$_POST["toDate"];
$fromDate=mysqli_real_escape_string($db,$fromDate);
$toDate=mysqli_real_escape_string($db,$toDate);

// Declaring Base URL for API EndPoint
$url = "http://localhost:8000/";

// Determining final end point and concatenating data as needed
if (!empty($fromDate) and !empty($toDate)) {
$url .= "feeds/between?startDate=$fromDate&endDate=$toDate";
} else {
$url .= "feeds";
}

// Opening a curl request
$apiRequest = curl_init();

//query generation for date
$whereDateClause=" where";
$whereDateClause.="( item_date between\"";
$whereDateClause.=$fromDate;
$whereDateClause.="\" AND \"";
$whereDateClause.=$toDate;
$whereDateClause.="\" )";
// Setting Request URL to the opened curl request
curl_setopt($apiRequest, CURLOPT_URL, $url);

// Telling curl to return results to var instead of printing to the screen
curl_setopt($apiRequest, CURLOPT_RETURNTRANSFER, 1);

// Executing and storing returns from the curl request
$result = curl_exec($apiRequest);

$item_select = "SELECT item_content,item_title,item_date, item_url,total,image,likes,shares,pa FROM `FEED`".$whereDateClause." ORDER BY total desc limit ".$limit;
// Closing curl request
curl_close($apiRequest);

//echo $item_select;
$result = mysqli_query($db, $item_select);
if (!$result) die('Av -- Could not get data: --' . mysqli_error($db));
// Converting JSON Return to a array Object
$result = json_decode($result, true);

include "chartsScript.php";
?>
Expand All @@ -69,43 +61,40 @@
$urlExists=array();
$iurlExists=0;

while ($rows = mysqli_fetch_assoc($result)) {
if (in_array($rows["item_url"], $urlExists))
foreach ($result['data'] as $index => $item) {
if (in_array($item["item_url"], $urlExists))
{
continue;
}
else
{
$adate = date_create($rows["item_date"]);
$adate = date_create($item["item_date"]);
date_sub($adate, date_interval_create_from_date_string('1 month'));


$urlExists[$iurlExists]=$rows["item_url"];
$urlExists[$iurlExists]=$item["item_url"];
$iurlExists++;
$table .= '{';
$table .= 'x:Date.UTC(';
$table .= date_format($adate, 'Y,m,d').'),';
$table .= 'y:';
$table .= $rows["total"].',';
$table .= $item["total"].',';
$table .= 'z:';
$table .= $rows["total"].',';
$table .= $item["total"].',';
$table .= 'heading:';
$table .= '\'';
$table .= addcslashes($rows["item_title"], "'");
$table .= addcslashes($item["item_title"], "'");
$table .= '\''.',';

$table .= 'url:';
$table .= '\'';
$table .= $rows["item_url"];
$table .= $item["item_url"];
$table .= '\'';
$table.='}';
$table.=',';

}
}
echo $table;


?>

// Highcharts code termination
Expand All @@ -124,61 +113,46 @@

$table = '<div class="row">';

mysqli_data_seek($result, 0);

$urlExists=array();
$iurlExists=0;
while ($rows = mysqli_fetch_assoc($result)) {
foreach ($result['data'] as $index => $item) {

if (in_array($rows["item_url"], $urlExists))
if (in_array($item["item_url"], $urlExists))
{
continue;

}
else
{

$urlExists[$iurlExists]=$rows["item_url"];
$urlExists[$iurlExists]=$item["item_url"];
$iurlExists++;
$table .= '<div class="column"><div class="card">';

$table .='<div class="center-image" style="background-image: url('.$rows["image"].');" style="width:320px"></div>';
$table .= '<h2 class="block-with-text">'.$rows["item_title"].'</h2>';
$table .='<div class="center-image" style="background-image: url('.$item["image"].');" style="width:320px"></div>';
$table .= '<h2 class="block-with-text">'.$item["item_title"].'</h2>';
$table .='<div class="container">';
$table .= '<span class="date">'.$rows["item_date"].'</span>';
$table .= '<span class="date">'.$item["item_date"].'</span>';
$table .= '<div class="meta"><div class="meta-item"><p class="label">Total Popularity:</p><p>';
$table .= $rows["total"];
$table .= $item["total"];
$table .= '</p></div><div class="meta-item"><p class="label">Search Engine Popularity:</p><p>';
$table .= $rows["pa"];
$table .= $item["pa"];
$table .= '</p></div><div class="meta-item"><p class="label">Facebook Shares</p><p>';
$table .= $rows["shares"];
$table .= $item["shares"];
$table .= '</p></div><div class="meta-item"><p class="label">Facebook Likes:</p><p>';
$table .= $rows["likes"];
$table .= $item["likes"];
$table .= '</p></div></div><p class="description">';
$table .= $rows["item_content"];
$table .= $item["item_content"];
$table .= '</p>';

$table .= '<a target="_blank" href="'.$rows["item_url"].'" >Read more</a>';
$table .= '<a target="_blank" href="'.$item["item_url"].'" >Read more</a>';
$table.='</div></div></div>' ;
}
}
$table.='</div>';

echo $table;


//




mysqli_free_result($result);
mysqli_close($db);





} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Expand Down