It is my first time working with an external API. I have already fixed that it gives me the right output. In a controller I have the following code:
public function index() {
$api = new Wefact();
$parameters = [
];
$api_response = $api->sendRequest('product', 'list', $parameters);
print_r($api_response);
}
This shows me the array on the page. But I do not know how to use this array to get it into a foreach in the blade. The output of the print_r is as follow:
Array
(
[controller] => product
[action] => list
[status] => success
[date] => 2022-05-05T04:20:03+02:00
[totalresults] => 2
[currentresults] => 2
[offset] => 0
[products] => Array
(
[0] => Array
(
[Identifier] => 1
[ProductCode] => P0001
[ProductName] => SIM ONLY 5GB
[ProductKeyPhrase] => SIM ONLY 5GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 25
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 03:49:57
)
[1] => Array
(
[Identifier] => 2
[ProductCode] => P0002
[ProductName] => SIM ONLY 10GB
[ProductKeyPhrase] => SIM ONLY 10GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 35
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 04:03:47
)
)
)
As you can see there are two products. I want to have these in a datatable with an foreach.
Since it is my first time, I really do not know how to do this.
Anyone that can help me out?
If you want to access the array of products it would be like this
return view('your_view', [
'products' => $api_response['products']
]);
Related
Hi i don't know what is wrong with the threaded of cakephp 2 please help
when i use threaded without condition it will show its children
$category = $this->Category->find('threaded');
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 113
[name] => Core samples
[parent_id] => 11
)
)
)
but when i use this with condition it won't show its parent
$this->Category->find('threaded',array('conditions'=>array('Category.id'=>11)));
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
)
)
)
please help why it is not showing the children when use with condition
I think the problem is because you are asking for all categories with an id of 11. Insted you should ask for all categories that are children of categorie 11.
You could use your left and right columns to get a nested array (keyword Tree behaviour). Try this:
$parentCategorie = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.id' => 11
)
);
$children = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.lft BETWEEN ? AND ?' => array($parentCategorie['Categorie']['lft'], $parentCategorie['Categorie'])['rght']
)
);
Probably the best chance you have to achive what you want is to use the children method of the Tree behavior.
You coul use it as follows
$parent=$this->Category->find('first',
array('conditions'=>array('Category.id'=>11)));
$this->Category->children($parent['Category']['id']);
I have a problem with the Laravel 5 Validation, I post data with the Angular method $http.post. I have allready created a Request file called, CreateProjectRequest.
The posted data looks like this.
Array(
[0] => Array
(
[project_id] =>
[title] => test
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => 'test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
[1] => Array
(
[project_id] =>
[title] => test2
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
)
I need to validate title and tasks, how can I reach that?
I tried a few things but the I get each time a error. For example title is required while title was filled in.
// Empty rules array
$rules = [];
foreach ($this->request->all() as $keyGroup => $request)
{
$rules['title'] = 'required';
// Loop throug tasks
foreach ($request['tasks'] as $key => $task)
{
$rules['quantity'] = 'required|numeric';
}
}
return $rules;
Presently m working with CakePhp..i got some problem with combining two array and prepare for a json encoded array.. i used array_merge() property but its not working.. how can i encoded both of these two array..
M doing like this :
return json_encode(array_merge ($product_list,$price_list));
i have two php array as follows :
array 1:
Array
(
[0] => Array
(
[PriceList] => Array
(
[price_id] => 2
[price_name] => abc
[date_time] => 2015-07-06 16:22:56
[dealer_type] => Dealer
[purpose] => dealer
[status] => ACTIVE
)
)
[1] => Array
(
[PriceList] => Array
(
[price_id] => 3
[price_name] => xyz
[date_time] => 2015-07-06 16:22:56
[dealer_type] => Dealer
[purpose] => dealer
[status] => ACTIVE
)
)
)
array 2:
Array
(
[0] => Array
(
[Product] => Array
(
[cat_id] => 1
[subcat_id] => 3
[brand_id] => 1
[p_code] => PP12567
[name] => akai
[model_no] =>
[specification] => color tv
[color] =>
[quality] =>
[size] =>
[p_unavail] => 1
[demo_avail] => 0
[brochure] =>
[status] => active
)
[ProductPrice] => Array
(
[id] => 154
[p_code] => PP12567
[price_id] => 1
[quantity] => 233
[purchase_price] => 344.00
[selling_price] => 44.00
[discount_price] => 33.00
[tax] => 5.00
[datetime] => 2015-07-23 15:47:11
)
[ProductSubCategory] => Array
(
[subcat_id] => 3
[cat_id] => 1
[subcat_name] => samsung
[status] => active
)
[ProductCategory] => Array
(
[cat_id] => 1
[cat_name] => Electronics
[cat_type] => Product
[status] => active
)
)
[1] => Array
(
[Product] => Array
(
[cat_id] => 1
[subcat_id] => 4
[brand_id] => 1
[p_code] => PBC-676767
[name] => music
[model_no] => 33
[specification] =>
[color] =>
[quality] =>
[size] =>
[p_unavail] => 0
[demo_avail] => 0
[brochure] =>
[status] => active
)
[ProductPrice] => Array
(
[id] => 156
[p_code] => PBC-676767
[price_id] => 1
[quantity] => 767
[purchase_price] => 54.00
[selling_price] => 55.00
[discount_price] => 22.00
[tax] => 3.00
[datetime] => 2015-07-23 15:47:11
)
[ProductSubCategory] => Array
(
[subcat_id] => 4
[cat_id] => 1
[subcat_name] => sony
[status] => active
)
[ProductCategory] => Array
(
[cat_id] => 1
[cat_name] => Electronics
[cat_type] => Product
[status] => active
)
)
)
how to combine these two array into one and encoded into a json array..
You have to use array_merge but not directly on array1 and array2:
$toEncodeArray = [array_merge ($array1[0], $array2[0])] ;
If you only want the associative array (without the wrapping array), simply do:
$toEncodeArray = array_merge ($array1[0], $array2[0]) ;
How would i save the following;
I want the first 4 items to apply to all the "products to be saved", given that this data is at the top of my form and "global" to the products that get saved how would i tell cakephp to save it to each product or do i have to create a foreach loop and manually insert the data?
Global data to each product.
[discount_id] => 17
[range_id] => 21
[category_id] => 6
[user_id] => 104
Array
(
[Product] => Array
(
[discount_id] => 17
[range_id] => 21
[category_id] => 6
[user_id] => 104
[0] => Array
(
[product_code] => ffff
[colour] => red
[lead_time_weeks] =>
[description] =>
[height] => 11111
[width] => 22222
[depth] =>
[price] =>
[discount] => 50
[discounted_price] =>
[quantity] =>
)
[1] => Array
(
[product_code] => fgfgfggf
[colour] => red
[lead_time_weeks] =>
[description] =>
[height] => 123
[width] => 123
[depth] =>
[price] =>
[discount] => 50
[discounted_price] =>
[quantity] =>
)
)
)
Save method in controller
$this->Product->saveAll($this->request->data['Product']
I would personally use a foreach loop, saveAll is meant for saving associated data.
e.g.
foreach($this->request->data['Product'] as $product){
// Don't want to add the generic items
if(is_array($product)){
$newProduct = $this->Product->create();
$newProduct['Product'] = $product; // To save added them seperately
// Then add the generic items into the array
$newProduct['Product']['discount_id'] = $this->request->data['Product']['discount_id'];
etc...
$this->Product->save($newProduct);
}
}
For the life of me I can't figure out what's going wrong. When I debug the value of the $order->save operation, I get a 1 (which I assume to mean true). Here's what I'm doing:
$order = $this->Order->find('first', array(
'conditions' => array('Order.token' => urldecode($token))
));
debug($order);
$this->Order->id = $order['Order']['id'];
$orderData = array('Order' => array(
'id' => $order['Order']['id'],
'billing_email' => urldecode($payPalResponse['EMAIL']),
'billing_name' => urldecode($payPalResponse['PAYMENTREQUEST_0_SHIPTONAME']),
'billing_address' => urldecode($payPalResponse['PAYMENTREQUEST_0_SHIPTOSTREET']),
'billing_city' => urldecode($payPalResponse['PAYMENTREQUEST_0_SHIPTOCITY']),
'billing_state' => $payPalResponse['PAYMENTREQUEST_0_SHIPTOSTATE'],
'billing_zipcode' => $payPalResponse['PAYMENTREQUEST_0_SHIPTOZIP']
));
debug($orderData);
$this->Order->save($orderData);
And here's what I'm getting:
controllers/markets_controller.php (line 149)
Array
(
[Order] => Array
(
[id] => 13
[token] => **************
[player_id] => 1
[status_id] => 1
[timestamp] => 2012-02-15 12:09:24
[date_filled] => February 15, 2012
)
[OrderItem] => Array
(
)
)
controllers/markets_controller.php (line 161)
Array
(
[Order] => Array
(
[id] => 13
[billing_email] => *********#************.com
[billing_name] => Test User
[billing_address] => 1 Main St
[billing_city] => San Jose
[billing_state] => CA
[billing_zipcode] => 95131
)
)
Why is it telling me that it's saving but it's not?
Okay, I had tried to clear the cache before, but for some reason it didn't take. I finally cleared the cache file for the Order model, and that fixed it.