I've stumbled upon some problems while creating a shopping application. In short:
I'm using a php $_SESSION['cart'] to register all the products inside the cart
The products are registered and added to the cart by the following code:
else {// It's new: Add it to the array
$_SESSION['cart']['cartItems'][] = $cartProduct;
$_SESSION['cart']['totalItems']++;
}
To that function a cart with 2 or more products looks like this:
{"cartItems":[{
"id":"3",
"name":"Bedrukte Jurk",
"price":25.99,
"size":"L",
"quantity":"12",
"total_product_price":311.88
},
{
"id":"11",
"name":"Product voorraad id",
"price":144,
"size":"M",
"quantity":"23",
"total_product_price":3312
}
],
"totalPrice":3623.88,
"totalItems":2
}
So now i've got two questions regarding my $_SESSION['cart'];
How would i be able to delete a specific product. I was thinking about using the product id. And for now that has been working for me unless the unset($_SESSION['cart']['cartItems'] ... id part that i still haven't figured out. So how would i be able to tell php to delete the row with the requested id.
The second question is a bit more confusing to me since this has to do with writing the "cart" to the database on "payOrder" click.
So i've been struggling with this part and i've tried using a foreach() statement to get each row. But sadly only the first product is written in the database. The function handeling this foreach() is shown down below:
$cart_encode = json_encode($_SESSION['cart']['cartItems']);
$cartDecode = json_decode($cart_encode);
$date = date("Y-m-d h:i:s");
// Create the cart Using XML
$opt = $webService->get(array(
'resource' => 'carts',
));
// Define the $resource
$resource = $opt->children()->children();
// Adding the product items
foreach ($cartDecode as $key => $cartItem) {
$resource->associations->cart_rows->cart_row->id_product = $cartItem->{"id"};
$resource->associations->cart_rows->cart_row->id_product_attribute = 1;
$resource->associations->cart_rows->cart_row->id_address_delivery = 1;
$resource->associations->cart_rows->cart_row->quantity = $cartItem->{"quantity"};
}
Update after the comments
So as #Randall has mentioned it's better to use the product_id's as a new array index layer for deleting the row's in the cart, in this case a particulair product.
So i've edited my code to an older state in wich this is true and Question one is solved.
Anyhow the new JSON structure is:
{"cartItems": {
"5": {
"id":"5",
"name":"Bedrukte Zomerjurk",
"price":30.5,
"size":"L",
"quantity":"34",
"total_product_price":1037
},
"4": {
"id":"4",
"name":"Bedrukte Avond Jurk",
"price":50.99,
"size":"L",
"quantity":"3",
"total_product_price":152.97
}
},
"totalPrice":1189.97,
"totalItems":2
}
Still Question 2 sadly stays unanswered about the database input. I hope there are some people that could help me out.
PS: #Randall thanks for the "it makes no difference tip, it solved question 1 of the 2!".
I hope my question is clear and if it isn't please let me know.
As always,
Thanks in advance!
So I am using an API (SendInBlue - a transactional email service) and I am trying to use the API to display a list of the users on my webpage.
Below, SendInBlue has given me sample code to use, however when I put them together I just get a blank screen.
I know this is a total beginner question... but how do I put these 2 pieces of code together so that it actually displays the list of contacts on my website?
Thank you so much!!
EXAMPLE
require('../mailin.php');
$mailin = new Mailin("https://api.sendinblue.com/v2.0","your access key");
$data = array( "listids" => array(1,2),
"timestamp" =>"2015-05-22 14:30:00",
"page" => 1,
"page_limit" => 2
);
var_dump($mailin->display_list_users($data));
SAMPLE OUTPUT
{
"code":"success",
"message":"Retrieved details of all users for the given lists",
"data":{
"data":[
{
"blacklisted":0,
"email":"email1#domain.com",
"id":1,
"listid":[1],
"blacklisted_sms":1,
"last_modified" : "2015-05-22 15:30:00"
},
{
"blacklisted":1,
"email":"email2#domain.com",
"id":2,
"listid":[1,2],
"blacklisted_sms":0 ,
"last_modified" : "2015-05-25 19:10:30"
}
],
"page":1,
"page_limit":2,
"total_list_records":100
}
}
Ok I figured this out. I turned every attribute into a variable like so...
$subscriberemail = $getemails['data']['data'][$number]['email'];
This took ages to figure out!
I followed this tutorial to set up my back end server based on Yii framework and then this tutorial to set up my API and everything is working as it should.
But I am not sure how to accomplish the next step:
My return array right now is pulling records that look like this:
{
"id": 1,
"user_id": 1,
"description": "This is a summary of article 1",
"status": 2,
"type": 1,
"created_at": 1426210780,
"updated_at": 1426365319
}
The value for 'type' is '1' in the db, but I want to store all the possible values of 'type' in another table like this:
1 : Red
2 : Blue
3 : Green
And then I want the JSON returned via my API to contain "type":'red' instead of "type":1. I assume I need to override something in my Model, but I can't figure out what to override or with what.
I'm happy to read through tutorials or documentation but I'm such a beginner at this that I'm not sure what terms to search for. Thanks for your help!
Have a look at models and their relationships to other models, this will allow you to get the information you need.
http://www.yiiframework.com/doc/guide/1.1/en/database.arr
Once the relationship is working correctly you should be able to get the colour from the original model.
Although this is from a earlier version of Yii it may help you understand how the models will interact as well
http://www.yiiframework.com/wiki/285/accessing-data-in-a-join-table-with-the-related-models/
#Burrito's response provided documentation but I want to give the full solution for other searchers:
First, I needed to set up a model for 'Type'.
Second, I needed to declare the relationship between 'Report' (my main model) and 'Type' like this (in my Report model):
public function getType()
{
return $this->hasOne(Type::className(), ['id' => 'type']);
}
(I'm not sure if that step is necessary, but the documentation makes it seem necessary.)
Third, I created getTypeName (in Report model), to get the name of the type based on the ID:
public function getTypeName($type = null)
{
return Type::findOne($type)->name;
}
Lastly, in my apiController, I modified the function that I am using to get all records to include a loop for each record that called getTypeName:
protected function findAllReports()
{
// get all reports
$reports = Report::find()
->asArray()
->all();
if( $reports ){
$i = 0;
// loop through each report
foreach($reports as $report){
$model = new Report();
// add a new key/value pair to each report array, populate with getTypeName and pass the type ID to it as a parameter
$reports[$i]['typeName'] = $model->getTypeName($reports[$i]['type']);
$i++;
}
return $reports;
} else {
// error or no results
}
}
For reference, the other routine needed here is the action that the API hits, which calls findAllReports():
public function actionList()
{
$reports=$this->findAllReports();
$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>$reports),JSON_PRETTY_PRINT);
}
Finally, now if I called [url]/api/list, I get an array of reports, including the typeName.
I'll get right to the point.
I'm trying to save data retrieved from an API using the following:
$i=0;
foreach($results->results as $product){
$products[$i]['pid'] = $product->listing_id;
$products[$i]['title'] = $product->title;
$products[$i]['url'] = $product->url;
$products[$i]['price'] = $product->price;
$products[$i]['quantity'] = $product->quantity;
$products[$i]['endtime'] = $product->ending_tsz;
$products[$i]['thumb'] = $product->results.Images[url_75x75];
Here is the JSON request:
{
"count":163,
"results":[
{
"listing_id":118973432,
"state":"active",
"user_id":27549667,
"category_id":68894752,
"title":"Funny I Love You Valentines Card - I Heart you Even More then a Nerd Loves Starwars - Adult Funny Humor Greeting Cards",
"description":"You know how much Nerds be loving the Star Wars , this Valentines day show the one you love that you love them even more then that - The perfect greeting card for that special someone!\r\n\r\nGUARANTEED CHRISTMAS DELIVERY - Place your orders by 12\/19 and get it by Christmas Eve - Upgraded Two Shipping available starting at $5, just contact us to upgrade\r\n\r\nDetails:\r\n\r\n- Outside Message: I love you even more then a nerd loves Starwars\r\n- Inside Message: Blank Inside\r\n- Size A7 (5x7)\r\n- Matching White Envelope included \r\n- Packaged in a cello sleeve for protection in transit\r\n\r\nWe will gladly ship to your recipient! Just specify a ship to address and any special message in "notes to seller" at checkout. Need a Custom Card, Magnet or keychain, we do that too!\r\n\r\nShipping:\r\nUS S&H is $2-Ships within 1 business day. Please allow 2-5 business days for delivery.\r\nIntl S&H is $4 - Ships within 1 business day - Please allow 7-10 business days for delivery.\r\n\r\nBuy MORE and SAVE on shipping! Express shipment is available to most US cities contact us for price and details.\r\n\r\nFor More Rude, crude and downright funny greeting cards, novelty gifts and prints, visit our Etsy store and browse a large selection single cards, greeting card sets, prints and more totally edgy, racy, adult, crass, lude, perverted and all things nasty!\r\nimjustsayininc.etsy.com",
"creation_tsz":1422565585,
"ending_tsz":1432929985,
"original_creation_tsz":1356746393,
"last_modified_tsz":1422565585,
"price":"4.00",
"currency_code":"USD",
"quantity":3,
"tags":[
"funny card",
"greeting card",
"novelty card",
"mature card",
"adult card",
"cards",
"funny cards",
"relationship card",
"love card",
"i love you card",
"valentines cards",
"funny valentine",
"valentines day card"
],
"category_path":[
"Paper Goods",
"Cards",
"Valentine"
],
"category_path_ids":[
69150367,
69152963,
68894752
],
"materials":[
"Papaer",
"Ink",
"Envelope"
],
"shop_section_id":12656719,
"featured_rank":null,
"state_tsz":1421921498,
"url":"https:\/\/www.etsy.com\/listing\/118973432\/funny-i-love-you-valentines-card-i-heart?utm_source=massetsy&utm_medium=api&utm_campaign=api",
"views":764,
"num_favorers":102,
"shipping_template_id":null,
"processing_min":1,
"processing_max":1,
"who_made":"i_did",
"is_supply":"false",
"when_made":"2010_2015",
"is_private":false,
"recipient":"unisex_adults",
"occasion":"valentines",
"style":null,
"non_taxable":false,
"is_customizable":true,
"is_digital":false,
"file_data":"",
"language":"en-US",
"has_variations":false,
"used_manufacturer":false,
"Images":[
{
"listing_image_id":410447372,
"hex_code":"C1AFB1",
"red":193,
"green":175,
"blue":177,
"hue":353,
"saturation":9,
"brightness":75,
"is_black_and_white":false,
"creation_tsz":1356746394,
"listing_id":118973432,
"rank":1,
"url_75x75":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_75x75.410447372_sjo4.jpg",
"url_170x135":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_170x135.410447372_sjo4.jpg",
"url_570xN":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_570xN.410447372_sjo4.jpg",
"url_fullxfull":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_fullxfull.410447372_sjo4.jpg",
"full_height":737,
"full_width":600
},
{
"listing_image_id":402927400,
"hex_code":"897761",
"red":137,
"green":119,
"blue":97,
"hue":33,
"saturation":29,
"brightness":53,
"is_black_and_white":false,
"creation_tsz":1356746394,
"listing_id":118973432,
"rank":2,
"url_75x75":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_75x75.402927400_e80x.jpg",
"url_170x135":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_170x135.402927400_e80x.jpg",
"url_570xN":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_570xN.402927400_e80x.jpg",
"url_fullxfull":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_fullxfull.402927400_e80x.jpg",
"full_height":800,
"full_width":986
},
{
"listing_image_id":402923983,
"hex_code":"C8C1C0",
"red":200,
"green":193,
"blue":192,
"hue":8,
"saturation":4,
"brightness":78,
"is_black_and_white":false,
"creation_tsz":1356746394,
"listing_id":118973432,
"rank":3,
"url_75x75":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_75x75.402923983_snri.jpg",
"url_170x135":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_170x135.402923983_snri.jpg",
"url_570xN":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_570xN.402923983_snri.jpg",
"url_fullxfull":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_fullxfull.402923983_snri.jpg",
"full_height":440,
"full_width":640
}
]
}
],
"params":{
"limit":"1",
"offset":0,
"page":null,
"shop_id":"imjustsayininc",
"keywords":null,
"sort_on":"created",
"sort_order":"down",
"min_price":null,
"max_price":null,
"color":null,
"color_accuracy":0,
"tags":null,
"category":null,
"translate_keywords":"false",
"include_private":0
},
"type":"Listing",
"pagination":{
"effective_limit":1,
"effective_offset":0,
"next_offset":1,
"effective_page":1,
"next_page":2
}
}
What I'm trying to do is pull all values from the image field where it shows an image URL (url_75x75,url_170x135,url_570xN & url_fullxfull).
I seem to be stuck only on the importing of the images. They seems to be in an array and I can't figure out how to implement pulling them like I do the rest of the content.
I've spent roughly 3 days searching, trying and failing at doing this.
I was successful when I had the Json request split up into separate requests but recently learned I could include images into one JSON request which is the method I am currently trying.
How can I do this?
I'm pretty new to PHP and I'm even newer at JSON so please be descriptive as possible and try not to overload me with complex ways I could do this, thank you.
results is an array of objects, so is Images
I didn't test it but I think something like this should be able to retrieve the first image of the first result:
$product->results[0]->Images[0]->url_75x75
In your code, this is obviously wrong:
$product->results.Images[url_75x75]
Javascript with PHP syntax mixed together? ;-)
This is the current code that I have in order to retrieve the sizes for a particular product, however when an item has multiple drop-downs such as this one does for color. You must first select the color before retrieving the sizes for that color. I will need to retrieve both sets of data; colors and sizes.
It seems if I am not mistaken that this site uses the JSON structure.
I am a VERY novice learner and would like guidance on what to do next in order to retrieve the data using php...
Thanks in advance! :)
<?php
$doc = new DOMDocument();
#$doc ->loadHTMLFile('http://www.drjays.com/shop/P1543932/');
$xpath = new DOMXPath($doc);
$size = $xpath->query('//*[contains(#class, \'product_dropdown\')]');
foreach ($size as $sizes)
{
echo $sizes->nodeValue .PHP_EOL;
}
?>
update: I was able to use Google Chrome (developer tools) to retrieve this information. I believe this is confirmation that it is in fact JSON.
"description" : "The Lace Inset Tie Front Woven Top by Apple Bottoms features:\r\n<br>\r\n<ul>\r\n<li> US sizing</li>\r\n<li> Pieced lace accents on shoulders</li>\r\n<li> Metal stud trim</li>\r\n<li> Two chest pockets</li>\r\n<li> Button-down closure</li>\r\n<li> Sleeveless cut</li>\r\n<li> Waist tie on front</li>\r\n\r\n<br>\r\n<b> Model is wearing size 1X</b>\r\n</ul>",
"sizes" : [
{
"id" : "1533783",
"display" : "1X"
},
{
"id" : "
1533785",
"display" : "2X"
}
]
i don't think that is the url for the actual JSON object, with the google chrome developer tools can you find the object url?