How to retrieve an array in URL? - php

Trying to get data following a shopping cart purchase on the success page but having issues with $_GET. The URL is:
http://domain.com/success.php?customer[email]=email%40gmail.com&order[button][description]=music+download&order[button][id]=89765464465423184847654556&order[button][name]=music&order[button][repeat]=&order[button][resource_path]=%2Fv2%2Fcheckouts%2F9db9d0ef-9cfd-52b9-b2d7-792683d2431d&order[button][subscription]=false
How can I parse the data from this in PHP?

If you print_r $_GET variable, then it will produce output:
Array
(
[customer] => Array
(
[email] => email#gmail.com
)
[order] => Array
(
[button] => Array
(
[description] => music download
[id] => 89765464465423184847654556
[name] => music
[repeat] =>
[resource_path] => /v2/checkouts/9db9d0ef-9cfd-52b9-b2d7-792683d2431d
[subscription] => false
)
)
)
That means you can access your data via $_GET['customer']['email'] and $_GET['order']['button']['description'] etc..

In your example, you are using what is called a query string. In order to retrieve the information from the query string, the $_GET super global exists and you can use it as follows:
$customer_email = $_GET['customer']['email'];
$order_button_description = $_GET['order']['button']['description'];
$order_button_id = $GET['order']['button']['id'];
// etc.
Let me know if that helps.

Related

PHP array (yaml) - add or remove sections of config file

I have some software that uses a yaml config file and I want to dynamically add a list of registered users to the config file when they join and remove them when their accounts are deleted.
The config file is as follows:
targets:
users:
- name: AlFredo01
uid: a1fa36h2a5hbd6535c919402ba8cc837bd75abfae
userinfo:
- email: Alfredo01#gmail.com
- gender: male
- country: it
- name: AlFredo02
uid: jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
userinfo:
- email: Alfredo02#gmail.com
- gender: male
- country: gb
I effectively would like to be able to add in new names and remove by name or UID using a PHP script on register/archive.
All the info needed would be available on registration. On deleting, their UID/Name is stored in the DB exactly how it will be added to the config file.
Can this be achieved, please?
I've never used yaml files before. I've found php-yaml and I can parse the data in to an array and print or dump it.
<?
$parsed = yaml_parse_file('config2.yml');
var_dump($parsed);
?>
The above does dump the data as you would expect. Like below
Array
(
[targets] =>
[users] => Array
(
[0] => Array
(
[name] => AlFredo01
[uid] => a1fa36h2a5hbd6535c919402ba8cc837bd75abfae
[userinfo] => Array
(
[0] => Array
(
[email] => Alfredo01#gmail.com
)
[1] => Array
(
[gender] => male
)
[2] => Array
(
[country] => it
)
)
)
[1] => Array
(
[name] => AlFredo02
[uid] => jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
[userinfo] => Array
(
[0] => Array
(
[email] => Alfredo02#gmail.com
)
[1] => Array
(
[gender] => male
)
[2] => Array
(
[country] => gb
)
)
)
)
)
I'd like to be able to effectively remove a full block based on UID.
So if I wanted to remove Alfredo02, I'd remove this section
[1] => Array
(
[name] => AlFredo02
[uid] => jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
[userinfo] => Array
(
[0] => Array
(
[email] => Alfredo02#gmail.com
)
[1] => Array
(
[gender] => male
)
[2] => Array
(
[country] => gb
)
)
)
)
Based on UID: jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
Additionally, I'd like to be able to add a full section in for a new user.
I've tried basic lines to get things like the array ID to start with but keep getting nothing returned.
$search = array_search('jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10', array_column($parsed, 'uid'));
Could anyone please help me with a method of removing and adding data? Once I can generate a new "output" with data added or removed, I can write that back to the config.yml file.
Through investigation and determination I have figured out a way which allows you to remove from the array and write back to the yml file with the removed block/user.
$search = print_r(array_search('jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10', array_column($parsed[users], 'uid', true)));
unset($parsed[users][$search]);
echo '<pre>' , print_r($parsed) , '</pre>';
yaml_emit_file("config2.yml",$parsed);
This effectively allows you to search for your uid inside of users and then print the result on the page using echo, but you can remove this.
Then writing the whole output back to the yml file using yaml_emit_file.
The next stage will be working out how to add a new user in to the array and write that back where necessary.
I'll update when I get to that point for the benefit of others.

How to find value inside multidimensional array without looping?

I have a multidimensional array stored in $accounts variable :
Array
(
[0] => Array
(
[id] => 1
[account] => ACR016
[desc] => Salary
)
[1] => Array
(
[id] => 2
[account] => ACR017
[desc] => Bonuses
)
)
I'd like to find/get the "desc" value by using its "account" inside $accounts,
In SQL i will go like this :
SELECT desc FROM table WHERE account = 'ACR016';
How to do that with PHP without do the SQL query (because the array already stored in variable) and without looping ?
Use https://www.php.net/manual/en/function.array-column.php for this.
As this is a builtin function it should perform better than doing your own loop.
Example:
$accountDescriptions = array_column($accounts,'desc','account');
Will result in:
Array
(
[ACR016] => Salary
[ACR017] => Bonuses
)
Alternatively you can use:
Example:
$accountDescriptions = array_column($accounts,null,'account');
Then you will get all data keyed with account instead of numeric index.

