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

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'
));

Related

Get url parameter in cakephp 1.2.4.8284

I am using cake php version 1.2.4.8284
I have url like below
http://www.example.com/users/index/filter:/site:917+919/
But when i print this url in users controller with 'print_r($this->params);'
I get result like below.
Array
(
[pass] => Array
(
)
[named] => Array
(
[filter] =>
[site] => 917 919
)
[controller] => users
[action] => index
[plugin] =>
[url] => Array
(
[ext] => html
[url] => users/index/filter:/site:917 919/
)
[form] => Array
(
)
[isAjax] =>
)
Here you can see + is missing from 'site' and 'url'.
But I want exact url and site value. Please Help.
Your URL must be encoded to get the actual symbol.
http:www.example.com/users/index/filter:/site:917%2B919/
The + character represents a space in a URL so print_r($this->params) is returning the correct value. If you want your url to include an actual + character you need to URL encode it as %2B; so your URL would be:-
http:www.example.com/users/index/filter:/site:917%2B919/
You might want to take a look at the PHP docs about urlencode.

How to retrieve an array in URL?

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.

How to reformat this array without knowing the keys in advance

I'm querying an API and getting a response back with various countries. Here is the relevant array I'm working with and what it prints out.
print_r($apiResponse['response']['data'][0]['countries']);
prints this:
Array ( [US] => Array ( [id] => 840 [code] => US [name] => United States [regions] => Array ( ) ) [CA] => Array ( [id] => 124 [code] => CA [name] => Canada [regions] => Array ( ) ) )
I am looking to save an array of only the two character country codes from that data. The only thing is the key is unknown to me when I query it so I don't know how to access the [code] section of it to save it to my new array.
I want to end up being able to take whatever amount of countries the API sends back and save the two character codes in a format like this:
'country_codes' => array('US','CA','UK','AU')
Thanks for your help!
Use the array_keys() function. Here you have the documentation.

Get values from Zend_Db_Table_Row Object array

i am using a zend model which returns me an object in form of $row with all values
but i am not able to get value from this array . is this posible to get values without foreach
this is the array returned
Zend_Db_Table_Row Object
(
[_data:protected] => Array
(
[user_id] => 2
[udid] => 34
[firstname] => a
[lastname] => a
[email] => jusic.sl#gmail.com
[username] => abc
[password] => c91718531fd9f8b89c4e
[created_date] => 2010-02-11
[updated_datetime] => 2012-06-25 12:48:17
[lastlogin_datetime] =>
[group_id] => 2
[status] => Active
)
)
i need to get the user_id,firstname,email from this array
any help will be appreciated .
i have tried like
$forgotpassword = $userModel->forgotpassword ( $post ); // which contains this array
$id = $forgotpassword['_data:protected']['id']; exit; // but doesnt seem to work
You cannot access _data directly. It's protected.
From the ZF Reference Guide on Naming Conventions:
[…] variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore.
You can do either do (due to __get/__set)
echo $forgotpassword->user_id;
or (due to ArrayAccess)
echo $forgotpassword['user_id'];
or (if you want an array)
$array = $forgotpassword->toArray();
echo $array['user_id'];
Please see the Reference Guide and the code
http://framework.zend.com/manual/en/zend.db.table.row.html
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Db/Table/Row/Abstract.php

How can i create a array with double GET variables

i have a form with some checkboxes. if i activate a ceckbox, jquery is sending the data with the .serialize() function to a php file via ajax. The problem is, that jquery send some double parameters. Here is the Query:
area=26-50&area=51-75&area=76-100&area=100&std=1&std=3
How can i create a array like this:
array(
'area' => array(0 => '26-50',1 => '51-75',2 => '76-100'), std => array(0 => 1,1 => 3)
)
PHP overwrites the last variable with a new one...
Thanks for the help!
greetings
[] notation will make it possible to transmit array data in a form.
Name the checkboxes in the form like this:
<input name="area[]" type="checkbox" value="51-75">
this should build an array of all selected check boxes.
PHP can support this if the key name is appended with []:
area[]=26-50&area[]=51-75&area[]=76-100&area[]=100&std[]=1&std[]=3
/*
Array
(
[area] => Array
(
[0] => 26-50
[1] => 51-75
[2] => 76-100
[3] => 100
)
[std] => Array
(
[0] => 1
[1] => 3
)
)
*/

Categories