Reputation: 79
I'm pulling deals from Groupon's api, and I have no clue how to take the data and put it into a database with php, i know how to show it on in html, but not in the database, I need to pull it into the database so I have more control over the info, if anybody knows how to do this or knows a better way, i'm all eye's, lol, thanks
<script type='text/javascript'>
$(function () {
$.getJSON("https://api.groupon.com/v2/deals.json?callback=?",
{
client_id: "b252ad3634a4ab2985b79d230ccc4e49a3ea9d19",
show: "all",
division_id: "los-angeles"
})
.done(function (data) {
console.log(data);
// do whatever processing you need to do to the data
// right here, then drop it in the div
$.each(data.deals, function (i, v) {
$title = $("<h2/>", {
html: v.title,
class: "heading"
});
$img = $("<img/>", {
src: v.mediumImageUrl
});
$deal = $("<div/>", {
html: v.highlightsHtml + v.pitchHtml
});
$("#main").append($deal);
$deal.prepend($title, $img);
});
});
});
</script>
Upvotes: 1
Views: 10005
Reputation: 2272
Theory
Well I'm just gonna start running through the process...
First, know which driver you are dealing with and research how PHP interacts with them. Look at this list and start reading... http://www.php.net/manual/en/refs.database.php
Sending the data to a PHP script to handle the rest, depends on how you got the data. Here are some basic flows...
After you can get a connection to your database you just need to save it to a table using a SQL query (most likely using INSERT or UPDATE). With JSON data, I prefer to save it to a column that has the data type of TEXT. The only real risk here is that you have to be sure that you can validate the data. ESPECIALLY IF THE DATA IS BEING GIVEN TO PHP FROM A JAVASCRIPT /AJAX SOURCE!
Pulling the data from that point is just using a "SELECT" sql statement. PHP's database modules will pull this data and put it into a lovely array for you, making manipulation easy.
Examples
Now thats the theory, heres some actions. I'm going to be choosing the 1st flow idea. Now this one will just save it to the database. I'm not doing any fancy checking or really pulling. But this will show you the idea of how ajaxing and saving to php would work.
view-deals.html
<script type='text/javascript'>
$(function () {
$.getJSON("https://api.groupon.com/v2/deals.json?callback=?",
{
client_id: "b252ad3634a4ab2985b79d230ccc4e49a3ea9d19",
show: "all",
division_id: "los-angeles"
}).done(function (data) {
console.log(data);
// do whatever processing you need to do to the data
$.post('save-deals.php',{dealData: data}, function(finishData) {
//This is an optional function for when it has finished saving
});
// Your formatting comes next
....
});
</script>
Now that will send all the data that you got (intact) from groupon to a seperate php script using an AJAX Post call. I use post, well, because that's what it's for.
save-deals.php
ob_start(); //I like output-buffering because if we need to change headers mid script nothing dies
$DealData = isset( $_POST['dealData'] )?$_POST['dealData']:die("Malformed form data!");
if($DealData!='') {
$DB = new mysqli("example.com", "user", "password", "database");
if ($DB->connect_errno) {
echo "Failed to connect to MySQL: " . $DB->connect_error;
}
$DealData = $DB->real_escape_string($DealData); //Sanitize it for MySQL
if (!$DB->query("INSERT INTO deals(data) VALUES ($DealData)") {
echo "Insert failed: (" . $DB->errno . ") " . $DB->error;
} else {
//AT THIS POINT IT SHOULD HAVE BEEN INSERTED!
//You could return a success string, or whatever you want now.
}
} else {
http_response_code("400");
die("Bad Request, please check your entry and try again");
}
ob_end_flush(); //Close up the output-buffer
Some important things to note about that script is that the ob_* functions are completely optional. The way DealData is set is a VERY shorthand way of checking that the post data contains that value, and setting it properly; if not, then to give an error.
This next script is to show you how to pull the data from the database now, and manipulate it if you want. It will also return the data as JSON information so it can be used with a javascript $.getJSON()
call. This is mostly a snippet for reference
manipulate-deals.php
//First connect to the database!
$DB = new mysqli("example.com", "user", "password", "database");
if ($DB->connect_errno) die("Failed to connect to MySQL: " . $DB->connect_error);
//Get ALL the entries!
if(!$results = $DB->query("SELECT * FROM data")) die("Failed to retrieve data! ".$DB->error);
//Decode the datas!
$returnResults = [];
while($entry = $results->fetch_assoc()) {
$JSON = json_decode($entry['data']);
//Manipulate however you wish!
$JSON->whatever->tags[1]->you->want = "whatever value";
//Add it to the results!
$returnResults[] = $JSON;
}
echo json_encode($returnResults);
That very last section is just for fun. It would export a json string containing an array of results. And each of that array's entries would be a valid object just as groupon had given you. Hope that helps!
Upvotes: 2