I have a survey page that will reference an invoice number. I'm trying to make the URL clean so that there are no question marks or equal sign. For example:
http://www.MyWebsite.com/survey/832551
Where 832551 is the invoice ID in reference. I am trying to avoid ?invoice_ID=832551.
How can I get a php page to read that and make it a variable value?
php 4.0.8
Make an variable with url
//You get all the url in array
$var = $_SERVER["REQUEST_URI"];
//Explode it in array, with slash '/'
$var_array = explode("/",$_SERVER["REQUEST_URI"]);
//Get the last element of array, which will be the invoice_id
$invoice_id = array_pop($var_array);
But if you use some of php framework this is how you get controller attribute by default,
I suggest you to use some php framework with MVC structure.
It is what we call URL rewriting.
You can find a good example here
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Related
The current URL
www.mydomain.com/?ct_keyword&ct_city=la-jolla&ct_property_type=apartments&ct_ct_status=for-rent
New URL .
www.mydomain.com/valueof-ct_city/valueof-ct_property_type/valueof-ct_status?ct_keyword&ct_city=la-jolla&ct_property_type=apartments&ct_ct_status=for-rent
Note: It should take 3 variables from the string and add it to the path
Example of the results
www.mydomain.com/la-jolla/apartments/for-rent/?ct_keyword&ct_city=la-jolla&ct_property_type=apartments&ct_ct_status=for-rent
And of course, the query string is changing so it should take the variables from the current query string.
The query string has more variables in it, I just want the 3 specified above added to the path.
The site is based on WordPress.
Any ideas on how can I do this?
Use the ampersand & to glue variables together:
$url = "http://domainname/main.php?email=$email_address&event_id=$event_id";
I have bellow URL and storing in one variable which is $currentURL.
$currentURL = http://localhost/example/villa-search/page/2/?features=Beachfront%2CCook%20Services%2CGarden%2CPrivate%20Pool%2CSea%20View
Now I want to break URL in to section and store in two different variables
First Part is:
http://localhost/example/villa-search
second Part is:
/page/2/?features=Beachfront%2CCook%20Services%2CGarden%2CPrivate%20Pool%2CSea%20View
How to make this possible?
You could use PHP's parse_url() and get components from it and re-construct your desired format from it.
And then use explode() the query part obtained from parse_url().
So I have this variable of $date = array($year,$month); inside a couple of nested foreach statements. I have a link that when pressed should pass the $date variable over to my functions.php for me to play around with.
I'm using wordpress and so far I understand the link has to work something like this:
$link = admin_url('admin-ajax.php?[$date variable needs to go here]&post_id='.$post->ID.'&nonce='.$nonce);
Basically my question is how exactly does the link above need to be formatted to send my variable? Also, on the server side, how best to receive that variable?
First of all, you can't send arrays directly through GET requests (GET requests are the ones with the parameters visible in the url, in layman terms)
therefore, you should do the following:
$date = "$year-$month"; //example: 2013-09
$link = admin_url('admin-ajax.php?my_date='.$date.'&post_id='.$post->ID.'&nonce='.$nonce);
breaking the url down to the components, in layman terms:
everything before the ? is the server address and the page that needs to be served
everything after is the so-called "querystring", which is a sequence of pairs (A=B) separated by ampersands &.
so, a URL that looks like this
www.example.com/dynamic_page.php?A=B&C=D&E=F
means:
visit www.example.com, fetch the page named "dynamic_page.php" and use the value B for the variable A, the value D for the variable C and the value F for the variable E.
For example I have a URL like this one:
index.php?country=Canada
If it is just index.php that means that the default country is USA. People can switch between countries by checking or unchecking a checkbox.
But people can sort their results via GET variables:
State
Surname
Name
But if I use $_SERVER["REQUEST_URI"] then it will always just keep adding new values to my URL array (query string). It works if it is just index.php then I can make it like this:
State
Surname
Name
I know that after index.php it will always be a question mark ? first.
But what when visitors want to keep index.php?country=Canada and just switch between sort=state, sort=surname and sort=name. Then I need to know if a question mark is already in the URL, when to add & mark. I'm not sure how to solve this problem.
Change the way you echo your link:
PHP
<?php
$link = $_SERVER["PHP_SELF"];
if(isset($_GET['country']
$link.="&";
else
$link.="?";
?>
And echo link like this:
HTML
State
NOTE: I deleted the question mark before "sort".
You need logic to dynamically build a querystring, rather than statically adding ? and &.
Something like http_build_query would be the route I'd go, but the information you've provided is small compared with the possibilities you appear to want, so its hard to provide specific code.
Here is some more information about the http_build_query function in PHP. Its purpose is to build a querystring, here is an example of what you might do:
// capture the values into variables, however you want ($_GET is example)
$country = $_GET['country'];
$state = $_GET['state'];
$city = $_GET['city'];
$data = array();
// use logic to dynamically build the array, so long as the variables have values
!empty($country) ? array_push($data,'country'=>$country) : '';
!empty($state) ? array_push($data,'state'=>$state) : '';
!empty($city) ? array_push($data,'city'=>$city) : '';
// apply the array dynamically built
$querystring = http_build_query($data);
// concatenate to form the new URL
$url = 'http://www.example.com/?'.$querystring
If you declared country to be USA and city to be Seattle, but did not declare state, it would produce:
http://www.example.com/?country=USA&city=Seattle
Something along these lines should build the dynamic array you want with only the values you are looking for.
All of the variables in the query string are stored in the $_GET php superglobal array. In your example you can access the country with $_GET['country']. Do default to the USA you can use isset() to check if the variable exists.
$country = "USA";
if(isset($_GET['country'])) {
$country = $_GET['country'];
}
The solution here is to use either $_GET (already mentioned in another answer), or http://php.net/manual/en/function.parse-url.php, which makes the ordering of arguments completely irrelevant. Simply parse the url into an array of query arguments, and test for the ones you need. To turn it all back into a url, you would use http://php.net/manual/en/function.http-build-query.php
Regular expressions have never been one of my strong points, and this one has me stumped. As part of a project, I want to develop an SEO link class in PHP. Handling the mod_rewrite through Apache is fairly straightforward for me, and that works great.
However, I'd like to create a function which is able to generate the SEO link based on a dynamic URL I pass in as the first (and only) parameter to the function.
For example, this would be the function call in PHP:
Blog Post Title
The function CreateLink would then analyse the string passed in, and output something like this:
blog/blog-post-title
The URL stub of the blog post is stored in the Database already. I think the best way to achieve this is to analyse the dynamic URL string passed in, and generate an associative array to be analysed. My question is, what would the Regular Expression be to take the URL and produce the following associative array in PHP?
link_pieces['page_type'] = 'blog/post';
link_pieces['post'] = 123;
link_pieces['category'] = 5;
Where page_type is the base directory and request page without extension, and the other array values are the request vars?
You can just use parse_url and parse_str, no need for regexes.
Use parse_url to break the URL into parts:
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
Then use parse_str to break down the querystring part of the URL.