Jump to content

json data in array


Go to solution Solved by jcbones,

Recommended Posts

Hi
 
I have a json data :
{
    "email": "john@john.fr",
    "line_items": [
        {
            "sku": "123456789",
			"price": "0.67",
			"price_with_tax": "4.00",
            "tax_lines": [
                {
                    "title": "tax010",
                    "rate": 0.01,
                    "price": "1.11"
                },
                {
                    "title": "tax00200",
                    "rate": 0.02,
                    "price": "2.22"
                }
            ]
        },
        {
            "sku": "012345666",
			"price": "1.67",
			"price_with_tax": "5.00",
            "tax_lines": [
                {
                    "title": "tax0003000",
                    "rate": 0.03,
                    "price": "3.33"
                }
            ]
        }
    ]
}
 
and I try  put this data in mySql. First data have a good value :happy-04:  but second have a wrong data on 'price_tax1' and 'price_tax2' in mySql :  :
Here is my MySql 
 
1st item :::: good value
email:john@john.fr
sku:123456789
price:0.67
price_with_tax:4.00
price_tax1:1.11
price_tax2:2.22
 
 
2nd item :::: 
email:john@john.fr
sku:012345666
price:1.67
price_with_tax:5.00
 
wrong value ::::
price_tax1:1.11
price_tax2:2.22
 
 
here is my code :
$jsondata = file_get_contents($jsondata);

$dataDecode = json_decode($jsondata);

$email = $dataDecode->email;



									 

