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>";
}
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>';
}
Have been trying to get a variable percentage from two helper functions inside .phtml file in magento
basically i have two variables based on two helper functions that on their own output/echo static numbers. Problem is in the below php they are just displaying as the value and not dividing/multiplying. So like i said the helper data & module work is using the data as variables to do/complete the equation within the .phtml file. See code below
get->FunctionA() just equals a round number value (from a collection)
get->FunctionB() just equals a round number value as well (from a collection)
probs not the best way but this just outputs two values from helper data and not dividing.
echo Mage::helper('module/data')->getFunctionA() / Mage::helper('module/data')->getFunctionB();
Also this does not work either, just produces the same result and is probs the best/easiest way
$dataA = Mage::helper('module/data')->getFunctionA();
$dataB = Mage::helper('module/data')->getFunctionB();
$result = ($dataA / $dataB) * 100;
echo $result;
Like i said above the values can be echoed (in either helper function or phtml files but the actual calculation does not wont to work
Any help would be great
Ok sorted the problem out. What was happening was that in the helper file the getFunctionA() & getFunctionB() value was being echoed when i should have been just returning the value then echoing the helper function in the .phtml file. Doh! See below the helper function example that is now working Woohoo!!!
public function getFunctionA()
{
$FunctionA = Mage::getModel('module/collection')->getCollection();
$FunctionA->addFieldToFilter('attribute', 'value_to_filter');
$FunctionA->addFieldToFilter('status','1');
return ''.count($FunctionA) . ''; //this line was the problem cause i was echoing & not returning the value
}
Now the value can be echoed in the phtml & the math equation is confirmed working
$dataA = Mage::helper('module/data')->getFunctionA();
$dataB = Mage::helper('module/core')->getFunctionB();
$result = ($dataA / $dataB) * 100;
echo $result;
this code does the math and now i can rest.
I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.
What I'm wondering is if there is a way I can format them to display as such?
Something like [[+MoneyField:formatmoney]]
I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.
You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.
So you'd have to create this custom snippet which could be as simple as
return '$'.number_format($input);
Another version of doing this is calling the snippet directly instead of as an output modifier like so:
[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]
I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)
Update:
Actually found the exact example you want to implement on this link;
http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples
Snippet:
<?php
$number = floatval($input);
$optionsXpld = #explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = #explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Used as output modifier:
[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]
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.
I have an array called config. I'm trying to echo a variable from the array in the session.
I've tried:
echo $this->session->userdata('config['item']');
but it doesn't work. What's wrong with my syntax here? I've print_r'd my session and the items are in the config array. I've also tried:
echo $this->session->userdata("config['item']");
I get no errors this time, but no data either.
If config is an array . And item is string name of what you want to get from config then
echo $this->session->userdata($config['item']);
or
echo $_SESSION[$config['item']];
If config is an array inside session you should first get it.
$tmp = $this->session->userdata('config');
echo $tmp['item'];
or
echo $_SESSION['config']['item']
Sorry for my english.
If you want to use the session array, use the variable, not the function:
echo $this->session->userdata['user_data']['item'];
If you want to write:
$this->session->userdata['user_data']['item'] = 'value';
$this->session->userdata['other_data']['other'] = 'value2';
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.
Always escape your string it should be this way:
echo $this->session->userdata('config[\'item\']');