Laravel foreach loop - laravel

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

Related

Condition Using Controller in Code Igniter

I'm trying to create a function, it exists and does not exist in the search feature. Condition does not exist, will display Error 404. Condition, exists. Then it will display from search. But in the code I wrote, it only shows Error 404.
This is my model:
function search_album($keyword){
$result=$this->db->query("SELECT tbl_album.*,DATE_FORMAT(album_date,'%d %M %Y') AS date FROM tbl_album WHERE album_title LIKE '%$keyword%'");
return $result;
}
This is my controller:
function search(){
$keyword=str_replace("'", "", $this->input->post('xfilter',TRUE));
$x['data']=$this->model_album->search_album($keyword);
if(empty($x) && empty($keyword)){
$this->load->view('view_contents',$x);
}
else if (!empty($x) && !empty($keyword)){
$this->load->view('view_error_404');
}
}
I've tried from this source, but it doesn't work. Can you help me?
There are several errors in your code:
ONE: You have interchanged your if statements. Like, if the search did not return any data and the keyword was not supplied, it is supposed to display the "view_error_404", otherwise, it is supposed to load data into the view "view_contents".You did the vice versa I have corrected that for you in the code below.
TWO: You are checking if $x is empty which will never be empty as you have initialized $x['data']. Note that the model can return empty data, such that $x['data'] is empty. Instead, check if the search result is empty by replacing empty($x) with empty($x['data'])
THREE: In your model, you are returning the query builder class, but not the actual data from the query statement. instead, replace return $result; with return $result->result();
FOUR:
From your if statements, you need to add an else statement so that if the 2 conditions are never met, it can execute. With your current implementation, there is a state which will not meet first or second conditions and will lead to a blank screen.
if(empty($x['data']) && empty($keyword)){
$this->load->view('view_error_404');
}else if (!empty($x['data']) && !empty($keyword)){
$this->load->view('view_contents',$x);
}else{
$this->load->view('view_error_404'); // replace this code with your preference
}
you need to mention search function in routes.php file like this:-
$routes['search'] ='your_controller_name/search';
without knowing your function model_album->search_album($keyword) and assuming it returns a string or array, you don't need to worry about checking for the keyword, since without a keyword the function should return false or empty.
anyway you mixed up your if/else conditions, since you are checking for not empty results to return a 404, instead of empty result returning the 404.
this should do it:
if(empty($x)){
$this->load->view('view_error_404');
}
else {
$this->load->view('view_contents',$x);
}

how to display single value use arrayshit or other thing (without loop) in Laravel

I use display value or data. In case of a single value use loop, but I know this is a bad habit. How can I solve this? for example I use this:
#foreach($result as $result)
{{$result->data}}
#endforeach
First of all, your code doesn't tell anything, and also you have this code all wrong.
You can't name a pointer as the same name of the array, ($result as $result ???) you will have wrong results or errors.
Also inside the froeach you are referencing a variable that doesn't exists (?).
In my opinion "foreach" is necessary to loop through arrays.
I really don't know what are you trying to do, so that would be great if you expand your question and provide more information.
You can replace that with some code like this:
#if($result)
{{$data}}
#endif
You can try code below see
//example in file ExampleController.php
use App\Posts;
public function showAll(){
$data = Posts::all();
return view('showall',['result'=>$data]);
}
//file web.php
Route::get('/show-all','ExampleController#showAll');
//show-all.blade.php
#foreach($result as $value)
{{$value.title_name}}
#endforeah

Laravel increment a variable inside HTML element

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>";
}

Is there any way to loop through uri segments in codeigniter?

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.

php xpath query on and xpath result

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.

Resources