try
{
	$dtbs = new PDO($dsn_dev, $pdo_user, $pdo_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
     die('Error : ' . $e->getMessage());
}
																									


		try 
		{
			foreach ($dataDecode->line_items as $obj) 
			{
			
				$var_sku = $obj->sku;
				
				$var_price = $obj->price;
				
				$var_price_with_tax = $obj->price_with_tax;
			
								
								$taxNewArray = array();
									foreach ($dataDecode->line_items[0]->tax_lines as $obj2)
										{
											array_push($taxNewArray , $obj2);
											
										}
									
									$val1st = array_shift($taxNewArray);
									$val2nd  = array_pop  ($taxNewArray);
								
			$var_tax1 = $val1st->price;
			$var_tax2 = $val2nd->price;
			

																												
																												
					
			$stmt = $dtbs->prepare("INSERT INTO $tabledata ($mysql_email,
																	$mysql_sku,
																	$mysq_price,
																	$mysql_price_with_tax,
																	$mysql_price__tax1___line_items,
																	$mysql_price__tax2___line_items
																		 )
														
								VALUES (:email,
										:sku,
										:price,
										:price_with_tax,
										:price_tax1,
										:price_tax2)");
										
			$stmt->execute(array(':email'=>$email,
								':sku'=>$var_sku,
								':price'=>$var_price,
								':price_with_tax'=>$var_price_with_tax,
								':price_tax1'=>$var_tax1,
								':price_tax2'=>$var_tax2
								));
								
								
								
																																					
			
			}
			
		}
		catch(Exception $e) 
		{
			throw $e;
		}
	

 
 
 

How can I put goog value of 2nd data in mySql ?

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/296022-json-data-in-array/
Share on other sites

Not sure what you are expecting for your "good" value.  However, I think the code may be confusing you a bit.  I've took the liberty to clean it up a might, with some notations.  Hope it helps.

 

try
{
$dtbs = new PDO($dsn_dev, $pdo_user, $pdo_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
     die('Error : ' . $e->getMessage());
}
 
try  {
$stmt = $dtbs->prepare("INSERT INTO $tabledata ($mysql_email,
$mysql_sku,
$mysq_price,
$mysql_price_with_tax,
$mysql_price__tax1___line_items,
$mysql_price__tax2___line_items
)
VALUES 
(:email,
:sku,
:price,
:price_with_tax,
:price_tax1,
:price_tax2)");
 
foreach ($dataDecode->line_items as $obj) { 
foreach ($dataDecode->line_items[0]->tax_lines as $obj2) {
$stmt->execute(array(':email'=>$email,
':sku'=>$obj->sku;,
':price'=> $obj->price,
':price_with_tax'=>$obj->price_with_tax,
':price_tax1'=>$obj2->title, //is this suppose to be rate?
':price_tax2'=>$obj2->price
)); 
} 
}
}
catch(Exception $e)  {
throw $e;
}
 
Link to comment
https://forums.phpfreaks.com/topic/296022-json-data-in-array/#findComment-1510552
Share on other sites

One closer look, I believe this is what you are looking for.

 

try
{
$dtbs = new PDO($dsn_dev, $pdo_user, $pdo_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
     die('Error : ' . $e->getMessage());
}
 
try  {
$stmt = $dtbs->prepare("INSERT INTO $tabledata ($mysql_email,
$mysql_sku,
$mysq_price,
$mysql_price_with_tax,
$mysql_price__tax1___line_items,
$mysql_price__tax2___line_items
)
VALUES 
(:email,
:sku,
:price,
:price_with_tax,
:price_tax1,
:price_tax2)");
 
foreach ($dataDecode->line_items as $obj) { 
$prices = (array)$dataDecode->line_items[0]->tax_lines;
$price1 = !empty($prices[0]) ? $prices[0]->price : 0.00;
$price2 = !empty($prices[1]) ? $prices[1]->price : 0.00; 
$stmt->execute(array(':email'=>$email,
':sku'=>$obj->sku;,
':price'=> $obj->price,
':price_with_tax'=>$obj->price_with_tax,
':price_tax1'=>$price1,
':price_tax2'=>$price2
)); 
 
}
}
catch(Exception $e)  {
throw $e;
}
Link to comment
https://forums.phpfreaks.com/topic/296022-json-data-in-array/#findComment-1510554
Share on other sites

 

Not sure what you are expecting for your "good" value.  However, I think the code may be confusing you a bit.  I've took the liberty to clean it up a might, with some notations.  

foreach ($dataDecode->line_items as $obj) { 
foreach ($dataDecode->line_items[0]->tax_lines as $obj2) {
$stmt->execute(array(':email'=>$email,
':sku'=>$obj->sku;,
':price'=> $obj->price,
':price_with_tax'=>$obj->price_with_tax,
':price_tax1'=>$obj2->title, //is this suppose to be rate?
':price_tax2'=>$obj2->price
)); 
} 
}
}
catch(Exception $e)  {
throw $e;
}
 

Hi Jcbones,

 

Non, I have a wrong valus with this code! Here is a result :
 
1st item 
email:john@john.fr
sku:123456789
price:0.67
price_with_tax:4.00
price_tax1:tax010     wrong!  [this is a good value : 1.11)
price_tax2:2.22
 
 
2nd item :::: 
email:john@john.fr
sku:012345666
price:1.67
price_with_tax:5.00
 
wrong value ::::
price_tax1:tax00200        wrong!  [this is a good value : 3.33]
price_tax: 2:2.22            wrong!  [this is a good value : 0.00  or NULL]
Link to comment
https://forums.phpfreaks.com/topic/296022-json-data-in-array/#findComment-1510591
Share on other sites

 

One closer look, I believe this is what you are looking for.

 

 
foreach ($dataDecode->line_items as $obj) { 
$prices = (array)$dataDecode->line_items[0]->tax_lines;
$price1 = !empty($prices[0]) ? $prices[0]->price : 0.00;
$price2 = !empty($prices[1]) ? $prices[1]->price : 0.00; 
$stmt->execute(array(':email'=>$email,
':sku'=>$obj->sku;,
':price'=> $obj->price,
':price_with_tax'=>$obj->price_with_tax,
':price_tax1'=>$price1,
':price_tax2'=>$price2
)); 
 
}
}
catch(Exception $e)  {
throw $e;
}

,

 

 
Hi jcbones,
have also wrong valus with this code! Here is a result :
 
1st item :::: good value
email:john@john.fr
sku:123456789
price:0.67
price_with_tax:4.00
price_tax1:1.11
price_tax2:2.22
 
 
2nd item :::: 
email:john@john.fr
sku:012345666
price:1.67
price_with_tax:5.00
 
wrong value ::::
price_tax1:1.11            wrong!  [this is a good value : 3.33]
price_tax2:2.22            wrong!  [this is a good value : 0.00  or NULL]
 
 
 
With 2nd Items we have only one tax in json data : 
 
look like that :
for 1st item :
"tax_lines": [
                {
                    "title": "tax010",
                    "rate": 0.01,
                    "price": "1.11"
                },
                {
                    "title": "tax00200",
                    "rate": 0.02,
                    "price": "2.22"
                }
            ]

and for second item (only one tax)

"tax_lines": [
                {
                    "title": "tax0003000",
                    "rate": 0.03,
                    "price": "3.33"
                }
            ]

 
How I must write my code so I get the following result :
first item value:
...
price_tax1:1.11
price_tax2:2.22
 
second item value (when there are only one tax)
...
price_tax1:3.33
price_tax2:0.00 
 
or
second item value (when there are only one tax)
...
price_tax1:3.33
price_tax2:NULL 
 
 
Link to comment
https://forums.phpfreaks.com/topic/296022-json-data-in-array/#findComment-1510592
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.