I was wondering if there is a way to loop through all the uri segments in a URL...
/segment1/segment2/segment3/
Is it possible to loop through these with a foreach loop?
You can reference a segment specifically via $this->uri->segment(n) or iterate the segments like so:
$segs = $this->uri->segment_array();
foreach ($segs as $segment) {
echo $segment . '<br />';
}
http://ellislab.com/codeigniter/user_guide/libraries/uri.html
$uri = $this->uri->uri_string();
You can use explode() to cut it in peaces and put in an array. Then use foreach to loop through the array.
I'm not sure about foreach, but you should be able to use a 'for' with $this->uri->segment($id)
from within a controller.
Related
It might be a weird question but I need your help
I might have an array with values.
foreach ($users as $user)
{
$name_user = //condition
array_push($firstarray, $name_user );
}
Let's suppose that $firstarray has these values now ( example : 1 ,2 ,3)
Now
foreach ($firstarray as $t)
{
dd($t);
}
It shows me only the first value
Should it work like this ? I think it has to show the three values.
Of course it will only display 1 because dd() means Dump and Die
You're using DD in a loop, during the first loop, you used the dd(), after reading the dd(), it will kill all the next processes
I suggest you use the dump() so you could see all the contents
Please be aware that Laravel has dd() and dump().
dd() = dump and die, meaning that it will show (print/echo) you the variable but on the same time your script will stop executing when it reach that point on your code.
dump() = dump, simply means that it will show the variable you are interested in, your script will continue executing.
For your situation you should do it like so:
foreach ($firstarray as $t)
{
dump($t);
}
"dd" means "Dump and die".
It prints the first value of $firstarray, "t", and then dies without executing the rest of the loop.
You may wanna try with "echo" or "print_r".
Also, you could use var_dump($firstarray) if you are debugging and want to know the values of the array without having to use a foreach loop.
Foreach will iterate over your array. Since you're doing dump and die, your code will die on first iteration.
If you want to know your array value, you can dump your array outside the loop
dd($firstarray);
dd is a function to dump and die .lets use var_dump method to dump and not die:
foreach ($firstarray as $t)
{
var_dump($t);
}
You can use this to show the data in a nice view.
foreach ($data as $row){
echo '<pre>';
print_r($row);
echo '</pre>';
}
I am trying to implement searching feature in laravel. Everything is working fine. I am passing the html table data in a variable and parsing it later.
The problem is I could not figure out how to increment a value inside the HTML element.
I am trying this
foreach ($studentDetails as $indexkey=>$studentDetail){
$output .= "<tr><td>". $indexkey + 1 ."</td></tr>";
}
This is not going to work as string concatenation is being done in the value. Any idea on how to increment the value by 1.
You can do something like this below: Take a variable for increment before the output variable and use that in output variable:-
foreach ($studentDetails as $indexkey=>$studentDetail){
$temp = $indexkey + 1;
$output .= "<tr><td>". $temp ."</td></tr>";
}
I know i can get all segments from url like this
Lets say i have this example link
www.example.com/de/products.html
Using url_helper like this:
$data['url'] = $this->uri->uri_string();
I will get value like this
de/products
But i dont need first segment de, only products, the problem is that
i dont know how many segments it will be, i only need to remove the first
Is there possible to forget first segment with url helper in CI?
Try like this...
Use the php's explode() function to make the url string as array.Then apply array's array_shift() function which always removes the first element from array.
Code is looks like as below
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
//print_r($arr);
Then use the php's implode() method to get the URI without first segment.Hope it will works...
$uri=implode('/',$arr);
echo $uri;
There is no URL helper in the CI to forget the first segment. However you can easily make a custom one and put #Hikmat's answer below it in the application/helpers/MY_url_helper.php in the Core folder.
e.g.
function my_forget_first_segment() {
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
$uri=implode('/',$arr);
return $uri;
}
Before Edit answer.
You need to try this
$second_segment = $this->uri->segment(2);
From Codeigniter documentation -
$this->uri->segment(n);
Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1. news
2. local
3. metro
4. crime_is_up
The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:
$product_id = $this->uri->segment(3, 0);
example:
<?php
$data=$this->uri->segment(2);
$val=explode('.', $data);
echo $val[0];
?>
The request is simple, however, I cannot find a way to implement it. I have links like:
httр://mysite.com/index.php?lang=EN
httр://mysite.com/index.php?route=add&lang=EN
httр://mysite.com/index.php?route=view&lang=EN
and so on. What I want is to create 301 redirects so that EN could be changed to GB. For example, if a customer opens httр://mysite.com/index.php?route=add&lang=EN, he should be redirected to httр://mysite.com/index.php?route=add&lang=GB.
I have searched for this for days and have failed to find a working solution. Please help.
Does it have to be done in .htaccess? Here's a relatively simple way of doing it in PHP:
<?
if ("EN" == $_GET['lang']) {
$params = $_GET;
$params['lang'] = "GB";
$query_strings = array();
foreach ($params as $key => $value) {
$query_strings[] = $key . "=" . $value;
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mysite.com?" . join($query_strings, "&");
}
Bottom line is that it may be easier to fix this problem on a level where you can isolate each query parameter and look at just the lang parameter and determine whether to do a redirect.
With regular expressions (as you would need to use in .htaccess) it's harder to isolate just the lang part. You would also need one line per language you want to redirect and maintain the list.
Can I use an xpath query on a result already obtained using xpath?
In most hosting languages/environments (like XSLT, XQuery, DOM) you can. Don't know about PHP, but it would be strange if it doesn't allow this.
Of course, the result of the first query must be a node-set, in order for a future "/" operator to be possible/allowed/successful on it.
I have done it in PHP/SimpleXML. The thing that I didn't understand at first is that you're still dealing with the full SimpleXML object, so if you start with "/nodename", you're operating on root. If you start with "nodename" you are starting at the beginning of the result node. Here's my example:
$parsed=simplexml_load_string($XML);
$s = '/ItemSearchResponse/Items/Item';
$items = $parsed->xpath($s);
foreach($items as $item)
{
$s = 'ItemAttributes/Feature';
$features[]=$item->xpath($s);
$s = 'ASIN';
$asins[]=$item->xpath($s);
$s = 'ImageSets/ImageSet[#Category="primary"]';
$primary_img_set=$item->xpath($s);
$s = 'MediumImage/URL';
$medium_image_url[] = $primary_img_set[0]->xpath($s);
}
In PHP, for example, you can run a query with a context, i.e. a given node. So if you have got a DOMNodeList as a result of the first query you can do things like this:
$query1 = '//p';
$query2 = './a'; // do not forget the dot
$node = $xpath->query($query1)->item(0);
$result = $xpath->query($query2, $node);
Of course this is a silly example because it could have been done just in one shot with the correct XPath experssion but I believe it illustrates your question.