I am making the dynamic website in PHP. While code is running good using pageid.
The url is now like www.google.com/pageid=1
Now, i want to change the url for 1 level pages as www.google.com/page1
for level 2 as www.google.com/page1/page2
for level 3 as www.google.com/page1/page2/page3
While unique address is stored in the my table as page1,page2,page3.
How can it be possible to change url at run time. Please give the examples and comments. So that it may helpful to understand.
Also i want to know if it is possible using the .htaccess file. If possible How .htaccess %{REQUESTED_FILE}% will reach out for my unique url's that are stored in the database.
$URL= "www.google.com/page1"
1) split the string with '/'
$data = explode("/",$URL);
$data[1] will have page1 value
2) replace the string page with ""
$pageId = str_replace("page","", $data[1]);
3) whatever is left is the page id
pageId has the value now.
Use that to query the database.
Another way is to use regex to extract the number from page1 and use it where-ever you want to use it.
Related
Note: I have edited the post on 2017/08/20
I'm trying to obtain a list of product page's URL that goes "www.example.com/product/11111/".
There are over 200 different products available and each of them has its own product page, I want to print out each product in a PDF file.
On "www.example.com/productlist/", there are URLs that lead to each product's page.
So, what I'm trying to do is
Obtain URLs that I need from "www.example.com/productlist/"
Generate PDF files of URLs that I have obtained
Insufficient Information: You did not provide me with much information about the code you already have and how the website will get the 200 URLs, so I can't write the whole code because it depends on the way your website will get the links from.
If you explain more about how the website is supposed to get the links, I will help you put them into an array and save them into a file and implement the rest of the code!
1-) Thing I understood is getting the last 5 characters
As you just want the last 5 characters, not the whole last part, you can do something like this.
$string = "http://example.com/folder/example/1234567"; //your link
$characters = strlen($string); //gets the characters count
$letters = 5; //edit 5 to show more or less manually
$code = substr($string, $characters - $letters, $characters);
echo $code; //will show the last 5 characters
I am always here to help. Good luck!
Start with parse_url().
$parts=parse_url("http://example.com/folder/example/12345");
That will give you an array with a handful of keys. The one you are looking for is path.
Split the path on / and take the last one.
$path_parts=explode('/', $parts['path']);
Your random numbers can now be stored like:
$number = $path_parts[count($path_parts)-1].
I want to cut a html link with php.
The html link is everytime the same pattern
domain.com/forum/members/84564-name.html
I want to get the 84564 from the name.
the /forum/members/ is everytime the same.
and the "-" after the user-id is also everstime the same.
Can you help me to extract the user id?
I'm going to assume the user id may not always be 5 digits.
$domainSplit = explode("/", $theDomain); // split into parts
$theIdSplit=explode("-", $domainSplit[3]); // split 84564-name.html
$id=$theIdSplit[0];
OK I have a large database I an querying LIKE SO
SELECT * FROM PricePaid WHERE PostCode LIKE '$loc%'
ORDER BY Price DESC
LIMIT $start,$perPage
I know how to do the paging links to work out the total records found and page through them. I am using the following code on my NEXT link :
<a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&start=$next'>NEXT</a>
Which works OK as it keeps my ?loc variable but the start variable duplicates.
This is how my url looks on the first page
mysite/uk-property-prices.php?loc=l24
Then when I click my next page link the first time my url passes the start variable and the query displays page two
mysite/uk-property-prices.php?loc=l24&start=1
But when I click next again I get this
mysite/uk-property-prices.php?loc=l24&start=1&start=2
Now I know this works but after a while my URL gets stupidly long LIKE so
mysite/uk-property-prices.php?loc=l24&start=1&start=2&start=3&start=4
What AM I doing wrong
$_SERVER['REQUEST_URI'] contains the query string as well. All you're doing is appending more and more variables each time.
You will need to parse the query string, replace the value that you want to replace, and re-build the URL. PHP has already parsed it for you in $_GET (assuming you don't need duplicate values). Untested, but try this:
$newQueryParts = $_GET;
$newQueryParts['loc'] = 3;
$urlParts = parse_url($_SERVER['REQUEST_URI']);
echo 'http://', $_SERVER['HTTP_HOST'], $urlParts['path'], '?', http_build_query($newQueryParts);
Don't forget to consider the protocol as well. Your site might use HTTPS in the future. Also, they way you are interpolating variables directly in your query implies that you might be open to SQL injection attacks. Always use prepared/parameterized queries to avoid this problem.
You're using 'REQUEST_URI', which will naturally contain the address of the page you're on, so if your visiting http://example.com/somescript.php?foo=bar in your browser, REQUEST_URI will be /somescript.php?foo=bar.
Since you spit out that into your html's href parameters, then slap on MORE parameters, you naturally start building up longer and longer urls:
1st request /somescript.php?foo=bar&start=1
^^^^^^^^^^^^^^^^^^^^^^^---request uri for #2
2nd request /somescript.php?foo=bar&start=1&start=2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---request uri for #2
3rd request /somescript.php?foo=bar&start=1&start=2&start=3
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^request_uri for #3
I'm putting together an extremely simple ad server with php and mysql on a website. The site currently has 4 or 5 static pages with lots of dynamic content, so URLs look like this:
index.php?pid=1 or content.php?spec=2
What I'd like to do is add a field to my table of ads to keep track of that page(s) the ad is going to be displayed on.
Should I store URLs that have an ad as a list of comma separated values?
Once I retrieve this variable, what's the best way to separate the values into an array?
What's the best way to split a string so I can split the page name $_GET name and variable (as in 'index, pid, 1' or 'content, spec, 2' using the examples above.) ??
Additional Info:
As an example, doctors.php is structured something like this:
doctors.php Listing of Doctor Specialties
doctors.php?spec=# Listing of Doctors that have a particular
specialty
doctors.php?pid=# One specific Doctor's information
I have a few dozen specialties, and a few hundred doctors. I want to be able to place an ad on specific pages/URLs, say doctors.php?pid=7
But I also want to be able to place an ad based on, say, all of the doctors who have a specialty with the ID of 6. That could be 60+ pages, so it doesn't really make sense to have separate table rows. If I needed to change the link on the ad, I don't want to have to change it 60 times or search for it.
Don't store as a CSV.
Add separate database rows for each ad / URL combination.
Then retrieving content will be trivial.
Store URls one per line in simple file. Than you can use php function "file" to read this file as an array.
For url split use http://php.net/parse_url function
Here's what I think will work...
My ad table will have three variables, (a, b, c)
If I have an ad placed on doctors.php?spec=12, I'll store the following:
a = 'doctors';
b = 'spec';
c = '12';
If the ad was meant to display on ALL specialty pages, I'd store:
a = 'doctors';
b = 'spec';
c = NULL;
If something is NULL, it will simply indicate ALL of a set. It seems like an elegant solution, I'll post code if it works.
$url = "index.php?pid=1";
$pos = strpos($url, "?");
if ($pos != false) {
$pieces1 = explode("?", $url);
$pieces2 = explode("=", $pieces1[1]);
$array = array($pieces1[0], $pieces2[0], $pieces2[1]);
}else{
$array = array($url, '', '');
}
print_r($array);
You'll need to improve this code if you want it to work for multiple variables in your url for ex: index.php?v=hello&t=world
Also, i did not test this code. I just wrote it without checking the functions etc.
I want to apply the page HTML title in the URL
for example in here (stackoverflow) the url is something like that:
http://stackoverflow.com/questions/10000000/get-the-title-of-a-page-url
you can see the "get-the-title-of-a-page-url" part which is the page title
what i mean is when the user go to spowpost.php?post=1
the actual url that shows up when the pages load will be
spowpost.php?post=1$title=..the_title..
how can i do that?
EDIT: i was thinking about htaccess , but i don't know this very well so tutorial would help for this CERTAIN case..
You can use .htaccess for this. For example:
RewriteEngine On
RewriteRule ^questions/(\d+)/([a-z-]+) index.php?id=$1&title=$2 [L]
Your PHP page (index.php) receives the id and title as parameters in $_GET[]:
echo $_GET['title'];
// get-the-title-of-a-page-url
You can then use that (or the id, which is easier) to retrieve the correct item from your data source:
// Assuming you didn't store the - in the database, replace them with spaces
$real_title = str_replace("-", " ", $_GET['title']);
$real_title = mysql_real_escape_string($real_title);
// Query it with something like
SELECT * FROM tbl WHERE LOWER(title) = '$real_title';
Assuming you do have an id parameter in the URL of some sort, it's easier to query based on that value. The title portion can be used really only to make a readable URL, without needing to act on it in PHP.
In reverse, to convert the title to the format-like-this-to-use-in-urls, do:
$url_title = strtolower(str_replace(' ', '-', $original_title));
The above assumes your titles don't include any characters that are illegal in a URL...
$article_link = "http://example.com/spowpost.php?post=$postid&$title=$url_title";
Or to feed to .htaccess:
$article_link = "http://example.com/spowpost$postid/$url_title";
From what I understand, your title will be passed to the page as part of the URL. To show it in the title bar, put this in the section:
<?php $title=urldecode($_GET["title"]); echo "<title>$title</title>"; ?>
You might need to change parts of this, for instance dashes to spaces or something. If that is the case, use PHP's str_replace function: http://php.net/str_replace
Not sure about what's the problem you are facing but just according to what you say in your post the anwser would be:
1.You take the ID from the URL.
2.You search in your database for the original title
3. And then display it in the tag in the of your HTML.
Clarify if you have problem with any of the previous points.