dongxian6285 2019-02-19 04:59
浏览 137
已采纳

将数据保存到SQL并根据json数组计数显示它们

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...

  • 写回答

2条回答 默认 最新

  • drll42469 2019-02-19 06:06
    关注

    You can do the following, no need to check the count of the offers coming from the api.

    $offerx = $json['productBaseInfoV1']['offers'];
    
    $offer0 = empty($offerx[0]) ? '' : $offerx[0];
    $offer1 = empty($offerx[1]) ? '' : $offerx[1];
    $offer2 = empty($offerx[2]) ? '' : $offerx[2];
    $offer3 = empty($offerx[3]) ? '' : $offerx[3];
    $offer4 = empty($offerx[4]) ? '' : $offerx[4]; 
    
    $sql = "UPDATE Flipkart SET title='$title', price='$price', time='$timestamp', image1='$imgurl200', offer1='$offer0', offer2='$offer1', offer3='$offer2', offer4='$offer3', offer5='$offer4' WHERE id='$id'";
    if ($conn->query($sql) === TRUE) {
    
    } else {
    
    }
    

    Just define the 5 variables for store offers and assign the values to it if available. Set null value otherwise.

    Very Important: Please fix SQL injection in your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度