Codeigniter URI function url_title() how to use - php

I am using the CodeIgniter framework and I am confused on how to remove %20 from my urls. Below is an example of my code.
Controller - blog
method - show
attribute - this is my blog
public function show($blog= null)
{
// my attempt to set the uri segment
$blogName = $this->uri->segment(3, url_title($blog));
... //other code
}
this doesnt work, I am confused where I implement the url_title('this is my blog') function so that on page load it shows:
/blog/show/this-is-my-blog
do I need to do something in the config/routes.php file?
thank you!
EDIT:
Alright so I found out that url_title() output this20is20my20blog so I now have this:
$blogUrl = str_replace("%20", "-", $blog);
$this->uri->segment(3, $blogUrl);
but it still returns the URL with %20

You just need to use the native php function urldecode to remove any characters that get encoded in a url. Spaces in a URL get encoded to %20 so instead of doing a str_replace just try
public function show($blog= null)
{
// i'm not sure what url_title does so you might have to tweak this a little
$blogName = $this->uri->segment(3, urldecode(url_title($blog)));
}
http://php.net/manual/en/function.urldecode.php

Try echoing the url_title() just before you do the $this->uri->segment(); to ensure it's returning correctly.

Related

Codeigniter spaces in url

I have a method which returns all jobs by category.
The problem it's when i have spaces in the category name. How could I access those results?
for ex if i access http://localhost/management_system/Job/get_jobs_by_cat/Architecture it will return all the jobs from the Architecture category.
But when i try to access the category Information Technology I can't do it while I have spaces into the url, so I've tried with _ - and i didn't get any response.
How I can fix this issue?
You would likely want to use urldecode() to decode the url parameters in the controller function processing this logic. In your controller, do something like this:
$jobCat = urldecode($this->uri->segment(4));
Then, you would pass $jobCat to your model.
Here are some other Stack Overflow links that might help your cause.
How to pass parameters with space to controller from URL in
codeigniter?
Php - Codeigniter url spaces
what is the use of $this->uri->segment(3) in codeigniter
pagination
You need to URL encode Information Technology.
$category = 'Information Technology';
$encodedCategory = rawurlencode($category);
$url = 'http://localhost/management_system/Job/get_jobs_by_cat/' . $encodedCategory;
echo $url;
// http://localhost/management_system/Job/get_jobs_by_cat/Information%20Technology

API url is being encoded with & instead of just &

I'm trying to connect my app up to an API using this code:
class PostcodePrice{
public function getPrice($postcodes){
$apikey = "MYAPIKEY";
$priceurl = "http://api.zoopla.co.uk/api/v1/area_value_graphs.js?area=".$postcodes."&output_type=outcode&api_key=".$apikey;
$price = file_get_contents($priceurl);
$decoded = json_decode($price);
return $decoded->result;
}
}
Now the problem I'm having is my url is being passed to the api and replacing my & with & so my url is looking like this:
http://api.zoopla.co.uk/api/v1/area_value_graphs.js?area="postcode&output_type=outcode&api_key=MYAPIKEY
The API I am using refuses the url with the encoded & so it obviously doesn't work as I get 403's back as a response.
I've tried things such as string replace, htmlspecialchars etc.
Anyone know of how I can stop this from happening?
U can make "$priceurl" url without concatenation like this.
$priceurl = "http://api.zoopla.co.uk/api/v1/area_value_graphs.js?area=$postcodes&output_type=outcode&api_key=$apikey";
Hope it's work for u.

Shorten Link with php and API

I already have a function on my page that produces url from curl. I need to use the following string to return short url in text format or "simple" using the shortswitch.com api.
Here Is my code:
<?php
$long_url = urlencode('curPageURL()');
$url = "http://api.shortswitch.com/shorten?apiKey=[apikey]&format=simple&longUrl={$long_url}";
$result = file_get_contents($url);
print_r($result);
?>
I'm attempting to use a function I found for bit.ly to use with shortswitch.com api as shortswitch.com/admin/api.
My problem is that I'm not getting any type of output from the function, no shortened url is being generated.
best regards,
I'll just guess that you're actually trying to call the function curPageURL() and url encode its result instead of url encoding the string "curPageURL()":
$long_url = urlencode(curPageURL());

Codeigniter how to grab the original string from url_title()?

