First of all pardon me, because my language and coding skills are not that great.
I am trying to create a coupon code website, it has a products section too. I was trying to copy data from Flipkart API and save it into SQL database. It works, but the problem is the Json array count/value is different for all products. For example, if a "gadget1" has "3" offers, it will have "3" arrays under json value "offers". if a "gadget2" has "5" offers, it will have "5" arrays under json value "offers".
I am not able to save these data to databse, because of the different count. Here what I tried and it will give you an idea about my codes.
$url = 'https://affiliate-api.flipkart.net/affiliate/1.0/product.json?id=' . $flipkartProductId;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Fk-Affiliate-Id: ID', 'Fk-Affiliate-Token: TOKEEN'));
$json = curl_exec($ch);
curl_close($ch);
$json = json_decode($json, true);
$productid = $flipkartProductId;
$title = $json['productBaseInfoV1']['title'];
$price = $json['productBaseInfoV1']['flipkartSpecialPrice']['amount'];
$cod = $json['productBaseInfoV1']['codAvailable'];
$stock = $json['productBaseInfoV1']['inStock'];
$imgurl200 = $json['productBaseInfoV1']['imageUrls']['200x200'];
$sql = "UPDATE Flipdata SET title='$title', price='$price', time='$timestamp', image1='$imgurl200', WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
} else {}
The above code is saving data to database, no issues, I am also able to count arrays using
$offerz = $json['productBaseInfoV1']['offers'];
$offersize = sizeof($offerx);
The above code does display exactly how many numbers of arrays are inside the "offers" section. It goes like the following
[offers] => Array ( [0] => No Cost EMI on Bajaj Finserv EMI Card [1] => No Cost EMI with HDFC Bank Credit Cards [2] => 10% Instant Discount* with Axis Bank Cards [3] => Extra 5% off* with Axis Bank Buzz Credit Card )
I just want to save all available offers in SQL database, and also show an php echo off the individual offers.
Only the "Create new Data/Updation" has problem. I am able to use the following code to read data from the database, and it will show nothing if there is no data.
if (!empty($row['offer1'])) {$offer1= $row["offer1"];} else {$offer1="";}
if (!empty($row['offer2'])) {$offer2= $row["offer2"];} else {$offer2= "";}
if (!empty($row['offer3'])) {$offer3= $row["offer3"];} else {$offer3= "";}
if (!empty($row['offer4'])) {$offer4= $row["offer4"];} else {$offer4= "";}
if (!empty($row['offer5'])) {$offer5= $row["offer5"];} else {$offer5= "";}
I have even tried the following, it saves data to database, but I am getting error about undefined value "offerx" in php echo.
$offerx = $json['productBaseInfoV1']['offers'];
$offersize = sizeof($offerx);
if($offersize == 0 ) {
$sql = "UPDATE Flipkart SET title='$title', price='$price', time='$timestamp', image1='$imgurl200' WHERE id='$idd'";
if ($conn->query($sql) === TRUE) {
} else {
}}
else if($offersize == 1 ) {
$offer0 = $json['productBaseInfoV1']['offers']['0'];
$sql = "UPDATE Flipkart SET title='$title', price='$price', time='$timestamp', image1='$imgurl200', offer1='$offer0' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
} else {
}}
else if($offersize == 2 ) {
$offer0 = $json['productBaseInfoV1']['offers']['0'];
$offer1 = $json['productBaseInfoV1']['offers']['1'];
$sql = "UPDATE Flipkart SET title='$title', price='$price', time='$timestamp', image1='$imgurl200', offer1='$offer0', offer2='$offer1' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
} else {... and so on...