How to pull out certain value using php and this output

I've come across a weird scenario I do not know how to code around. I'm creating a JSON API for a wordpress site. I'm using the Connections plugin and trying to pull out the "original" image filename. The output of my sql command is this:
{
["options"]=>
string(396) "a:4:{s:5:"entry";a:1:{s:4:"type";s:12:"organization";}s:5:"group";a:1:{s:6:"family";a:0:{}}s:4:"logo";a:2:{s:6:"linked";b:0;s:7:"display";b:0;}s:5:"image";a:3:{s:6:"linked";b:1;s:7:"display";b:1;s:4:"name";a:4:{s:9:"thumbnail";s:25:"invoicelogo_thumbnail.jpg";s:5:"entry";s:21:"invoicelogo_entry.jpg";s:7:"profile";s:23:"invoicelogo_profile.jpg";s:8:"original";s:24:"invoicelogo_original.jpg";}}}"
}
}
I'm using the following command to acquire that:
querystr = "SELECT options FROM {$wpdb->prefix}connections WHERE id= '{$_GET['companyID']}'";
$options = $wpdb->get_results($querystr);
I'm not sure how to pull out the "original" part of this code though as it's not all that organized. Any help would be appreciated.
What you are seeing is the results of a php serialize call
To get at the original name just do this.
$decodedOptions = unserialize($options);
$original = $decodedOptions["image"]["name"]["original"];
Hope that helps
As a side note the deserialized data looks like
Array
(
[entry] => Array
(
[type] => organization
)
[group] => Array
(
[family] => Array
(
)
)
[logo] => Array
(
[linked] =>
[display] =>
)
[image] => Array
(
[linked] => 1
[display] => 1
[name] => Array
(
[thumbnail] => invoicelogo_thumbnail.jpg
[entry] => invoicelogo_entry.jpg
[profile] => invoicelogo_profile.jpg
[original] => invoicelogo_original.jpg
)
)
)

Named parameters being passed as part of the url because of slashes

I am using cakephp 1.3 and i want to search a record which contain / in string.
i have passed parameter as below.
Search params are as below.
Array
(
[controller] => indents
[action] => admin_index
[named] => Array
(
[pr_no_data] => pr
)
[pass] => Array
(
[0] => no
[1] => dip
[2] => 002
)
[prefix] => admin
[admin] => 1
[plugin] =>
[form] => Array
(
)
[url] => Array
(
[url] => admin/indents/index/pr_no_data:pr/no/dip/002
)
[isAjax] =>
)
as you all can see my url params contain admin/indents/index/pr_no_data:pr/no/dip/002 and now i want to search pr_no as defined in url.
How can i do this.?
because search for / record passing values as passed parameter.
Please help me.
Thanks a lot.
As you can see in your array, CakePHP sees part of the data as passed parameters. In the pr_no_data named variable, you see it references pr as it's value.
[named] => Array
(
[pr_no_data] => pr
)
However, the remaining character string in the data is recognized as passed parameters because of the slashes. So it is reading them as part of the URL, not the pr_no_data variable.
[pass] => Array
(
[0] => no
[1] => dip
[2] => 002
)
What you need to look at is how you generate that variable before it is added to the URL. Perhaps you can change it to a pipe | separated list or a comma , separated list so it does not interfere with the URL and confuse cakephp.
pr|no|dip|002
pr,no,dip,002
Once cakephp receives the data, you can then convert them back to slashes if needed:
$passed = preg_replace('/,/', '/', $this->params['named']['pr_no_data']);
Try to encode the named parameter value using urlencode.
The URL should be
admin/indents/index/pr_no_data:pr%2Fno%2Fdip%2F002
If you create the URL using the Html helper, then CakePHP should encode the value for you.
echo $this->Html->url(array(
'prefix'=>'admin',
'controller'=>'indents',
'action'=>'index',
'pr_no_data'=>'pr/no/dip/002'
));

SugarCRM get_entry returns no fields with REST API

Calling "get_entry" on the REST webservice returns an empty result in this manner:
Array
(
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 85a67fbe-ab86-597a-6bca-4f0305719543
[module_name] => Contacts
[name_value_list] => Array
(
)
)
)
[relationship_list] => Array
(
)
)
My call looks like:
$method = 'get_entry';
$params = array(
'module_name' => 'Contacts',
'id' => $sugarbean_id,
);
But I have absolutely no clue why i get an "empty" object as a result.
Can anyone help me or give me some pointers?
Thanks
Jeroen
You need to provide a session id for the get_entry call. You get the session id when calling the login method. See the get_entry documentation for more information.
Found it. It turns out that I had to explicitly specify select_fields.

Categories