I'm trying to learn how to use code ignitor but I've run into a little problem. As with most people when they first use a framework, I too am making a blog. I'm trying to make my view links look like: http://localhost/blog/view/my-blog-post-title and I've gotten that far. But when I get to the actual view method is when I run into problems. Basically I'm my-blog-post-title refers to the 1st record of posts in my database. But the actual title looks like My Blog, Post Title.
So how do I get the id from my-blog-post-title when the original is My Blog, Post Title so I can pull that post from the database? Or should I just use numbers(I don't want to ;_;).
Well, I think the best solution and the easier approach would be to create a column in your posts table, something called "slug", which contains the url_title() output (the moment you create your article, you save that value in this db column as well as the other infos), and query against that instead of using this more complicated method.
So, you grab the last segment of the url, either via $this->uri->segment(3) or just by passing the whole uri to your controllers' method, and query against that column:
class Blog extends CI_Controller {
public function view($slug)
{
$this->load->model('blog_model');
$data['posts'] = $this->blog_model->search_slug($slug);
$this->load->view('myview',$data);
}
}
Model:
function search_slug($slug)
{
$this->db->select('id,title')
->from('posts')
->where('slug',$slug);
$query = $this->db->get();
return $query->row();
}
View 'myview.php':
echo $posts->id;
echo $posts->title;
You should be able to use something like
$parts = explode("/",$_SERVER['REQUEST_URI']);
$title=$parts[(count($parts)-1)];
to turn your url into an array and then grab the title from the last section. string replace the "-" with " " and then do a %like& search in your db for the title. Not sure that's the best approach but should work.
This code can get you as far as extracting the title:
$url_string = "view/my-blog-post-title";
function getOriginal($url_string) {
$url_parts = explode("/",$url_string);
$url_title = $url_parts[1];
$title_parts = array_map("ucfirst",
explode("-",$url_title));
return implode(" ",$title_parts);
}
echo getOriginal($url_string);
Which will output:
My Blog Post Title
The tricky part is where to insert the comma (,). This is tricky because blog post titles may be have more words like my-blog-post-title-some-other-words or my-blog-title-word-word-word. The comma can go anywhere.
If it is always constant that the comma is to be inserted after My Blog (My Blog is constant) then you just do an str_replace after calling getOriginai(..);
echo str_replace("My Blog","My Blog,","My Blog Post Title");

PHP Query String Manipulation

I have a small issue with manipulating the current URL query string to add an extra parameter at the end.
Per example, say there's a category layout for products, the URL would be:
index.php?category=3&type=5
Now, on that page I have a link for a layout that is either a table or a grid. In those URLs I currently have:
<a href="index.php?<?php echo preg_replace($array,'',$_SERVER['QUERY_STRING']); ?>&layout=grid" ...
Then, I do the same for the table href as well. Also in my array I have just:
$array = array ( '/&layout=table/', '/&layout=grid/' )
Is this the right way, or is there a better way for doing this? I'm asking because without preg_replace, it will continue adding that same layout parameter everytime it is clicked, so it will also show the previous parameter, then the next, then the next.. without removing the previous layout parameters.
Any insight on this will be much appreciated.
EDIT:
Thanks to the answers below, I have created a little function:
function buildQuery($key,$value) {
$params = $_GET;
$params[$key] = $value;
return http_build_query($params);
}
Then its only a matter off:
grid
this might seem pointless but i like to have my view / template files without the extra set vars. Im a clean freak. I might even return the 'index.php?' with it just so i can be more lazy, anyways something to play with now :)..
If you want to modify the query string, it's easier to simply modify the GET variables and rebuild the query string:
$params = $_GET;
$params['layout'] = 'new_layout';
Then:
...
Although you could also do:
...
Think about directly parsing the $_GET paramaters to build your url.
I think what you want to do is have the link going to index.php with all the same parameters as you have at the moment, but changing layout to grid. I'd suggest you do something like this:
<?php
// make a copy of the $_GET array with all the parameters from the query string
$params = $_GET;
// set layout=grid regardless of whether layout was set before or its value
$params['layout'] = 'grid';
// generate a query string to append to your urls.
// Note that & is used as the arg separator; this is necessary for XHTML and advised for HTML
$queryString = http_build_query($params, '', '&');
?>
href="index.php?<?php echo $queryString; ?>">
This is much easier than trying to edit and fix the $_SERVER['QUERY_STRING'] yourself.

Categories